AWS Builder Center

Building an AWS Chaos Engineering Lab with Terraform, Auto Scaling, and AWS Fault Injection Service

A hands-on journey building an AWS Chaos Engineering lab using Terraform, EC2, Auto Scaling, Application Load Balancer, CloudWatch, and AWS Fault Injection Service to simulate infrastructure failures, test automated recovery, and measure system resilience.

Cloud Operations



Building an AWS Chaos Engineering Lab with Terraform, Auto Scaling, and AWS Fault Injection Service

What happens when one of your EC2 instances suddenly disappears in production?
Does your application continue serving requests? Does Auto Scaling launch a replacement? How quickly does your infrastructure detect the failure? And, perhaps more importantly, how do you know your system is actually resilient?
These are the questions that inspired me to build AWS Resilience Lab, a hands-on Chaos Engineering project designed to simulate infrastructure failures and evaluate automated recovery mechanisms on AWS.
Rather than simply deploying an application and assuming everything works, I wanted to deliberately introduce failures, observe system behaviour, and measure recovery.
This project combines Terraform, Python, Amazon EC2, Application Load Balancer, Auto Scaling, CloudWatch, and AWS Fault Injection Service (FIS).

1. Why Chaos Engineering?

Traditional infrastructure testing often focuses on whether resources deploy successfully and whether an application responds under normal conditions.
However, production environments rarely operate under perfect conditions.
Instances fail. Applications crash. Network connectivity becomes unreliable. Dependencies become unavailable.
Chaos Engineering takes a different approach: deliberately introduce controlled failures into a system to understand how it behaves.
The goal is not to break infrastructure randomly. It is to identify weaknesses before unexpected failures expose them.
For this project, I chose a practical starting point:
What happens when AWS terminates one of the EC2 instances serving my application?
I wanted to verify whether the remaining instance could continue handling traffic while the Auto Scaling Group automatically replaced the terminated instance.

2. Architecture Overview

I designed the environment around a multi-AZ deployment using two public subnets in separate Availability Zones.
The architecture includes:
  • Amazon VPC for network isolation.
  • Two public subnets across Availability Zones.
  • Internet Gateway for internet connectivity.
  • Application Load Balancer for distributing traffic.
  • EC2 instances running a Flask application.
  • Auto Scaling Group maintaining application capacity.
  • AWS Systems Manager for instance management.
  • CloudWatch for infrastructure monitoring.
  • Amazon SNS for notifications.
  • AWS Fault Injection Service for controlled failure experiments.

Architecture flow

Internet
|
v
Application Load Balancer
|
Target Group
Port 5000
/ \
v v
EC2 Node 1 EC2 Node 2
\ /
\ /
Flask Application

Auto Scaling Group
|
AWS Fault Injection
|
Controlled EC2 Termination

CloudWatch
|
CloudWatch Alarms
|
SNS
|
Email Notifications
The design maintains a desired capacity of two instances, with a minimum of two and maximum of four.
This provides a basic environment for testing instance replacement and application availability.

3. Infrastructure as Code with Terraform

I chose Terraform to provision and manage the infrastructure.
Instead of manually creating resources through the AWS Management Console, I separated the configuration into logical files.
terraform/
├── main.tf
├── providers.tf
├── variables.tf
├── network.tf
├── security.tf
├── compute.tf
├── alb.tf
├── autoscaling.tf
├── monitoring.tf
├── iam.tf
├── fis.tf
└── outputs.tf
This structure makes the infrastructure easier to understand, maintain, and extend.
Some of the key Terraform resources include:
  •  aws_vpc 
  •  aws_subnet 
  •  aws_internet_gateway 
  •  aws_security_group 
  •  aws_launch_template 
  •  aws_autoscaling_group 
  •  aws_lb 
  •  aws_lb_target_group 
  •  aws_cloudwatch_metric_alarm 
  •  aws_cloudwatch_dashboard 
  •  aws_fis_experiment_template 
I also used Terraform variables to make deployment configuration reusable.
For example:
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t3.micro"
}

