How do I expose Amazon EC2 instances via ngrok in C#?
In this guide, we’ll set up an Amazon EC2 instance and expose it to the internet using ngrok. We’ll define all the necessary resources and configurations in a Pulumi program written in C#. The key services utilized include Amazon EC2 for the virtual machine and ngrok to create a secure tunnel to the instance.
Steps:
- Create an AWS EC2 Instance: This will be the virtual machine where your application is hosted.
- Install ngrok: On the EC2 instance to create a secure tunnel to localhost.
- Security Group Configuration: Set up security groups to allow the necessary traffic.
- Output the ngrok URL: Provide the URL to access your application publicly.
Here’s the complete code to achieve this:
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";
Key Points:
- AWS EC2 Instance: We used a t2.micro instance and an Amazon Linux 2 AMI.
- User Data Script: This script installs ngrok on the instance and sets up a service to run ngrok automatically.
- Security Group: We configured security groups to allow HTTP traffic on port 80 and SSH traffic on port 22.
- Ngrok URL Output: Directed to check the ngrok dashboard for the actual URL for secure tunneling.
Summary
We created an AWS EC2 instance, installed and configured ngrok to provide a secure public URL for our instance. The security group ensures necessary access, and we demonstrated how to find the ngrok URL using the ngrok dashboard.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.