Q66 — AWS DOP-C02 Ch.3
Question 66 of 100 | ← Chapter 3
A DevOps engineer is building a container-based architecture. The engineer has decided to use AWS CloudFormation templates to automatically provision an Amazon ECS cluster and an Amazon EC2 Auto Scaling group to launch EC2 container instances. After successfully creating the CloudFormation stack, the engineer notices that although the ECS cluster and EC2 instances have been created successfully and the stack creation has completed, the EC2 instances are still associated with a different cluster.
- A. Reference the EC2 instances in the AWS::ECS::Cluster resource and reference the ECS cluster in the AWS::ECS::Service resource.
- B. Reference the ECS cluster in the UserData property of the AWS::AutoScaling::LaunchConfiguration resource. ✓
- C. Reference the ECS cluster in the UserData property of the AWS::EC2::Instance resource.
- D. Reference the ECS cluster in an AWS::CloudFormation::CustomResource resource to trigger an AWS Lambda function that registers the EC2 instances with the appropriate ECS cluster.
Correct Answer: B. Reference the ECS cluster in the UserData property of the AWS::AutoScaling::LaunchConfiguration resource.
Explanation
When deploying an ECS cluster using AWS CloudFormation, the ECS agent on EC2 instances must be explicitly configured via the UserData script to join the correct cluster. Setting the ECS_CLUSTER environment variable in the UserData property of AWS::AutoScaling::LaunchConfiguration is the required step; AWS documentation states that ECS container instances depend on this parameter to register with the specified cluster. Option B correctly implements this configuration, whereas the other options do not address dynamic injection of the ECS cluster name at instance launch or rely on non-standard mechanisms.