How do I map API Gateway responses to HTTP status codes?
In this guide, we will demonstrate how to map API Gateway responses to specific HTTP status codes using Pulumi. This allows you to define custom responses for different scenarios, such as errors or successful operations. We will create an API Gateway, define methods, and map responses to HTTP status codes.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create an API Gateway
const api = new aws.apigateway.RestApi("myApi", {
description: "API for demonstrating response mapping",
});
// Create a resource
const resource = new aws.apigateway.Resource("myResource", {
restApi: api.id,
parentId: api.rootResourceId,
pathPart: "example",
});
// Create a method for the resource
const method = new aws.apigateway.Method("myMethod", {
restApi: api.id,
resourceId: resource.id,
httpMethod: "GET",
authorization: "NONE",
});
// Create a method response for HTTP 200 status
const methodResponse200 = new aws.apigateway.MethodResponse("methodResponse200", {
restApi: api.id,
resourceId: resource.id,
httpMethod: method.httpMethod,
statusCode: "200",
responseModels: {
"application/json": "Empty",
},
});
// Create a method response for HTTP 400 status
const methodResponse400 = new aws.apigateway.MethodResponse("methodResponse400", {
restApi: api.id,
resourceId: resource.id,
httpMethod: method.httpMethod,
statusCode: "400",
responseModels: {
"application/json": "Error",
},
});
// Create an integration for the method
const integration = new aws.apigateway.Integration("myIntegration", {
restApi: api.id,
resourceId: resource.id,
httpMethod: method.httpMethod,
type: "MOCK",
requestTemplates: {
"application/json": `{
"statusCode": 200
}`,
},
});
// Create an integration response for HTTP 200 status
const integrationResponse200 = new aws.apigateway.IntegrationResponse("integrationResponse200", {
restApi: api.id,
resourceId: resource.id,
httpMethod: method.httpMethod,
statusCode: methodResponse200.statusCode,
responseTemplates: {
"application/json": `{
"message": "Success"
}`,
},
});
// Create an integration response for HTTP 400 status
const integrationResponse400 = new aws.apigateway.IntegrationResponse("integrationResponse400", {
restApi: api.id,
resourceId: resource.id,
httpMethod: method.httpMethod,
statusCode: methodResponse400.statusCode,
selectionPattern: ".*\\[400\\].*",
responseTemplates: {
"application/json": `{
"error": "Bad Request"
}`,
},
});
// Deploy the API
const deployment = new aws.apigateway.Deployment("myDeployment", {
restApi: api.id,
stageName: "dev",
}, { dependsOn: [integration] });
// Export the API endpoint
export const apiEndpoint = deployment.invokeUrl;
Key Points
- Defined an API Gateway with a resource and method.
- Mapped method responses to specific HTTP status codes (200 and 400).
- Created integration responses to map backend responses to API Gateway responses.
- Deployed the API and exported the endpoint URL.
Summary
In this guide, we created an API Gateway with a resource and a method, defined method responses for specific HTTP status codes, and mapped integration responses to these status codes using Pulumi. This setup allows you to handle different response scenarios effectively in your API.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.