variable "instance_count" {
description = "Desired number of EC2 instances"
type = number
default = 2
}
One important lesson from this process was the value of reviewing Terraform plans before applying changes.
Infrastructure as Code makes deployments repeatable, but it does not eliminate the need to understand what Terraform intends to create, modify, or destroy.

4. Building the Application

For the application layer, I used Python and Flask.
The application is intentionally lightweight because the main focus is infrastructure resilience rather than application complexity.
It exposes four endpoints:
Endpoint
Purpose
 / 
Returns application status and hostname
 /health 
Provides a health-check response
 /failure 
Simulates an HTTP 500 application failure
 /metrics 
Returns basic application information
The application runs using Gunicorn and listens on port 5000.
The  /health  endpoint is particularly important because the Application Load Balancer uses it to determine whether an instance is healthy.
This allows the load balancer to stop routing requests to targets that fail their health checks.

5. Auto Scaling and Load Balancing

I configured an Application Load Balancer to distribute incoming HTTP traffic across the EC2 instances.
The target group uses:
  • Protocol: HTTP
  • Port: 5000
  • Health check path:  /health 
The Auto Scaling Group maintains:
Configuration
Value
Minimum capacity
2
Desired capacity
2
Maximum capacity
4
Instance type
t3.micro
Availability Zones
2
The intended recovery mechanism is straightforward:
  1. An EC2 instance is terminated.
  1. Auto Scaling detects that capacity has fallen below the desired level.
  1. Auto Scaling launches a replacement instance.
  1. The replacement runs the application through its launch configuration.
  1. The target group performs health checks.
  1. The new instance becomes eligible to receive traffic once healthy.
This is the behaviour I want to validate through controlled failure injection.

6. Monitoring with Amazon CloudWatch

A resilience experiment without monitoring would tell me very little.
I therefore configured a CloudWatch dashboard with four monitoring widgets:
  1. Application Load Balancer request count.
  1. Unhealthy application targets.
  1. Load Balancer HTTP 5XX errors.
  1. Auto Scaling Group in-service instances.
I also configured three CloudWatch alarms.
Alarm
What it monitors
ALB errors
Elevated HTTP 5XX errors generated by the load balancer
Unhealthy hosts
Unhealthy application targets
Low capacity
Auto Scaling instances falling below the minimum capacity
The low-capacity alarm is particularly important because it is configured as the stop condition for the FIS experiment.
This introduces an additional safeguard into the experiment design.
I also configured Amazon SNS to deliver email notifications when alarms change state.

7. AWS Systems Manager Instead of SSH

One security decision I made was to avoid opening SSH access to the EC2 instances.
Instead, I configured an EC2 IAM role with:
  •  AmazonSSMManagedInstanceCore 
  •  CloudWatchAgentServerPolicy 
This allows me to manage instances through AWS Systems Manager.
It also means I can inspect application services and execute commands without exposing port 22 to the internet.
For example, I used Systems Manager to verify the application service:
sudo systemctl status resiliencelab --no-pager
And check whether the application was listening:
sudo ss -lntp | grep 5000
Finally, I tested the health endpoint locally:
curl -i http://localhost:5000/health
Both EC2 instances returned HTTP 200 and reported a healthy application state.

8. Introducing Chaos with AWS Fault Injection Service

The next major component is AWS Fault Injection Service.
FIS allows controlled fault injection against AWS resources.
For this project, I configured an experiment using:
aws:ec2:terminate-instances
The experiment targets one EC2 instance selected through the project tag.
The intended target selection is:
COUNT(1)
This means the experiment selects one matching instance rather than terminating the entire application fleet.
I also configured a CloudWatch alarm as the experiment stop condition.
This is important because chaos experiments should have boundaries. A test should not continue if the environment reaches an unacceptable state.
The first experiment will deliberately terminate one instance and observe how the remaining infrastructure responds.

9. Verifying the Infrastructure Before Failure Injection

Before introducing any failures, I verified the infrastructure using AWS CLI.

Auto Scaling Group

