1. Answers
  2. How do I use ngrok with Amazon EC2?

How do I use ngrok with Amazon EC2?

To use ngrok with Amazon EC2, you need to set up an EC2 instance and install ngrok on it. Ngrok is a tool that allows you to expose a local server to the internet securely. This can be useful for testing webhooks, running demos, or accessing your development environment remotely.

Here are the steps to accomplish this with Pulumi:

  1. Create an EC2 instance: Set up an Amazon EC2 instance with the desired specifications.
  2. Install ngrok: Use a startup script to install ngrok on the EC2 instance.
  3. Expose a local service: Use ngrok to expose a local service running on the EC2 instance to the internet.

We’ll use Pulumi to define this infrastructure as code. Below is the detailed explanation followed by the TypeScript code to achieve this.

Explanation

  1. Provision an EC2 Instance:

    • We use the aws.ec2.Instance resource to create an EC2 instance.
    • Specify the instance type, AMI (Amazon Machine Image), and other necessary configurations.
    • Use a user data script to install ngrok when the instance starts.
  2. Security Group:

    • Create a security group to allow necessary inbound and outbound traffic.
  3. Outputs:

    • Export the public IP of the EC2 instance so you can SSH into it if needed.

Pulumi Program

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

// Create a security group to allow SSH and HTTP access
const securityGroup = new aws.ec2.SecurityGroup("ngrok-sg", {
    description: "Allow SSH and HTTP access",
    ingress: [
        { protocol: "tcp", fromPort: 22, toPort: 22, cidrBlocks: ["0.0.0.0/0"] }, // SSH access
        { protocol: "tcp", fromPort: 80, toPort: 80, cidrBlocks: ["0.0.0.0/0"] }, // HTTP access
    ],
    egress: [
        { protocol: "-1", fromPort: 0, toPort: 0, cidrBlocks: ["0.0.0.0/0"] }, // Allow all outbound traffic
    ],
});

// Define the user data script to install ngrok
const userData = `#!/bin/bash
sudo apt-get update
sudo apt-get install -y unzip
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc > /dev/null
echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list
sudo apt-get update
sudo apt-get install -y ngrok
ngrok http 80
`;

// Create an EC2 instance
const server = new aws.ec2.Instance("ngrok-server", {
    instanceType: "t2.micro", // Choose an instance type
    ami: "ami-0c55b159cbfafe1f0", // Ubuntu AMI
    securityGroups: [securityGroup.name], // Associate the security group
    userData: userData, // Run the user data script on startup
    tags: {
        Name: "ngrok-server",
    },
});

// Export the public IP of the instance
export const publicIp = server.publicIp;
export const publicDns = server.publicDns;

Explanation of the Code

  1. Security Group:

    • We create a security group to allow SSH (port 22) and HTTP (port 80) access.
    • This ensures that we can connect to the instance via SSH and that ngrok can expose an HTTP service.
  2. User Data Script:

    • The script updates the package list, installs unzip, and installs ngrok.
    • After installation, it starts ngrok to expose HTTP service on port 80.
  3. EC2 Instance:

    • We create an EC2 instance using the aws.ec2.Instance resource.
    • The instance uses a predefined Ubuntu AMI, is associated with the security group, and runs the user data script on startup.
  4. Outputs:

    • We export the public IP and DNS of the EC2 instance so you can easily access it.

By running this Pulumi program, you’ll have an EC2 instance with ngrok installed and running, exposing an HTTP service on port 80 to the internet.

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