Q6 — AWS DVA-C02 Ch.2
Question 6 of 100 | ← Chapter 2
A developer is implementing a serverless application using the AWS Cloud Development Kit (AWS CDK). The developer will provision an AWS Lambda function and an Amazon API Gateway API during AWS CloudFormation stack creation. The developer has installed both the AWS Serverless Application Model (AWS SAM) and AWS CDK locally. How can the developer test a specific Lambda function locally?
- A. Run the sam package and sam deploy commands. Create a Lambda test event from the AWS Management Console. Test the Lambda function.
- B. Run the cdk synth and cdk deploy commands. Create a Lambda test event from the AWS Management Console. Test the Lambda function.
- C. Run cdk synth and sam local invoke commands, specifying the function construct identifier and the path to the synthesized CloudFormation template. ✓
- D. Run cdk synth and samlocal start-lambda commands, specifying the function construct identifier and the path to the synthesized CloudFormation template.
Correct Answer: C. Run cdk synth and sam local invoke commands, specifying the function construct identifier and the path to the synthesized CloudFormation template.
Explanation
The cdk synth command synthesizes an AWS CDK application into a CloudFormation template. This is the process of converting an AWS CDK application into a native AWS CloudFormation template. The sam local invoke command is part of the AWS SAM CLI and allows simulating Lambda function invocations locally. By specifying the function’s logical ID and the path to the synthesized CloudFormation template, developers can locally invoke and test a specific Lambda function. Analysis of other options: A. The cdk synth and cdk deploy commands synthesize and deploy CDK stacks but do not directly simulate Lambda invocations locally. Creating test events via the AWS Management Console is unnecessary when sam local invoke enables direct local testing. C. The sam package and sam deploy commands are part of the AWS SAM toolchain for packaging and deploying serverless applications and are not directly applicable for local testing of CloudFormation stacks generated by AWS CDK. D. The combination cdk synth and samlocal start-lambda is incorrect; 'samlocal start-lambda' is not a valid AWS SAM CLI command. Therefore, option C is the correct answer, as it combines AWS CDK template synthesis with AWS SAM’s local Lambda invocation capability, enabling effective local testing of Lambda functions.