Skip to main content

Web server using Azure Virtual Machine with ComponentResource

An Azure example using Python to provision a webserver using ComponentResource

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 classic-azure-py-webserver-component
cd pulumi-examples/classic-azure-py-webserver-component

This example uses pulumi.ComponentResource as described here to create and deploy an Azure Virtual Machine and starts a HTTP server on it.

The use of pulumi.ComponentResource demonstrates how multiple low-level resources can be composed into a higher-level, reusable abstraction.

Prerequisites#

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

Deploying the example#

  1. Create a new stack:

    Terminal window
    pulumi stack init
  2. Set the Azure environment and subscription:

    Terminal window
    pulumi config set azure:environment public
    pulumi config set azure:subscriptionId <YOUR_SUBSCRIPTION_ID>
  3. 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. The password is a secret, so we ask Pulumi to encrypt the configuration:

    Terminal window
    pulumi config set username myusername
    pulumi config set --secret password Hunter2hunter2
  4. Install dependencies:

    Terminal window
    python3 -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
  5. Run pulumi up to preview and deploy the changes:

    Terminal window
    pulumi up
    Updating (dev):
    Type Name Status
    + pulumi:pulumi:Stack azure-py-webserver-component-dev created
    + ├─ custom:app:WebServer server created
    + │ ├─ azure:network:PublicIp server-ip created
    + │ ├─ azure:network:NetworkInterface server-nic created
    + │ └─ azure:compute:VirtualMachine server-vm created
    + └─ azure:core:ResourceGroup server created
    + └─ azure:network:VirtualNetwork server-network created
    + └─ azure:network:Subnet server-subnet created
    Outputs:
    public_ip: "13.64.196.146"
    Resources:
    + 8 created
    Duration: 2m9s
  6. Get the IP address of the newly-created instance from the stack’s outputs, and check that your server is running:

    Terminal window
    pulumi stack output public_ip
    curl http://$(pulumi stack output public_ip)
    13.64.196.146
    Hello, World!

Cleaning up#

Once you are done, destroy the stack and remove it:

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure as code platform for any cloud.