How do I build an AWS API Gateway Integration Response?
To create an AWS API Gateway Integration Response, you’ll need to set up several resources that define how your API Gateway interacts with backend services. This includes creating an API Gateway, defining methods, integrating those methods with backend services (like AWS Lambda), and then specifying the integration responses that map backend responses to the frontend.
Here’s a step-by-step guide to building this configuration with comments explaining the purpose of each resource.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
// Create the API Gateway
const example = new aws.apigateway.RestApi("example", {
name: "example_api",
description: "Example API Gateway",
});
// Create the Resource in the API Gateway
const exampleResource = new aws.apigateway.Resource("example_resource", {
restApi: example.id,
parentId: example.rootResourceId,
pathPart: "example",
});
// Create the HTTP Method for the Resource
const exampleMethod = new aws.apigateway.Method("example_method", {
restApi: example.id,
resourceId: exampleResource.id,
httpMethod: "GET",
authorization: "NONE",
});
// Integrate the Method with a Mock Integration
const exampleIntegration = new aws.apigateway.Integration("example_integration", {
restApi: example.id,
resourceId: exampleResource.id,
httpMethod: exampleMethod.httpMethod,
type: "MOCK",
});
// Define the Integration Response for the method
const exampleIntegrationResponse = new aws.apigateway.IntegrationResponse("example_integration_response", {
restApi: example.id,
resourceId: exampleResource.id,
httpMethod: exampleMethod.httpMethod,
statusCode: "200",
selectionPattern: "",
responseTemplates: {
"application/json": "{"message": "Hello from API Gateway!"}",
},
});
// Deploys the API
const exampleDeployment = new aws.apigateway.Deployment("example_deployment", {
restApi: example.id,
stageName: "test",
}, {
dependsOn: [exampleIntegrationResponse],
});
export const apiUrl = exampleDeployment.invokeUrl;
In this setup:
- The
aws_api_gateway_rest_api
resource creates the API Gateway. - The
aws_api_gateway_resource
defines a new resource within the API (e.g.,/example
). - The
aws_api_gateway_method
creates an HTTP method (e.g., GET) for the defined resource. - The
aws_api_gateway_integration
integrates the method with a backend service. Here, it’s using a mock integration as an example. - The
aws_api_gateway_integration_response
specifies how to map the backend response to the frontend request. It returns a JSON message for status code 200. - The
aws_api_gateway_deployment
deploys these configurations to a specified stage (test
).
The output provides the URL to access the deployed API.
In summary, we walked through the setup of building an API Gateway with integration responses, including creating API resources, defining methods, integrating with mock backends, and mapping responses.
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.