1. Answers
  2. Using aws ec2 with apigateway

How do I use AWS EC2 with API Gateway?

To create an AWS EC2 instance and expose it through an API Gateway using Pulumi, you will need to follow these steps:

  1. Create an EC2 instance: This instance will host your application or service.
  2. Create an API Gateway: This will provide a RESTful API interface to your EC2 instance.
  3. Integrate API Gateway with the EC2 instance: This involves creating an API Gateway resource and method, and then linking it to the EC2 instance.

Here’s a detailed explanation followed by a Pulumi program written in TypeScript to achieve this:

Explanation

  1. EC2 Instance:

    • We will create a security group to allow HTTP traffic.
    • We will launch an EC2 instance using a specified AMI and instance type.
  2. API Gateway:

    • We will create a REST API.
    • We will create a resource and method for the API.
    • We will create an integration to link the API method to the EC2 instance.
    • We will deploy the API.

Pulumi Program

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

// Create an EC2 security group to allow HTTP traffic
const ec2SecurityGroup = new aws.ec2.SecurityGroup("web-secgrp", {
    ingress: [
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] },
    ],
});

// Launch an EC2 instance
const ec2Instance = new aws.ec2.Instance("web-server", {
    instanceType: "t2.micro",
    ami: "ami-0c55b159cbfafe1f0", // Amazon Linux 2 AMI
    securityGroups: [ec2SecurityGroup.name],
    userData: `#!/bin/bash
              echo "Hello, World" > index.html
              nohup python -m SimpleHTTPServer 80 &`,
});

// Create an API Gateway REST API
const api = new aws.apigateway.RestApi("myapi", {
    description: "API for my web server",
});

// Create a resource in the API
const resource = new aws.apigateway.Resource("myresource", {
    restApi: api.id,
    parentId: api.rootResourceId,
    pathPart: "{proxy+}",
});

// Create a method for the resource
const method = new aws.apigateway.Method("mymethod", {
    restApi: api.id,
    resourceId: resource.id,
    httpMethod: "ANY",
    authorization: "NONE",
});

// Create an integration to link the API method to the EC2 instance
const integration = new aws.apigateway.Integration("myintegration", {
    restApi: api.id,
    resourceId: resource.id,
    httpMethod: method.httpMethod,
    type: "HTTP_PROXY",
    uri: pulumi.interpolate`http://${ec2Instance.publicDns}`,
    integrationHttpMethod: "ANY",
});

// Deploy the API
const deployment = new aws.apigateway.Deployment("mydeployment", {
    restApi: api.id,
    stageName: "prod",
}, { dependsOn: [method] });

// Export the URL of the API
export const apiUrl = deployment.invokeUrl;

Explanation of the Code

  1. Security Group: We create a security group web-secgrp to allow incoming HTTP traffic on port 80.
  2. EC2 Instance: We launch an EC2 instance web-server using the Amazon Linux 2 AMI. The instance runs a simple HTTP server that serves “Hello, World”.
  3. API Gateway:
    • We create a REST API myapi.
    • We create a resource myresource with a path parameter {proxy+} to act as a catch-all for all paths.
    • We create a method mymethod for the resource, allowing any HTTP method (ANY) and no authorization.
    • We create an integration myintegration that links the method to the EC2 instance using the instance’s public DNS.
  4. Deployment: We deploy the API to a stage named prod.
  5. Export: We export the URL of the deployed API.

This program sets up an EC2 instance and exposes it via an API Gateway, allowing you to access the EC2 instance through the API Gateway URL.

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