A company has an AWS CloudFormation template that includes an AWS::EC2::Instance resource and a custom resource (Lambda function). The Lambda function fails because it runs 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.
The AWS Cloud Operations and Infrastructure-as-Code documentation specifies that when using AWS CloudFormation, resources are created in parallel by default unless explicitly ordered using DependsOn.
If a custom resource (Lambda) depends on another resource (like an EC2 instance) to exist before execution, a DependsOn attribute must be added to enforce creation order. This ensures the EC2 instance is launched and available before the custom resource executes its automation logic.
Updating the service token (Option B) doesn’t affect order of execution. The cfn-response module (Option C) handles callback communication but not sequencing. Fn::If (Option D) is for conditional creation, not dependency control.
Therefore, Option A is correct — adding a DependsOn attribute guarantees that CloudFormation provisions the EC2 instance before executing the Lambda custom resource.
[Reference: AWS Cloud Operations & Infrastructure-as-Code Guide – Using DependsOn for Resource Creation Order in CloudFormation Templates, ]
Contribute your Thoughts:
Chosen Answer:
This is a voting comment (?). You can switch to a simple comment. It is better to Upvote an existing comment if you don't have anything to add.
Submit