1. Answers
  2. Building an AWS API Gateway with Pulumi

How do I build an AWS API Gateway with Pulumi?

In this guide, we will build a REST API using AWS API Gateway with Pulumi. We will define a REST API, create a resource and method, and deploy it to a stage. This setup will include creating an API Gateway, defining a resource, associating a method with this resource, and deploying the API.

Key Points

  • Create an API Gateway REST API.
  • Define a resource and a method for the API.
  • Deploy the API to a stage.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create an API Gateway REST API
const api = new aws.apigateway.RestApi("myApi", {
    description: "This is my API for demonstration purposes",
    // OpenAPI specification for the API
    body: `
    {
        "swagger": "2.0",
        "info": {
            "title": "myApi",
            "description": "This is my API for demonstration purposes",
            "version": "1.0"
        },
        "paths": {
            "/hello": {
                "get": {
                    "responses": {
                        "200": {
                            "description": "200 response",
                            "schema": {
                                "type": "string"
                            }
                        }
                    },
                    "x-amazon-apigateway-integration": {
                        "type": "mock",
                        "requestTemplates": {
                            "application/json": "{\"statusCode\": 200}"
                        },
                        "responses": {
                            "default": {
                                "statusCode": "200",
                                "responseTemplates": {
                                    "application/json": "\"Hello, World\""
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    `
});

// Create a deployment of the API
const deployment = new aws.apigateway.Deployment("myDeployment", {
    restApi: api.id,
    stageName: "dev",
});

// Export the URL of the API
export const apiUrl = pulumi.interpolate`${deployment.invokeUrl}hello`;

Summary

In this example, we created an AWS API Gateway REST API using Pulumi. We defined the API with a single resource (/hello) and a GET method that returns a “Hello, World” message. We then deployed the API to a stage named dev and exported the URL of the deployed API. This setup allows you to access the API endpoint and receive the predefined response.

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