Skip to content

Commit 4650ca2

Browse files
Luke Hobanmikhailshilkov
andauthored
Add nextgen examples for Azure WebServer (pulumi#799)
* Add nextgen examples for Azure WebServer Currently in Python and TypeScript. Co-authored-by: Mikhail Shilkov <github@mikhail.io>
1 parent a1bf5d0 commit 4650ca2

10 files changed

Lines changed: 445 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ build/
66
node_modules/
77
*.pyc
88
.Python
9+
venv/
910
include/
1011
lib/
1112
yarn.lock
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: azure-py-webserver
2+
runtime: python
3+
description: Basic example of an Azure web server accessible over HTTP
4+
template:
5+
config:
6+
location:
7+
description: The Azure location to deploy to
8+
default: westus
9+
username:
10+
description: The username used to configure the Virtual Machine
11+
password:
12+
description: The password used to configure the Virtual Machine
13+
secret: true
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Web Server Using Azure Virtual Machine
4+
5+
This example deploys an Azure Virtual Machine and starts an HTTP server on it.
6+
7+
## Prerequisites
8+
9+
1. [Install Pulumi](https://www.pulumi.com/docs/get-started/install/)
10+
1. [Configure Pulumi for Azure](https://www.pulumi.com/docs/intro/cloud-providers/azure/setup/)
11+
1. [Configure Pulumi for Python](https://www.pulumi.com/docs/intro/languages/python/)
12+
13+
## Deploying and running the program
14+
15+
1. Set up a virtual Python environment and install dependencies
16+
17+
```bash
18+
$ python3 -m venv venv
19+
$ source venv/bin/activate
20+
$ pip install -r requirements.txt
21+
```
22+
23+
1. Create a new stack:
24+
25+
```bash
26+
$ pulumi stack init dev
27+
```
28+
29+
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.
30+
31+
```
32+
$ pulumi config set location westus # any valid Azure region will do
33+
$ pulumi config set username webmaster
34+
$ pulumi config set password <your-password> --secret
35+
```
36+
37+
Note that `--secret` ensures your password is encrypted safely.
38+
39+
40+
1. Run `pulumi up` to preview and deploy the changes:
41+
42+
```
43+
$ pulumi update
44+
Previewing update (azuredev):
45+
46+
Type Name Plan
47+
+ pulumi:pulumi:Stack azure-py-webserver-azuredev create
48+
+ ├─ azure:core:ResourceGroup server create
49+
+ ├─ azure:network:VirtualNetwork server-network create
50+
+ ├─ azure:network:PublicIp server-ip create
51+
+ ├─ azure:network:Subnet server-subnet create
52+
+ ├─ azure:network:NetworkInterface server-nic create
53+
+ └─ azure:compute:VirtualMachine server-vm create
54+
55+
Resources:
56+
+ 7 to create
57+
58+
Do you want to perform this update? yes
59+
Updating (azuredev):
60+
61+
Type Name Status
62+
+ pulumi:pulumi:Stack azure-py-webserver-azuredev created
63+
+ ├─ azure:core:ResourceGroup server created
64+
+ ├─ azure:network:VirtualNetwork server-network created
65+
+ ├─ azure:network:PublicIp server-ip created
66+
+ ├─ azure:network:Subnet server-subnet created
67+
+ ├─ azure:network:NetworkInterface server-nic created
68+
+ └─ azure:compute:VirtualMachine server-vm created
69+
70+
Outputs:
71+
public_ip: "137.117.15.111"
72+
73+
Resources:
74+
+ 7 created
75+
76+
Duration: 2m55s
77+
78+
Permalink: https://app.pulumi.com/swgillespie/azure-py-webserver/azuredev/updates/3
79+
```
80+
81+
1. Get the IP address of the newly-created instance from the stack's outputs:
82+
83+
```bash
84+
$ pulumi stack output public_ip
85+
137.117.15.111
86+
```
87+
88+
1. Check to see that your server is now running:
89+
90+
```
91+
$ curl http://$(pulumi stack output public_ip)
92+
Hello, World!
93+
```
94+
95+
1. Destroy the stack:
96+
97+
```bash
98+
▶ pulumi destroy --yes
99+
Previewing destroy (azuredev):
100+
101+
Type Name Plan
102+
- pulumi:pulumi:Stack azure-py-webserver-azuredev delete
103+
- ├─ azure:compute:VirtualMachine server-vm delete
104+
- ├─ azure:network:NetworkInterface server-nic delete
105+
- ├─ azure:network:Subnet server-subnet delete
106+
- ├─ azure:network:PublicIp server-ip delete
107+
- ├─ azure:network:VirtualNetwork server-network delete
108+
- └─ azure:core:ResourceGroup server delete
109+
110+
Resources:
111+
- 7 to delete
112+
113+
Destroying (azuredev):
114+
115+
Type Name Status
116+
- pulumi:pulumi:Stack azure-py-webserver-azuredev deleted
117+
- ├─ azure:compute:VirtualMachine server-vm deleted
118+
- ├─ azure:network:NetworkInterface server-nic deleted
119+
- ├─ azure:network:Subnet server-subnet deleted
120+
- ├─ azure:network:VirtualNetwork server-network deleted
121+
- ├─ azure:network:PublicIp server-ip deleted
122+
- └─ azure:core:ResourceGroup server deleted
123+
124+
Resources:
125+
- 7 deleted
126+
127+
Duration: 3m49s
128+
129+
Permalink: https://app.pulumi.com/swgillespie/azure-py-webserver/azuredev/updates/4
130+
```
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright 2016-2020, Pulumi Corporation. All rights reserved.
2+
3+
import base64
4+
from pulumi import Config, Output, export
5+
from pulumi_azure_nextgen.compute import latest as compute
6+
from pulumi_azure_nextgen.network import latest as network
7+
from pulumi_azure_nextgen.resources import latest as resources
8+
9+
config = Config()
10+
location = config.get("location") or "westus"
11+
username = config.require("username")
12+
password = config.require("password")
13+
14+
resource_group = resources.ResourceGroup("server",
15+
resource_group_name="server",
16+
location=location)
17+
18+
net = network.VirtualNetwork(
19+
"server-network",
20+
resource_group_name=resource_group.name,
21+
location=location,
22+
virtual_network_name="server-network",
23+
address_space=network.AddressSpaceArgs(
24+
address_prefixes=["10.0.0.0/16"],
25+
),
26+
subnets=[network.SubnetArgs(
27+
name="default",
28+
address_prefix="10.0.1.0/24",
29+
)])
30+
31+
public_ip = network.PublicIPAddress(
32+
"server-ip",
33+
resource_group_name=resource_group.name,
34+
location=location,
35+
public_ip_address_name="server-ip",
36+
public_ip_allocation_method="Dynamic")
37+
38+
network_iface = network.NetworkInterface(
39+
"server-nic",
40+
resource_group_name=resource_group.name,
41+
location=resource_group.location,
42+
network_interface_name="server-nic",
43+
ip_configurations=[network.NetworkInterfaceIPConfigurationArgs(
44+
name="webserveripcfg",
45+
subnet=network.SubnetArgs(id=net.subnets[0].id),
46+
private_ip_allocation_method="Dynamic",
47+
public_ip_address=network.PublicIPAddressArgs(id=public_ip.id),
48+
)])
49+
50+
init_script = """#!/bin/bash
51+
52+
echo "Hello, World!" > index.html
53+
nohup python -m SimpleHTTPServer 80 &"""
54+
55+
vm = compute.VirtualMachine(
56+
"server-vm",
57+
resource_group_name=resource_group.name,
58+
location=location,
59+
vm_name="server-vm",
60+
network_profile=compute.NetworkProfileArgs(
61+
network_interfaces=[
62+
compute.NetworkInterfaceReferenceArgs(id=network_iface.id),
63+
],
64+
),
65+
hardware_profile=compute.HardwareProfileArgs(
66+
vm_size="Standard_A0",
67+
),
68+
os_profile=compute.OSProfileArgs(
69+
computer_name="hostname",
70+
admin_username=username,
71+
admin_password=password,
72+
custom_data=base64.b64encode(init_script.encode("ascii")).decode("ascii"),
73+
linux_configuration=compute.LinuxConfigurationArgs(
74+
disable_password_authentication=False,
75+
),
76+
),
77+
storage_profile=compute.StorageProfileArgs(
78+
os_disk=compute.OSDiskArgs(
79+
create_option="FromImage",
80+
name="myosdisk1",
81+
),
82+
image_reference=compute.ImageReferenceArgs(
83+
publisher="canonical",
84+
offer="UbuntuServer",
85+
sku="16.04-LTS",
86+
version="latest",
87+
),
88+
))
89+
90+
combined_output = Output.all(vm.id, public_ip.name, resource_group.name)
91+
public_ip_addr = combined_output.apply(
92+
lambda lst: network.get_public_ip_address(
93+
public_ip_address_name=lst[1],
94+
resource_group_name=lst[2]))
95+
export("public_ip", public_ip_addr.ip_address)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pulumi>=2.0.0,<3.0.0
2+
pulumi-azure-nextgen>=0.1.0
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
name: azure-nextgen-ts-webserver
2+
runtime: nodejs
3+
description: Basic example of an Azure web server accessible over HTTP
4+
template:
5+
config:
6+
location:
7+
description: The Azure location to deploy to
8+
default: westus
9+
username:
10+
description: The username used to configure the Virtual Machine
11+
password:
12+
description: The password used to configure the Virtual Machine
13+
secret: true
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![Deploy](https://get.pulumi.com/new/button.svg)](https://app.pulumi.com/new)
2+
3+
# Web Server Using Azure Virtual Machine
4+
5+
This example provisions a Linux web server in an Azure Virtual Machine and gives it a public IP address.
6+
7+
## Prerequisites
8+
9+
- [Node.js](https://nodejs.org/en/download/)
10+
- [Download and install the Pulumi CLI](https://www.pulumi.com/docs/get-started/install/)
11+
- [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)
12+
13+
## Running the App
14+
15+
1. Create a new stack:
16+
17+
```
18+
$ pulumi stack init dev
19+
```
20+
21+
1. Configure the app deployment. The username and password here will be used to configure the Virtual Machine. The
22+
password must adhere to the [Azure restrictions on VM passwords](
23+
https://docs.microsoft.com/en-us/azure/virtual-machines/windows/faq#what-are-the-password-requirements-when-creating-a-vm).
24+
25+
```
26+
$ pulumi config set location westus # any valid Azure region will do
27+
$ pulumi config set username webmaster
28+
$ pulumi config set password <your-password> --secret
29+
```
30+
31+
Note that `--secret` ensures your password is encrypted safely.
32+
33+
1. Login to Azure CLI (you will be prompted to do this during deployment if you forget this step):
34+
35+
```
36+
$ az login
37+
```
38+
39+
1. Restore NPM dependencies:
40+
41+
```
42+
$ npm install
43+
```
44+
45+
1. Run `pulumi up` to preview and deploy changes:
46+
47+
```
48+
$ pulumi up
49+
Previewing changes:
50+
...
51+
52+
Performing changes:
53+
...
54+
info: 7 changes performed:
55+
+ 7 resources created
56+
Update duration: 2m38s
57+
```
58+
59+
1. Check the IP address:
60+
61+
```
62+
$ pulumi stack output ipAddress
63+
40.112.181.239
64+
```

0 commit comments

Comments
 (0)