Question #1017
A developer is using AWS CloudFormation to deploy a serverless application that includes an Amazon S3 bucket for hosting static assets, an AWS Lambda function for backend logic, and Amazon API Gateway to expose RESTful APIs. The Lambda function's source code is packaged into a ZIP file and stored in an S3 bucket. The CloudFormation template references the S3 object key of the ZIP file in the Lambda resource definition.
The developer modifies the Lambda function's code, uploads the updated ZIP file to the same S3 bucket with the same object key, and updates the CloudFormation stack. However, the Lambda function does not receive the new code changes after the stack update.
What should the developer do to ensure CloudFormation deploys the updated Lambda code during stack updates?
Create a new Lambda function version and reference it in the CloudFormation template before updating the stack.
Modify the S3 object version or change the S3 object key in the CloudFormation template before updating the stack.
Upload the updated ZIP file to a different S3 bucket and update the bucket reference in the CloudFormation template.
Enable automatic code versioning for the Lambda function in the CloudFormation template before updating the stack.
Explanation
Answer B is correct because AWS CloudFormation uses the S3 object key (and version, if enabled) to determine whether a resource update is needed. When the developer uploads a new ZIP file with the same key, CloudFormation does not detect the change unless the object version or key is modified. By changing the S3 object key or referencing a new version (if S3 versioning is enabled), CloudFormation recognizes the update and deploys the new Lambda code during the stack update.
Incorrect options:
- A: Creating a new Lambda version manually does not resolve the root issue, as CloudFormation manages versions automatically when the underlying code changes.
- C: Uploading to a different bucket is unnecessary; changing the key in the same bucket suffices.
- D: Lambda's automatic versioning does not force CloudFormation to update the code unless the template references a new version explicitly.
Key Takeaway: To ensure CloudFormation deploys updated Lambda code, modify the S3 object key or version referenced in the template to trigger a resource update.
Answer
The correct answer is: B