Skill 3.2.1
Develop and validate tests (for example, by using AWS SAM, AWS CDK, AWS CloudFormation).
Key Concepts
Infrastructure code is tested too. cfn-lint and aws cloudformation validate-template catch template errors, sam validate --lint checks a SAM template, and a change set shows exactly which resources an update will add, modify or replace. In the AWS CDK, the assertions module synthesises a stack in a unit test and checks the generated template, for example that a queue has encryption enabled or that exactly one Lambda function exists.
Unit tests should exercise your business logic without calling real AWS services. Keeping the Lambda handler thin and moving logic into plain functions makes this easy, and AWS calls can be replaced with mocks or stubs (for example, moto or botocore.stub.Stubber in Python, or the AWS SDK client mock libraries in JavaScript).
The AWS SAM CLI complements unit tests. sam local invoke runs a function locally in a container that mimics the Lambda runtime, sam local start-api serves your API Gateway endpoints locally, and sam local generate-event produces sample events for services such as S3, SQS and API Gateway to use as test input.
Locally, the AWS SAM CLI runs functions in containers with sam local invoke and serves APIs with sam local start-api, using events created by sam local generate-event. In the Lambda console you can save test events, including shareable ones that other developers in the account can use, and invoke the function with them.
For integration testing, deploy the stack to a separate test environment with sam deploy (or sam sync for fast iteration) and invoke it with sam remote invoke, the AWS CLI (aws lambda invoke) or the SDK, checking results in CloudWatch Logs and X-Ray traces.
Lambda functions can be invoked from the console with saved test events, from the CLI with aws lambda invoke, or with sam remote invoke. API Gateway has a test feature in the console that calls a method without deploying, showing the request, response and execution logs. Step Functions shows each state's input and output for an execution.
CloudWatch Logs, Logs Insights and X-Ray traces show what happened during a test. CloudWatch Synthetics canaries run scripts on a schedule to check endpoints and user flows continuously.