Question #844
A developer is creating a serverless application that uses an AWS Lambda function to process messages from an Amazon SQS queue. The developer is using AWS CloudFormation to deploy the application. The developer has created an SQS queue in the CloudFormation template. The developer needs to ensure the queue's Amazon Resource Name (ARN) is available to the Lambda function at runtime. Which solution will meet this requirement?
Use the AWS::Include transform in CloudFormation to dynamically include the queue's ARN in the Lambda function configuration.
Embed the queue's ARN in the Lambda function's user data section within the CloudFormation template.
Specify the queue's ARN in the CloudFormation template's Mappings section and reference it in the Lambda function.
Pass the queue's ARN as an environment variable to the Lambda function using CloudFormation.
Explanation
Answer D is correct because environment variables in AWS Lambda allow dynamic values to be passed at runtime. By using CloudFormation's intrinsic functions (e.g., !GetAtt MyQueue.Arn), the SQS queue's ARN is resolved during stack deployment and injected into the Lambda function's environment. This approach ensures the ARN is always up-to-date and avoids hardcoding.
Why other options are incorrect:
- A: AWS::Include transform is used for reusable template snippets but does not dynamically resolve runtime-generated values like ARNs.
- B: Lambda functions do not have a 'user data' section; this is specific to EC2 instances.
- C: Mappings are static and predefined, making them unsuitable for dynamically generated ARNs created during stack deployment.
Key Points:
1. Use environment variables to pass dynamic resource attributes (e.g., ARNs) to Lambda functions.
2. CloudFormation intrinsic functions like !GetAtt or !Ref retrieve resource attributes during stack creation.
3. Avoid hardcoding ARNs or using static configurations for resources created dynamically.
Answer
The correct answer is: D