1. Answers
  2. Expose Amazon EC2 Instances via Ngrok in C#

How Do I Expose Amazon EC2 Instances via Ngrok in C#?

Introduction

In this guide, you will learn how to set up an Amazon EC2 instance and expose it to the internet using ngrok. We will define all the necessary resources and configurations in a Pulumi program written in C#. Pulumi serves as the infrastructure as code tool, facilitating the deployment and management of cloud resources. The key services we will use include Amazon EC2 for the virtual machine and ngrok to create a secure tunnel to the instance.

Step-by-Step Guide

  1. Create an AWS EC2 Instance:

    • This will be the virtual machine where your application is hosted.
    • Use Pulumi to define and deploy this resource.
  2. Install ngrok:

    • On the EC2 instance, install ngrok to create a secure tunnel to localhost.
  3. Configure Security Groups:

    • Set up security groups to allow the necessary traffic such as HTTP and SSH.
  4. Retrieve the ngrok URL:

    • To access your application publicly, check the ngrok dashboard for the URL.

Here’s the complete code to achieve this setup:

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

const example = new aws.ec2.Instance("example", {
    ami: "ami-0c55b159cbfafe1f0",
    instanceType: aws.ec2.InstanceType.T2_Micro,
    userData: `#!/bin/bash
# Update and install necessary packages
yum update -y
amazon-linux-extras install epel -y
yum install -y wget unzip

# Download ngrok
wget https://bin.equinox.io/c/4VmDzA7iaHb/ngrok-stable-linux-amd64.zip
unzip ngrok-stable-linux-amd64.zip
mv ngrok /usr/local/bin/

# Authorize ngrok
ngrok authtoken <your-ngrok-authtoken>

# Create ngrok service
echo "[Unit]
Description=ngrok

[Service]
ExecStart=/usr/local/bin/ngrok http 80
Restart=always
User=ec2-user

[Install]
WantedBy=multi-user.target" > /etc/systemd/system/ngrok.service

# Start ngrok service
systemctl enable ngrok
systemctl start ngrok
`,
    tags: {
        Name: "ngrok-ec2-example",
    },
});
const ngrokSg = new aws.ec2.SecurityGroup("ngrok_sg", {
    name: "ngrok-sg",
    ingress: [
        {
            fromPort: 80,
            toPort: 80,
            protocol: "tcp",
            cidrBlocks: ["0.0.0.0/0"],
        },
        {
            fromPort: 22,
            toPort: 22,
            protocol: "tcp",
            cidrBlocks: ["0.0.0.0/0"],
        },
    ],
    egress: [{
        fromPort: 0,
        toPort: 0,
        protocol: "-1",
        cidrBlocks: ["0.0.0.0/0"],
    }],
    tags: {
        Name: "allow_ngrok",
    },
});
export const publicIp = example.publicIp;
export const ngrokUrl = "Check the ngrok dashboard for your URL: https://dashboard.ngrok.com/status/tunnels";

Conclusion

In this guide, we successfully created an AWS EC2 instance and installed ngrok to provide a secure public URL for our instance. The security group was configured to ensure necessary access, and we demonstrated how to retrieve the ngrok URL using the ngrok dashboard. This entire setup was achieved using Pulumi in a C# program context. By following these steps, you can efficiently expose your EC2 instances to the internet securely.

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