Study Guide915 words

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 event object.
  • 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., PROD vs DEV) 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

FeatureAPI Gateway Proxy EventAPI Gateway Custom Event
Data StructureHighly structured (headers, body, pathParameters)Defined by your Mapping Template
FlexibilityRigid; Lambda must parse the whole objectFlexible; Lambda only gets what it needs
SetupFast; zero configuration neededRequires VTL (Velocity Mapping Templates)
Best ForStandard web apps, REST APIsLegacy system integration, data stripping

Hierarchical Outline

  1. Lambda Test Events
    • Console Testing: Saving named JSON templates in the AWS Console.
    • Input vs. Output: The event object (input) vs. the return JSON (output).
  2. 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.
  3. 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.
  4. Automated Testing
    • Amazon Q Developer: Generating unit tests and test events using AI assistance.

Visual Anchors

The Event Lifecycle

Loading Diagram...

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).
  • Staging Variables: Key-value pairs defined in API Gateway stages.
    • Example: Using ${stageVariables.functionName} in a payload to dynamically call a v1 or v2 Lambda alias.
  • 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-Origin header.

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:

bash
sam local generate-event s3 put --bucket my-test-bucket --key image.jpg > event.json

Resulting JSON (event.json):

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:

json
{ "isBase64Encoded": false, "statusCode": 200, "headers": { "Content-Type": "application/json" }, "body": "{\"message\": \"Success\"}" }

[!IMPORTANT] Note that the body field must be a stringified JSON, not a raw JSON object.

Checkpoint Questions

  1. Which AWS SAM command is used to create a sample JSON payload for an Amazon SNS trigger?
  2. In an AWS_PROXY integration, what happens if the Lambda function returns a simple string like "Hello World" instead of a JSON object with a statusCode?
  3. True or False: Mock integrations in API Gateway require a Lambda function to be associated with the method.
  4. What is the benefit of using stageVariables in an API Gateway integration request payload?
Click to see answers
  1. sam local generate-event sns notification.
  2. API Gateway will return a 502 Bad Gateway error because it cannot parse the required structure.
  3. False. Mock integrations return responses defined within API Gateway itself.
  4. It allows you to dynamically route requests to different backend resources (like different Lambda aliases) based on the deployment stage (Dev, Test, Prod).

Ready to study AWS Certified Developer - Associate (DVA-C02)?

Practice tests, flashcards, and all study notes — free, no sign-up needed.

Start Studying — Free