-
Notifications
You must be signed in to change notification settings - Fork 880
/
index.ts
84 lines (74 loc) · 2.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.
import * as pulumi from "@pulumi/pulumi";
import * as compute from "@pulumi/azure-native/compute";
import * as network from "@pulumi/azure-native/network";
import * as resources from "@pulumi/azure-native/resources";
// Get the desired username and password for our VM.
const config = new pulumi.Config();
const username = config.require("username");
const password = config.requireSecret("password");
// All resources will share a resource group.
const resourceGroupName = new resources.ResourceGroup("server-rg").name;
// Create a network and subnet for all VMs.
const virtualNetwork = new network.VirtualNetwork("server-network", {
resourceGroupName,
addressSpace: { addressPrefixes: ["10.0.0.0/16"] },
subnets: [{
name: "default",
addressPrefix: "10.0.1.0/24",
}],
});
// Now allocate a public IP and assign it to our NIC.
const publicIp = new network.PublicIPAddress("server-ip", {
resourceGroupName,
publicIPAllocationMethod: network.IPAllocationMethod.Dynamic,
});
const networkInterface = new network.NetworkInterface("server-nic", {
resourceGroupName,
ipConfigurations: [{
name: "webserveripcfg",
subnet: virtualNetwork.subnets.apply(subnet => subnet![0]),
privateIPAllocationMethod: network.IPAllocationMethod.Dynamic,
publicIPAddress: { id: publicIp.id },
}],
});
const initScript = `#!/bin/bash\n
echo "Hello, World!" > index.html
nohup python -m SimpleHTTPServer 80 &`;
// Now create the VM, using the resource group and NIC allocated above.
const vm = new compute.VirtualMachine("server-vm", {
resourceGroupName,
networkProfile: {
networkInterfaces: [{ id: networkInterface.id }],
},
hardwareProfile: {
vmSize: compute.VirtualMachineSizeTypes.Standard_A0,
},
osProfile: {
computerName: "hostname",
adminUsername: username,
adminPassword: password,
customData: Buffer.from(initScript).toString("base64"),
linuxConfiguration: {
disablePasswordAuthentication: false,
},
},
storageProfile: {
osDisk: {
createOption: compute.DiskCreateOption.FromImage,
name: "myosdisk1",
},
imageReference: {
publisher: "canonical",
offer: "UbuntuServer",
sku: "16.04-LTS",
version: "latest",
},
},
});
// The public IP address is not allocated until the VM is running, so wait for that
// resource to create, and then lookup the IP address again to report its public IP.
export const ipAddress = vm.id.apply(_ => network.getPublicIPAddressOutput({
resourceGroupName: resourceGroupName,
publicIpAddressName: publicIp.name,
})).ipAddress;