1. Answers
  2. Building an AWS API Gateway Integration Response

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:

  1. The aws_api_gateway_rest_api resource creates the API Gateway.
  2. The aws_api_gateway_resource defines a new resource within the API (e.g., /example).
  3. The aws_api_gateway_method creates an HTTP method (e.g., GET) for the defined resource.
  4. The aws_api_gateway_integration integrates the method with a backend service. Here, it’s using a mock integration as an example.
  5. 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.
  6. 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 up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up