AWS Certified Developer: Creating Application Test Events and JSON Payloads
Create application test events (for example, JSON payloads for testing AWS Lambda, API Gateway, AWS SAM resources)
AWS Certified Developer: Creating Application Test Events and JSON Payloads
This guide covers the essential skills for Content Domain 3 (Deployment) of the DVA-C02 exam, focusing on how to construct, manage, and use JSON payloads to test serverless resources like AWS Lambda, API Gateway, and AWS SAM.
Learning Objectives
After studying this guide, you should be able to:
- Construct valid JSON test events for various AWS service triggers.
- Differentiate between Proxy and Non-Proxy integration payloads in API Gateway.
- Use the AWS SAM CLI to generate sample event data locally.
- Implement Mock Integrations in API Gateway for testing without backend dependencies.
- Leverage Amazon Q Developer to generate automated test cases.
Key Terms & Glossary
- Payload: The actual data (usually JSON) transmitted in a request or response. In Lambda, this is the
eventobject. - AWS_PROXY: A streamlined integration type where API Gateway passes the entire HTTP request to Lambda as a structured JSON object.
- Mock Integration: An API Gateway setting that returns a response directly from the gateway without calling a backend service.
- Test Event: A saved JSON snippet in the AWS Lambda console used to simulate a specific service trigger (e.g., an S3 upload or a Kinesis stream record).
- Lambda Alias: A pointer to a specific Lambda function version, allowing you to route traffic to different environments (e.g.,
PRODvsDEV) using the same ARN base.
The "Big Idea"
In a serverless architecture, components are decoupled and event-driven. Testing these components requires simulating the "handshake" between services. Because you cannot easily "click a button" to trigger an S3 event during local development, you must master the art of JSON Payload Simulation. By crafting precise JSON objects that mimic AWS service signatures, you can validate application logic in isolation before deploying to production.
Formula / Concept Box
| Feature | API Gateway Proxy Event | API Gateway Custom Event |
|---|---|---|
| Data Structure | Highly structured (headers, body, pathParameters) | Defined by your Mapping Template |
| Flexibility | Rigid; Lambda must parse the whole object | Flexible; Lambda only gets what it needs |
| Setup | Fast; zero configuration needed | Requires VTL (Velocity Mapping Templates) |
| Best For | Standard web apps, REST APIs | Legacy system integration, data stripping |
Hierarchical Outline
- Lambda Test Events
- Console Testing: Saving named JSON templates in the AWS Console.
- Input vs. Output: The
eventobject (input) vs. the return JSON (output).
- API Gateway Integrations
- Lambda Proxy (
AWS_PROXY): Passing the raw request. Requires a specific response format:{ "statusCode": 200, "body": "..." }. - Mock Integrations: Useful for frontend teams to build against while the backend is in progress.
- Lambda Proxy (
- Local Testing with AWS SAM
sam local generate-event: Commands to create sample payloads for S3, DynamoDB, SNS, etc.sam local invoke: Using those payloads to run functions locally.
- Automated Testing
- Amazon Q Developer: Generating unit tests and test events using AI assistance.
Visual Anchors
The Event Lifecycle
Mapping vs. Proxy Integration
\begin{tikzpicture}[node distance=2cm] \draw[thick] (0,0) rectangle (3,2) node[pos=.5] {Client Request}; \draw[->, thick] (3,1) -- (5,1) node[midway, above] {JSON Payload}; \draw[thick, fill=blue!10] (5,-0.5) rectangle (10,2.5); \node at (7.5, 2.2) {\textbf{API Gateway}}; \draw[thick] (5.5,0.5) rectangle (9.5,1.5) node[pos=.5] {Mapping Template}; \draw[->, thick] (10,1) -- (12,1) node[midway, above] {Transformed JSON}; \draw[thick, fill=green!10] (12,0) rectangle (15,2) node[pos=.5] {Lambda}; \end{tikzpicture}
Definition-Example Pairs
- Event Object: The data structure received by a Lambda handler.
- Example: An S3 event object contains the bucket name (
s3.bucket.name) and the file key (s3.object.key).
- Example: An S3 event object contains the bucket name (
- Staging Variables: Key-value pairs defined in API Gateway stages.
- Example: Using
${stageVariables.functionName}in a payload to dynamically call av1orv2Lambda alias.
- Example: Using
- CORS (Cross-Origin Resource Sharing): A browser security feature.
- Example: If testing an API from a local web server (localhost), the JSON response from API Gateway must include the
Access-Control-Allow-Originheader.
- Example: If testing an API from a local web server (localhost), the JSON response from API Gateway must include the
Worked Examples
Example 1: Generating an S3 Test Event with SAM
If you need to test a Lambda function that processes image uploads, you don't need to actually upload a file to S3.
Command:
sam local generate-event s3 put --bucket my-test-bucket --key image.jpg > event.jsonResulting JSON (event.json):
{
"Records": [
{
"s3": {
"bucket": { "name": "my-test-bucket" },
"object": { "key": "image.jpg" }
}
}
]
}Example 2: API Gateway Proxy Response Requirement
When using AWS_PROXY, your Lambda function must return a specific JSON format, or the API will throw a 502 Bad Gateway error.
Correct Lambda Return JSON:
{
"isBase64Encoded": false,
"statusCode": 200,
"headers": { "Content-Type": "application/json" },
"body": "{\"message\": \"Success\"}"
}[!IMPORTANT] Note that the
bodyfield must be a stringified JSON, not a raw JSON object.
Checkpoint Questions
- Which AWS SAM command is used to create a sample JSON payload for an Amazon SNS trigger?
- In an
AWS_PROXYintegration, what happens if the Lambda function returns a simple string like "Hello World" instead of a JSON object with astatusCode? - True or False: Mock integrations in API Gateway require a Lambda function to be associated with the method.
- What is the benefit of using
stageVariablesin an API Gateway integration request payload?
▶Click to see answers
sam local generate-event sns notification.- API Gateway will return a 502 Bad Gateway error because it cannot parse the required structure.
- False. Mock integrations return responses defined within API Gateway itself.
- It allows you to dynamically route requests to different backend resources (like different Lambda aliases) based on the deployment stage (Dev, Test, Prod).