Create an AWS API Gateway & Mock Responses |Video upload date:  · Duration: PT59S  · Language: EN

Configure API Gateway mock responses with mapping templates for quick testing and seamless Lambda integration without deploying backend code

Why mock responses are worth the tiny amount of effort

If you like shipping broken things loudly then skip this guide. If you prefer reduced panic and fewer angry Slack threads then configuring AWS API Gateway to return mock responses is one of the best tiny wins you can get. Mock responses make integration testing faster and let you iterate on client code without needing Lambda or an ECS service to be ready yet.

What this covers

  • Create a RESTful API resource in API Gateway
  • Set the method integration to Mock and define status codes
  • Use mapping templates to shape application slash json responses
  • Test with the console or curl and then switch to Lambda or HTTP backends

Step 1 Create the API resource

Open the API Gateway console and either create a new REST API or use an existing one. Add a resource and pick an HTTP method such as GET or POST. Name things clearly now to avoid debugging that looks like modern art later.

Step 2 Choose Mock integration and response codes

For the method integration select Mock as the integration type. In the integration response area declare the status codes you want to simulate. For example add 200 for success and 500 for a simulated server failure. API Gateway will return these status codes without ever touching your backend which speeds up feedback and lowers stress.

Step 3 Mapping templates that do the heavy lifting

Mapping templates let you shape the mock response body and populate headers so tests feel realistic. Set the response content type to application slash json and create a mapping template using the Velocity template language. Use expressions such as $input.path('$.message') to pull values from a simulated request body or from stage variables.

Example mapping template

#set($payload = $input.path('$.payload'))
{
  "status": "ok",
  "message": "$input.path('$.message')",
  "data": $payload
}

This template produces a JSON response and demonstrates how to echo back fields from a fake request. Replace the keys and structure to match your API contract so clients never get surprised when you flip the integration to Lambda later.

Step 4 Test it without the backend

Use the API Gateway test console for quick checks or hit the deployed stage endpoint with curl. A sample curl call might look like this

curl -X POST https://{api-id}.execute-api.{region}.amazonaws.com/{stage}/resource \
  -H "Content-Type: application/json" \
  -d '{"message":"hello world","payload":{"id":123}}'

Confirm the returned status code headers and JSON structure match what your clients expect. Rapid feedback here prevents downstream disasters and awkward release notes.

Step 5 Swap to Lambda or HTTP backend when ready

When your serverless function or ECS service is actually ready change the integration type to Lambda or HTTP. You can often reuse the mapping templates or adapt them so the contract stays stable and client code does not explode. That keeps versioning simple and reduces finger pointing.

Troubleshooting tips that save time

  • If your mapping template returns invalid JSON validate the template in the console and check for unescaped quotes
  • Use stage variables to toggle behavior between mock and real integration during testing
  • Keep mapping templates small and focused so they are easy to reuse when you switch to Lambda

Recap

Create an API resource choose Mock integration define response status codes add mapping templates test with the console or curl and then flip to Lambda or HTTP when ready. Mock Responses with mapping templates are a tiny investment that buys you fast integration testing and more predictable serverless deployments.

Keywords covered AWS API Gateway Mock Responses Mapping Templates Lambda RESTful ECS Serverless Integration Testing

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.