The following command confirmed the desired capacity and instance health:
aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-names aws-resilience-lab-asg \
--query 'AutoScalingGroups[].{
Desired:DesiredCapacity,
Min:MinSize,
Max:MaxSize,
Instances:Instances[].{
ID:InstanceId,
Health:HealthStatus,
Lifecycle:LifecycleState
}
}' \
--output json
The result showed:
  • Desired capacity: 2
  • Minimum capacity: 2
  • Maximum capacity: 4
  • Two healthy instances
  • Both instances in  InService  state

Application Load Balancer targets

I also checked target group health:
aws elbv2 describe-target-health \
--target-group-arn YOUR_TARGET_GROUP_ARN \
--output table
Both EC2 instances were registered on port 5000 and reported  healthy .

CloudWatch baseline

I queried the Auto Scaling metric:
aws cloudwatch get-metric-statistics \
--namespace AWS/AutoScaling \
--metric-name GroupInServiceInstances \
--dimensions Name=AutoScalingGroupName,Value=aws-resilience-lab-asg \
--start-time "$(date -u -d '30 minutes ago' +%Y-%m-%dT%H:%M:%SZ)" \
--end-time "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--period 60 \
--statistics Average \
--output table
The recorded datapoints consistently showed two in-service instances.
This established the baseline against which recovery will be measured.

10. Challenges Encountered

Building this environment was not entirely straightforward.
A few issues provided useful learning opportunities.
Terraform variable duplication
I encountered a duplicate variable declaration during configuration.
The issue reinforced the importance of maintaining a clean variable definition and validating Terraform configuration before planning deployments.
CIDR configuration
An invalid security group CIDR initially caused Terraform planning to fail.
The correct format for a single public IP address is:
allowed_cidr = "YOUR_PUBLIC_IP/32"
CloudWatch dimension formatting
I also encountered issues constructing the correct CloudWatch metric dimensions from AWS resource ARNs.
This mattered because a metric alarm can exist successfully while monitoring the wrong resource dimension.
Application Load Balancer connectivity
Although both targets were healthy and the ALB was active, external connectivity from my development workstation timed out.
This highlighted an important distinction: healthy backend targets do not automatically prove that the entire external request path is working.
I am treating this as a separate networking verification task rather than assuming that a healthy target group guarantees end-to-end availability.

11. Current Project Status

At this stage, the infrastructure has been provisioned and the following components have been verified:
Component
Status
VPC and networking
Provisioned
Two public subnets
Provisioned
Application Load Balancer
Provisioned
EC2 instances
Running
Auto Scaling Group
Healthy
Flask application
Running
Target group health checks
Healthy
Systems Manager
Verified
CloudWatch dashboard
Deployed
CloudWatch alarms
Deployed
SNS email subscription
Confirmed
FIS experiment template
Configured
First termination experiment
Pending
Recovery measurements
Pending
External ALB connectivity
Under investigation
I am deliberately separating infrastructure deployment from experiment execution.
A successful Terraform deployment is not proof of resilience. The system must demonstrate that it can recover from failure.

12. What I Plan to Measure Next

The next stage is the actual chaos experiment.
I plan to capture the following timestamps:
Metric
Measurement
T0
FIS experiment starts
T1
EC2 termination occurs
T2
Auto Scaling detects reduced capacity
T3
Replacement instance launches
T4
Replacement registers with target group
T5
Replacement becomes healthy
T6
Application service returns to baseline
From these measurements, I can calculate:
Instance replacement time
 T3 - T1 
Target recovery time
 T5 - T1 
End-to-end recovery time
 T6 - T1 
I also want to investigate CPU stress and application-level failure scenarios after validating the first experiment.
These additional experiments will help distinguish infrastructure recovery from application recovery.

13. What This Project Has Taught Me

One of the biggest lessons from building this lab is that resilience is not a single AWS service or configuration.
It is the result of multiple components working together:
  • Load balancing distributes traffic.
  • Health checks identify unhealthy targets.
  • Auto Scaling restores capacity.
  • Systems Manager enables operational access.
  • CloudWatch provides observability.
  • SNS communicates incidents.
  • Fault Injection Service helps validate recovery mechanisms.
More importantly, resilience needs to be tested.
It is easy to configure an Auto Scaling Group and assume that it will handle failures correctly. It is much more valuable to introduce a controlled failure, observe the behaviour, and measure the actual recovery time.




Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article