Sub topic 6
Recreating infrastructure with infrastructure as code
This chapter assumes you can read a CloudFormation template, know what an Auto Scaling group is, and have the AWS CLI configured. It explains why the recovery Region's infrastructure should come from the same code as production, and which parts of that code usually break when you run it in a second Region.
Why code beats manual rebuilds
In backup and restore and pilot light, you recreate infrastructure during the disaster. Console clicks are slow and hard to repeat while you are under pressure. They also drift from production over time. Infrastructure as code (IaC) is a template that describes your resources. You deploy that template to the recovery Region and get the same result each time, so recovery time is predictable.
IaC also changes what "dormant" means. A pilot light stack can exist with its compute set to zero. Failover then changes a few parameters instead of building a stack from scratch.
A Region-neutral template
The template below takes capacity and subnets as parameters. It resolves the AMI (machine image) through a Systems Manager public parameter, because AMI IDs differ in every Region.
AWSTemplateFormatVersion: "2010-09-09"
Description: App compute tier that deploys unchanged to any Region
Parameters:
LatestAmiId:
Type: AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>
Default: /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64
SubnetIds:
Type: List<AWS::EC2::Subnet::Id>
MinSize:
Type: Number
Default: 0
DesiredCapacity:
Type: Number
Default: 0
MaxSize:
Type: Number
Default: 12
Resources:
LaunchTemplate:
Type: AWS::EC2::LaunchTemplate
Properties:
LaunchTemplateData:
ImageId: !Ref LatestAmiId
InstanceType: t3.medium
AppGroup:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
MinSize: !Ref MinSize
DesiredCapacity: !Ref DesiredCapacity
MaxSize: !Ref MaxSize
VPCZoneIdentifier: !Ref SubnetIds
LaunchTemplate:
LaunchTemplateId: !Ref LaunchTemplate
Version: !GetAtt LaunchTemplate.LatestVersionNumberDeploy it to the recovery Region with the compute fleet off:
aws cloudformation deploy \
--region us-west-2 \
--stack-name app-dr \
--template-file app.yaml \
--parameter-overrides MinSize=0 DesiredCapacity=0 MaxSize=12 \
SubnetIds=subnet-0aaa1111,subnet-0bbb2222At failover, run the same command with MinSize=4 DesiredCapacity=4. The change goes through the stack, so the stack stays the source of truth. If you scale the group by hand, as in the earlier CLI example, the stack no longer matches reality. That is drift, and you should fold such changes back into the parameters.
What breaks in the second Region
Most failed DR deployments come from hardcoded values. Check for these:
| Problem | Fix |
|---|---|
| Hardcoded AMI IDs | Use SSM parameters, or copy your own AMIs to the DR Region |
| Hardcoded AZ names or subnet IDs | Pass subnets as parameters |
| Hardcoded ARNs with a Region or account ID | Use `!Sub` with `${AWS::Region}` and `${AWS::AccountId}` |
| KMS keys that exist only in the primary Region | Create keys, or multi-Region keys, in the DR Region |
| Secrets and parameters that were never copied | Replicate them ahead of time |
| Service quotas lower in the DR Region | Request increases before you need them |
| Global names, such as S3 bucket names | Include the Region in the name |
Keep the recovery path independent
Store templates and pipeline definitions somewhere that survives the loss of the primary Region. Examples are a Git host outside that Region or an S3 bucket replicated to the DR Region. If your only copy of app.yaml sits in a bucket in the failed Region, you cannot deploy it.
Deploy to the DR Region on every release, even when capacity is zero. A pipeline that only deploys to production finds template errors in the DR Region during the disaster. For many Regions or accounts, CloudFormation StackSets can roll one template out to all of them.
Exercise
Your DR stack deploys fine in us-west-2 during a test, but the Auto Scaling group fails to launch instances. The launch template contains an AMI ID copied from us-east-1. What is the cause and the fix?
Show answer
AMI IDs are Region-specific, so the ID from us-east-1 does not exist in us-west-2. Fix it by resolving the AMI through an SSM parameter, as in the template above, or by copying your own AMI to us-west-2 and referencing it per Region.