|
| 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) |
0 commit comments