How to Pass a CloudFormation Log Group Name to an AWS Lambda Function?
Short answer
D Pass the log group's Amazon Resource Name (ARN) as an environment variable to the Lambda function.
Question
Key Takeaways
This question tests your ability to wire CloudFormation resource attributes to Lambda runtime configuration; the trap is choosing template-level features like Mappings or AWS::Include that do not directly expose runtime values.
Learn the correct way to pass a CloudFormation-created log group resource to an AWS Lambda function at runtime. The community agrees that an environment variable referencing the log group's ARN is the best solution.
Common mistake: Choosing option C (Mappings) or option A (AWS::Include) because they seem like CloudFormation-based ways to share values; however, they do not provide a direct, automated reference to a resource's dynamically generated ARN.
Expert Analysis
Why the Answer Is Correct
Option D is correct because Lambda functions can read environment variables at runtime, and CloudFormation can dynamically generate the log group's ARN using the !GetAtt intrinsic function. This approach is simple, maintainable, and follows AWS best practices for passing resource references into application code. The environment variable is resolved at stack deployment time, so the Lambda function has the correct ARN without hardcoding.
Why the Other Options Are Wrong
Option A (AWS::Include transform) is used for template modularization and does not pass runtime values to an application. Option B (user data) is intended for EC2 instance bootstrap scripts, not Lambda functions. Option C (Mappings) is for static key-value pairs and cannot reference a resource that is created in the same template, nor does it automatically update if the resource is replaced.
Community Comment Notes
Comment [3] correctly explains that specifying the ARN as an environment variable provides a flexible way to pass dynamic resource references without hardcoding. Comment [4] provides a concrete example using !GetAtt MyLogGroup.Arn, which is exactly how this is implemented in practice. Comment [1] and [2] detail why the other options are unsuitable for serverless Lambda-based applications.
Official Reference
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-lambda-function-environment.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html
- https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html
These links open the official vendor documentation in a new tab.
Exam Strategy
Look for the option that uses an environment variable to pass a resource ARN, especially when the question mentions "at runtime" and involves Lambda. Remember that CloudFormation intrinsic functions like !GetAtt are the standard way to expose resource attributes, not Mappings or transforms.
Community Votes
100% of anonymous learners picked answer D. Votes are pick records left by other test-takers — they are not the verified answer.
Discussion 6 contributions
Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.
From public community threads 6
-
Abdullah22 👍 4 Answered DExplanation of why other options are less suitable: A. AWS::Include transform: While the AWS::Include transform allows importing values from other templates, it's generally used for larger configurations and not ideal for this specific purpose of passing a single value to the application. B. User data: The user data section in CloudFormation is typically used for one-time configuration scripts and not intended for passing sensitive information like log group names or ARNs, raising security concerns. C. CloudFormation Mappings: While mappings can store key-value pairs within the template, they are not directly accessible as environment variables within the application code.
-
CrescentShared 👍 4 Answered DUser data is typically used to pass startup scripts to EC2 instances, not Lambda functions. This would not be the appropriate mechanism for a serverless application using Lambda. The Mappings section in a CloudFormation template is used to define sets of key-value pairs that can be used to specify conditional parameter values based on region or other criteria. It doesn't provide a direct way to make the log group's name available to the Lambda function at runtime.
-
SerialiDr 👍 2 Answered DBy specifying the log group's ARN as an environment variable in the CloudFormation template for the Lambda function, the developer can easily access this information within the application code at runtime. This method provides a straightforward way to pass dynamic configuration or resource references to AWS Lambda functions without hardcoding values, thereby maintaining flexibility and ensuring that the application can utilize the correct resources after deployment.
-
albert_kuo 👍 1 Answered DResources: MyLogGroup: Type: AWS::Logs::LogGroup Properties: LogGroupName: !Sub "/aws/lambda/${AWS::StackName}-log-group" MyLambdaFunction: Type: AWS::Lambda::Function Properties: FunctionName: MyFunction Handler: index.handler Runtime: nodejs14.x Role: arn:aws:iam::123456789012:role/service-role/MyLambdaRole Environment: Variables: LOG_GROUP_ARN: !GetAtt MyLogGroup.Arn
-
KarBiswa 👍 1 Answered DVery nice question totally confusing. D is correct
Show 1 more comment
-
65703c1 👍 2 Answered DD is the correct answer.
Reader comments & corrections
No reader corrections yet — spotted an error or have a note? Share it below.
Related Analysis
Practice All DVA-C02 Questions
Access 100 questions with complete answers and detailed explanations.
View Full DVA-C02 Practice Test →