AWS Certified Developer – Associate / Question #643 of 557

Question #643

A developer is testing an AWS CDK serverless application that includes Lambda functions and Amazon API Gateway. The developer wants to test a specific Lambda function locally by invoking it through the API Gateway. The workstation has AWS SAM and AWS CDK installed. How can the developer achieve this?

A

Run the SAM package and SAM deploy commands. Use the AWS Management Console to configure API Gateway. Test the API Gateway endpoint locally.

B

Run the CDK synth and CDK deploy commands. Use the AWS Management Console to configure API Gateway. Test the API Gateway endpoint locally.

C

Run the CDK synth and SAM local invoke commands with the function construct identifier and the path to the synthesized CloudFormation template.

D

Run the CDK synth and SAM local start-lambda commands with the function construct identifier and the path to the synthesized CloudFormation template.

Explanation

Answer D is correct because:
1. cdk synth generates the CloudFormation template required by SAM to understand the application structure.
2. sam local start-lambda starts a local Lambda endpoint, allowing API Gateway (when run locally) to invoke the Lambda function locally.

Why other options are incorrect:
- A & B: Both involve deploying resources to AWS (via sam deploy or cdk deploy), which does not achieve local testing.
- C: sam local invoke tests the Lambda function in isolation, not through API Gateway.

Key Points:
- Use cdk synth to generate the template.
- Use sam local start-lambda to emulate Lambda for local API Gateway testing.
- Avoid deploying to AWS for local-only testing scenarios.

Answer

The correct answer is: D