1. Answers
  2. Setting up ngrok on Azure Virtual Machines

How do I use ngrok with Azure Virtual Machines?

In this guide, we will set up an Azure Virtual Machine and install ngrok on it using Pulumi. ngrok is a tool that allows you to expose a local server to the internet securely. This setup will help you create a development environment where you can test webhooks and other services that require public URLs.

import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure-native";
import * as random from "@pulumi/random";

// Create a resource group
const resourceGroup = new azure.resources.ResourceGroup("resourceGroup", {
    location: "WestUS",
});

// Generate a random password for the VM admin
const adminPassword = new random.RandomPassword("adminPassword", {
    length: 16,
    special: true,
}).result;

// Create a virtual network
const virtualNetwork = new azure.network.VirtualNetwork("virtualNetwork", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    addressSpace: { addressPrefixes: ["10.0.0.0/16"] },
});

// Create a subnet
const subnet = new azure.network.Subnet("subnet", {
    resourceGroupName: resourceGroup.name,
    virtualNetworkName: virtualNetwork.name,
    addressPrefix: "10.0.1.0/24",
});

// Create a public IP address
const publicIp = new azure.network.PublicIPAddress("publicIp", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    publicIPAllocationMethod: "Dynamic",
});

// Create a network interface
const networkInterface = new azure.network.NetworkInterface("networkInterface", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    ipConfigurations: [{
        name: "ipConfig",
        subnet: { id: subnet.id },
        privateIPAllocationMethod: "Dynamic",
        publicIPAddress: { id: publicIp.id },
    }],
});

// Create a virtual machine
const vm = new azure.compute.VirtualMachine("vm", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    networkProfile: { networkInterfaces: [{ id: networkInterface.id }] },
    hardwareProfile: { vmSize: "Standard_B1s" },
    osProfile: {
        computerName: "hostname",
        adminUsername: "adminuser",
        adminPassword: adminPassword,
        linuxConfiguration: {
            disablePasswordAuthentication: false,
        },
    },
    storageProfile: {
        imageReference: {
            publisher: "canonical",
            offer: "UbuntuServer",
            sku: "18.04-LTS",
            version: "latest",
        },
        osDisk: {
            createOption: "FromImage",
        },
    },
});

// Script to install ngrok on the VM
const installNgrokScript = `#!/bin/bash
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 ngrok
`;

// Run the script on the VM
const runCommand = new azure.compute.VirtualMachineRunCommandByVirtualMachine("runCommand", {
    resourceGroupName: resourceGroup.name,
    vmName: vm.name,
    location: resourceGroup.location,
    source: {
        script: installNgrokScript,
    },
    runCommandName: "InstallNgrok",
});

Key Points

  • We created an Azure Virtual Machine using Pulumi.
  • We installed ngrok on the VM by running a script using the VirtualMachineRunCommandByVirtualMachine resource.

Summary

In this guide, we set up an Azure Virtual Machine and installed ngrok on it using Pulumi. This setup allows you to expose local services to the internet securely, which is useful for testing webhooks and other services requiring public URLs.

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