AWS Certified Developer – Associate / Question #719 of 557

Question #719

A developer is encountering frequent deployment failures in the AWS Lambda functions managed by their team's TypeScript application. To reduce these failures, the developer aims to establish automated testing that replicates the Lambda execution environment. The solution must allow developers to execute tests locally and be incorporated into the CI/CD pipeline prior to deployment using the AWS Cloud Development Kit (AWS CDK).

Which approach fulfills these requirements?

A

Use CDK synth to generate CloudFormation templates. Create test scripts that invoke the Lambda functions using these templates. Document the process for the team and update the CI/CD pipeline to execute the test scripts during deployment.

B

Implement a mocking framework to simulate the Lambda environment. Write unit tests that directly call the handler function with mock events. Document how to run the tests locally and update the CI/CD pipeline to include the unit testing framework.

C

Install the AWS Serverless Application Model (AWS SAM) CLI. Use the SAM local generate-event command to create test events. Develop automated test scripts that invoke the Lambda functions with SAM local invoke. Document the scripts for the team and integrate them into the CI/CD pipeline before CDK deployment.

D

Create a Docker container configured with the Node.js Lambda runtime. Develop test scripts to invoke the Lambda functions within the container. Document the Docker setup for local testing and modify the CI/CD pipeline to run the containerized tests.

Explanation

Option C is correct because AWS SAM CLI provides tools like sam local invoke to test Lambda functions in an environment that closely mirrors AWS Lambda. This approach allows developers to test locally and integrate these tests into CI/CD pipelines, ensuring issues are caught before deployment. SAM's generate-event command creates realistic test events, and sam local invoke executes the function with the correct runtime and dependencies.

Other options are less suitable:
- A relies on CloudFormation templates but does not replicate the Lambda runtime environment.
- B uses mocking frameworks, which do not test the actual Lambda environment.
- D requires manual Docker setup, which is more complex compared to SAM's built-in local testing.

Key points: SAM CLI emulates Lambda's execution environment, supports local testing, and integrates with CDK pipelines, making it the optimal solution.

Answer

The correct answer is: C