1. Answers
  2. Integrating AWS API Gateway with Elastic Beanstalk

How do I integrate AWS API Gateway with Elastic Beanstalk?

In this guide, we will set up AWS API Gateway to route HTTP requests to an Elastic Beanstalk application. This setup typically involves creating an API Gateway REST API, configuring the API Gateway with appropriate resources and methods, and finally integrating it with the Elastic Beanstalk environment URL.

Steps Overview:

  1. Elastic Beanstalk Application & Environment: Define and deploy your Elastic Beanstalk application and environment.
  2. API Gateway REST API: Create an API Gateway REST API to accept HTTP requests.
  3. API Gateway Resource & Method: Define the resources and methods on API Gateway.
  4. Integration with Beanstalk: Link API Gateway methods to the Elastic Beanstalk application.

Below is the complete code example for setting up the described infrastructure.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Step 1: Define the Elastic Beanstalk Application and Environment
const myApp = new aws.elasticbeanstalk.Application("my_app", {
    name: "my-beanstalk-app",
    description: "My Elastic Beanstalk Application",
});
const myEnv = new aws.elasticbeanstalk.Environment("my_env", {
    name: "my-env",
    application: myApp.name,
    solutionStackName: "64bit Amazon Linux 2 v3.1.2 running Node.js 14",
    settings: [
        {
            namespace: "aws:autoscaling:launchconfiguration",
            name: "InstanceType",
            value: "t2.micro",
        },
        {
            namespace: "aws:elasticbeanstalk:environment",
            name: "EnvironmentType",
            value: "SingleInstance",
        },
    ],
});
// Step 2: Create the API Gateway REST API
const myApi = new aws.apigateway.RestApi("my_api", {
    name: "MyAPIGateway",
    description: "API Gateway linked to Elastic Beanstalk",
});
// Step 3: Define the API Gateway Resource & Method
const myResource = new aws.apigateway.Resource("my_resource", {
    restApi: myApi.id,
    parentId: myApi.rootResourceId,
    pathPart: "{proxy+}",
});
const proxyMethod = new aws.apigateway.Method("proxy_method", {
    restApi: myApi.id,
    resourceId: myResource.id,
    httpMethod: "ANY",
    authorization: "NONE",
});
// Step 4: Integrate API Gateway with Elastic Beanstalk
const myIntegration = new aws.apigateway.Integration("my_integration", {
    restApi: myApi.id,
    resourceId: myResource.id,
    httpMethod: proxyMethod.httpMethod,
    integrationHttpMethod: "ANY",
    type: "HTTP_PROXY",
    uri: myEnv.endpointUrl,
});
const myDeployment = new aws.apigateway.Deployment("my_deployment", {
    restApi: myApi.id,
    stageName: "prod",
}, {
    dependsOn: [myIntegration],
});
export const apiGatewayUrl = myDeployment.invokeUrl;
export const elasticBeanstalkUrl = myEnv.endpointUrl;

Summary

In this example, we created an Elastic Beanstalk application and environment, an API Gateway REST API, and the necessary resources and methods to route traffic through API Gateway to the Elastic Beanstalk application. Finally, we integrated API Gateway with the Elastic Beanstalk environment to complete the setup. This way, incoming HTTP requests to the API Gateway will be proxied to the Elastic Beanstalk application.

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