Skip to content

Commit

Permalink
Add nextgen examples for Azure WebServer (pulumi#799)
Browse files Browse the repository at this point in the history
* Add nextgen examples for Azure WebServer

Currently in Python and TypeScript.

Co-authored-by: Mikhail Shilkov <[email protected]>
  • Loading branch information
lukehoban and mikhailshilkov committed Sep 21, 2020
1 parent a1bf5d0 commit 4650ca2
Show file tree
Hide file tree
Showing 10 changed files with 445 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ build/
node_modules/
*.pyc
.Python
venv/
include/
lib/
yarn.lock
Expand Down
13 changes: 13 additions & 0 deletions azure-nextgen-py-webserver/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: azure-py-webserver
runtime: python
description: Basic example of an Azure web server accessible over HTTP
template:
config:
location:
description: The Azure location to deploy to
default: westus
username:
description: The username used to configure the Virtual Machine
password:
description: The password used to configure the Virtual Machine
secret: true
130 changes: 130 additions & 0 deletions azure-nextgen-py-webserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Web Server Using Azure Virtual Machine

This example deploys an Azure Virtual Machine and starts an HTTP server on it.

## Prerequisites

1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
1. [Configure Pulumi for Azure](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/)
1. [Configure Pulumi for Python](https://www.pulumi.com/docs/intro/languages/python/)

## Deploying and running the program

1. Set up a virtual Python environment and install dependencies

```bash
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install -r requirements.txt
```

1. Create a new stack:

```bash
$ pulumi stack init dev
```

1. 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.

```
$ pulumi config set location westus # any valid Azure region will do
$ pulumi config set username webmaster
$ pulumi config set password <your-password> --secret
```

Note that `--secret` ensures your password is encrypted safely.


1. Run `pulumi up` to preview and deploy the changes:

```
$ pulumi update
Previewing update (azuredev):
Type Name Plan
+ pulumi:pulumi:Stack azure-py-webserver-azuredev create
+ ├─ azure:core:ResourceGroup server create
+ ├─ azure:network:VirtualNetwork server-network create
+ ├─ azure:network:PublicIp server-ip create
+ ├─ azure:network:Subnet server-subnet create
+ ├─ azure:network:NetworkInterface server-nic create
+ └─ azure:compute:VirtualMachine server-vm create
Resources:
+ 7 to create
Do you want to perform this update? yes
Updating (azuredev):
Type Name Status
+ pulumi:pulumi:Stack azure-py-webserver-azuredev created
+ ├─ azure:core:ResourceGroup server created
+ ├─ azure:network:VirtualNetwork server-network created
+ ├─ azure:network:PublicIp server-ip created
+ ├─ azure:network:Subnet server-subnet created
+ ├─ azure:network:NetworkInterface server-nic created
+ └─ azure:compute:VirtualMachine server-vm created
Outputs:
public_ip: "137.117.15.111"
Resources:
+ 7 created
Duration: 2m55s
Permalink: https://app.pulumi.com/swgillespie/azure-py-webserver/azuredev/updates/3
```

1. Get the IP address of the newly-created instance from the stack's outputs:

```bash
$ pulumi stack output public_ip
137.117.15.111
```

1. Check to see that your server is now running:

```
$ curl http:https://$(pulumi stack output public_ip)
Hello, World!
```

1. Destroy the stack:

```bash
▶ pulumi destroy --yes
Previewing destroy (azuredev):

Type Name Plan
- pulumi:pulumi:Stack azure-py-webserver-azuredev delete
- ├─ azure:compute:VirtualMachine server-vm delete
- ├─ azure:network:NetworkInterface server-nic delete
- ├─ azure:network:Subnet server-subnet delete
- ├─ azure:network:PublicIp server-ip delete
- ├─ azure:network:VirtualNetwork server-network delete
- └─ azure:core:ResourceGroup server delete

Resources:
- 7 to delete

Destroying (azuredev):

Type Name Status
- pulumi:pulumi:Stack azure-py-webserver-azuredev deleted
- ├─ azure:compute:VirtualMachine server-vm deleted
- ├─ azure:network:NetworkInterface server-nic deleted
- ├─ azure:network:Subnet server-subnet deleted
- ├─ azure:network:VirtualNetwork server-network deleted
- ├─ azure:network:PublicIp server-ip deleted
- └─ azure:core:ResourceGroup server deleted

Resources:
- 7 deleted

Duration: 3m49s

Permalink: https://app.pulumi.com/swgillespie/azure-py-webserver/azuredev/updates/4
```
95 changes: 95 additions & 0 deletions azure-nextgen-py-webserver/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# Copyright 2016-2020, Pulumi Corporation. All rights reserved.

import base64
from pulumi import Config, Output, export
from pulumi_azure_nextgen.compute import latest as compute
from pulumi_azure_nextgen.network import latest as network
from pulumi_azure_nextgen.resources import latest as resources

config = Config()
location = config.get("location") or "westus"
username = config.require("username")
password = config.require("password")

resource_group = resources.ResourceGroup("server",
resource_group_name="server",
location=location)

net = network.VirtualNetwork(
"server-network",
resource_group_name=resource_group.name,
location=location,
virtual_network_name="server-network",
address_space=network.AddressSpaceArgs(
address_prefixes=["10.0.0.0/16"],
),
subnets=[network.SubnetArgs(
name="default",
address_prefix="10.0.1.0/24",
)])

public_ip = network.PublicIPAddress(
"server-ip",
resource_group_name=resource_group.name,
location=location,
public_ip_address_name="server-ip",
public_ip_allocation_method="Dynamic")

network_iface = network.NetworkInterface(
"server-nic",
resource_group_name=resource_group.name,
location=resource_group.location,
network_interface_name="server-nic",
ip_configurations=[network.NetworkInterfaceIPConfigurationArgs(
name="webserveripcfg",
subnet=network.SubnetArgs(id=net.subnets[0].id),
private_ip_allocation_method="Dynamic",
public_ip_address=network.PublicIPAddressArgs(id=public_ip.id),
)])

init_script = """#!/bin/bash
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &"""

vm = compute.VirtualMachine(
"server-vm",
resource_group_name=resource_group.name,
location=location,
vm_name="server-vm",
network_profile=compute.NetworkProfileArgs(
network_interfaces=[
compute.NetworkInterfaceReferenceArgs(id=network_iface.id),
],
),
hardware_profile=compute.HardwareProfileArgs(
vm_size="Standard_A0",
),
os_profile=compute.OSProfileArgs(
computer_name="hostname",
admin_username=username,
admin_password=password,
custom_data=base64.b64encode(init_script.encode("ascii")).decode("ascii"),
linux_configuration=compute.LinuxConfigurationArgs(
disable_password_authentication=False,
),
),
storage_profile=compute.StorageProfileArgs(
os_disk=compute.OSDiskArgs(
create_option="FromImage",
name="myosdisk1",
),
image_reference=compute.ImageReferenceArgs(
publisher="canonical",
offer="UbuntuServer",
sku="16.04-LTS",
version="latest",
),
))

combined_output = Output.all(vm.id, public_ip.name, resource_group.name)
public_ip_addr = combined_output.apply(
lambda lst: network.get_public_ip_address(
public_ip_address_name=lst[1],
resource_group_name=lst[2]))
export("public_ip", public_ip_addr.ip_address)
2 changes: 2 additions & 0 deletions azure-nextgen-py-webserver/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pulumi>=2.0.0,<3.0.0
pulumi-azure-nextgen>=0.1.0
13 changes: 13 additions & 0 deletions azure-nextgen-ts-webserver/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: azure-nextgen-ts-webserver
runtime: nodejs
description: Basic example of an Azure web server accessible over HTTP
template:
config:
location:
description: The Azure location to deploy to
default: westus
username:
description: The username used to configure the Virtual Machine
password:
description: The password used to configure the Virtual Machine
secret: true
64 changes: 64 additions & 0 deletions azure-nextgen-ts-webserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)

# Web Server Using Azure Virtual Machine

This example provisions a Linux web server in an Azure Virtual Machine and gives it a public IP address.

## Prerequisites

- [Node.js](https://nodejs.org/en/download/)
- [Download and install the Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
- [Connect Pulumi with your Azure account](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/) (if your `az` CLI is configured, no further changes are required)

## Running the App

1. Create a new stack:

```
$ pulumi stack init dev
```

1. Configure the app deployment. The username and password here will be used to configure the Virtual Machine. The
password must adhere to the [Azure restrictions on VM passwords](
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/faq#what-are-the-password-requirements-when-creating-a-vm).

```
$ pulumi config set location westus # any valid Azure region will do
$ pulumi config set username webmaster
$ pulumi config set password <your-password> --secret
```

Note that `--secret` ensures your password is encrypted safely.

1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step):

```
$ az login
```

1. Restore NPM dependencies:

```
$ npm install
```

1. Run `pulumi up` to preview and deploy changes:

```
$ pulumi up
Previewing changes:
...
Performing changes:
...
info: 7 changes performed:
+ 7 resources created
Update duration: 2m38s
```

1. Check the IP address:

```
$ pulumi stack output ipAddress
40.112.181.239
```
Loading

0 comments on commit 4650ca2

Please sign in to comment.