Q82 — AWS SOA-C02 Ch.1
Question 82 of 100 | ← Chapter 1
A company has created an AWS CloudFormation template that consists of the AWS::EC2::Instance resource and a custom CloudFormation resource. The custom CloudFormation resource is an AWS Lambda function that attempts to run automation on the Amazon EC2 instance. During testing, the Lambda function fails because the Lambda function tries to run before the EC2 instance is launched. Which solution will resolve this issue?
- A. Add a DependsOn attribute to the custom resource. Specify the EC2 instance in the DependsOn attribute. ✓
- B. Update the custom resource's service token to point to a valid Lambda function.
- C. Update the Lambda function to use the cfn-response module to send a response to the custom resource.
- D. Use the Fn::If intrinsic function to check for the EC2 instance before the custom resource runs.
Correct Answer: A. Add a DependsOn attribute to the custom resource. Specify the EC2 instance in the DependsOn attribute.
Explanation
AWS CloudFormation模板中资源的创建默认并行执行,若需确保特定顺序,需利用依赖关系。根据AWS官方文档,`DependsOn`属性可显式指定资源间的依赖,确保目标资源在依赖项创建完成后再执行。题目中自定义资源(Lambda)需在EC2实例启动后运行,添加`DependsOn`属性至自定义资源并指向EC2实例可解决时序问题。选项B涉及服务令牌配置,与资源启动顺序无关;选项C的`cfn-response`模块用于自定义资源响应处理,不直接影响依赖顺序;选项D的`Fn::If`适用于条件判断,无法确保资源创建顺序。选项A直接符合AWS推荐依赖管理方式。