Skip to main content

Load balancer and web server using Azure Load Balancer and Virtual Machine

Basic example of an Azure Load Balancer fronting an Azure VM accessible over HTTP

This example lives in the pulumi/examples repository. Check out just this directory to use it:

Get started with this example
git clone --filter=blob:none --sparse https://github.com/pulumi/examples pulumi-examples
git -C pulumi-examples sparse-checkout set azure-py-loadbalancer-vm
cd pulumi-examples/azure-py-loadbalancer-vm

This example deploys an Azure Load Balancer fronting an Azure Virtual Machine and starts an HTTP server on the VM.

Prerequisites#

  1. Install Pulumi
  2. Configure Azure credentials
  3. Install Python

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init dev
  2. Set the required configuration for this example. This example requires you to supply a username and password to the virtual machine that we are going to create. Note that --secret ensures your password is encrypted safely.

    Terminal window
    pulumi config set azure-native:location westus # any valid Azure region will do
    pulumi config set username webmaster
    pulumi config set password --secret <your-password>
  3. Install dependencies:

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  4. Deploy the stack:

    Terminal window
    pulumi up
    Updating (azuredev):
    Type Name Status
    + pulumi:pulumi:Stack azure-py-loadbalancer-vm-azuredev created (14s)
    + ├─ random:index:RandomString azure-py-loadbalancer-vm-lb-domain-label created (0.28s)
    + ├─ azure-native:resources:ResourceGroup azure-py-loadbalancer-vm-resource-group created (0.90s)
    + ├─ azure-native:network:VirtualNetwork azure-py-loadbalancer-vm-network created (4s)
    + ├─ azure-native:network:NetworkSecurityGroup azure-py-loadbalancer-vm-security-group created (2s)
    + ├─ azure-native:network:PublicIPAddress azure-py-loadbalancer-vm-lb-public-ip created (5s)
    + ├─ azure-native:network:LoadBalancer azure-py-loadbalancer-vm-lb created (1s)
    + ├─ azure-native:network:NetworkInterface azure-py-loadbalancer-vm-network-interface created (5s)
    + └─ azure-native:compute:VirtualMachine azure-py-loadbalancer-vm created (87s)
    Outputs:
    fqdn : "http://azure-py-loadbalancer-vm-n3jtn905.westus2.cloudapp.azure.com"
    lb-ip: "20.3.225.29"
    Resources:
    + 9 created
    Duration: 1m51s
  5. Get the IP address of the newly-created Load Balancer from the stack’s outputs:

    Terminal window
    pulumi stack output lb-ip
  6. Get the FQDN of the newly-created Load Balancer from the stack’s outputs:

    Terminal window
    pulumi stack output fqdn
  7. Check to see that your server is now running:

    Terminal window
    curl "http://$(pulumi stack output lb-ip)"
    curl "$(pulumi stack output fqdn)"
    Hello, World!

Cleaning up#

Once you are finished, you can destroy your stack and remove it to avoid incurring any additional cost:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.