Defining an EC2 Auto Scaling launch configuration

Although you have defined an EC2 Auto Scaling group resource, you cannot yet deploy your CloudFormation template as the Auto Scaling group references a resource called ApplicationAutoscalingLaunchConfiguration, which is yet to be defined.

An EC2 Auto Scaling launch configuration defines the configuration that is applied to each instance at launch time, and provides a common approach to ensuring each instance in your Auto Scaling group is consistent.

The following example demonstrates configuring an Auto Scaling launch configuration within your CloudFormation template:

...
...
Parameters:
ApplicationDesiredCount:
Type: Number
Description: Desired EC2 instance count
ApplicationImageId:
Type: String
Description: ECS Amazon Machine Image (AMI) ID
ApplicationSubnets:
Type: List<AWS::EC2::Subnet::Id>
Description: Target subnets for EC2 instances

Resources:
ApplicationAutoscalingLaunchConfiguration:
Type: AWS::AutoScaling::LaunchConfiguration

Properties:
ImageId: !Ref ApplicationImageId
InstanceType: t2.micro
KeyName: admin
IamInstanceProfile: !Ref ApplicationAutoscalingInstanceProfile
SecurityGroups:
- !Ref ApplicationAutoscalingSecurityGroup
UserData:
Fn::Base64:
Fn::Sub: |
#!/bin/bash
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} \
--resource ApplicationAutoscalingLaunchConfiguration \
--region ${AWS::Region}
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} \
--resource ApplicationAutoscaling \
--region ${AWS::Region}
ApplicationCluster:
Type: AWS::ECS::Cluster
Properties:
ClusterName: todobackend-cluster
ApplicationAutoscaling:
Type: AWS::AutoScaling::AutoScalingGroup
Properties:
LaunchConfigurationName: !Ref ApplicationAutoscalingLaunchConfiguration
MinSize: 0
MaxSize: 4
DesiredCapacity: !Ref ApplicationDesiredCount
VPCZoneIdentifier: !Ref ApplicationSubnets
Tags:
- Key: Name
Value: !Sub ${AWS::StackName}-ApplicationAutoscaling-instance
PropagateAtLaunch: "true"
Defining an EC2 Auto Scaling Launch Configuration

Notice that you specify a AWS::AutoScaling::LaunchConfiguration resource type and configure the following properties for your launch configuration:

Notice the use of the Fn::Sub function followed by the pipe operator (|), which enables you to enter free-form text that will honour all line breaks and allows you to reference the correct stack name and AWS region using the AWS::StackName and AWS::Region pseudo-parameters.

You may notice that the set -e flag is not set in the UserData bash script, and this is deliberate as we want the cfn-signal script to report the exit code of the cfn-init script to CloudFormation (as defined by -e $? option, where $? outputs the exit code of the last process). If you were to include set -e, the script would exit immediately if cfn-init returned an error, and cfn-signal would not be able to signal CloudFormation of the failure.
ApplicationDesiredCount=1
ApplicationImageId=ami-ec957491

ApplicationSubnets=subnet-a5d3ecee,subnet-324e246f
Adding the ApplicationImageId parameter to the dev.cfg file