An engineer used AWS CloudFormation to provision an Amazon ECS cluster and an EC2 Auto Scaling group for the container instances. After the stack completed, the EC2 instances launched but registered with a different ECS cluster than intended. What change should be made to the CloudFormation template to ensure the instances register with the correct ECS cluster?
Choose an answer
Tap an option to check your answer.
Correct answer: Include a reference to the ECS cluster in the UserData of the AWS::AutoScaling::LaunchConfiguration resource..
Why this is the answer
The correct answer is to include a reference to the ECS cluster in the UserData of the AWS::AutoScaling::LaunchConfiguration resource. When EC2 instances launch as part of an ECS cluster, they need to know which cluster to join. This is typically configured in the instance's UserData script, which runs on first boot. The ECSCLUSTER environment variable or a similar configuration in /etc/ecs/ecs.config within the UserData script tells the ECS agent on the instance which cluster to register with. Referencing EC2 instances in AWS::ECS::Cluster or the ECS cluster in AWS::ECS::Service is incorrect because these resources define the cluster and services, not how individual instances join a cluster. AWS::EC2::Instance is not used here as instances are launched by an Auto Scaling group. A custom resource with a Lambda function is an overly complex solution for a problem that can be solved with standard UserData configuration.
Pass your exam — without the endless answer hunt
Get every verified question and explanation for this exam in one place, and save hours of prep. 1,000+ certifications · 20+ languages · free to start.
Pass your exam faster → No card needed