Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Azure webserver example #455

Merged
merged 3 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/azure-webserver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
### Scala an JVM
*.class
*.log
.bsp
.scala-build

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

kubeconfig.json
122 changes: 122 additions & 0 deletions examples/azure-webserver/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import besom.*
import besom.api.azurenative

@main def main = Pulumi.run {
// Get the desired username and password for our VM.
val username = config.requireString("username")
val password = config.requireString("password")

// All resources will share a resource group.
val resourceGroupName = azurenative.resources.ResourceGroup("server-rg").name

// Create a network for all VMs.
val virtualNetwork = azurenative.network.VirtualNetwork(
name = "server-network",
azurenative.network.VirtualNetworkArgs(
resourceGroupName = resourceGroupName,
addressSpace = azurenative.network.inputs.AddressSpaceArgs(
addressPrefixes = List(
"10.0.0.0/16"
)
)
)
)

// Create a subnet within the Virtual Network.
val subnet = azurenative.network.Subnet(
name = "default",
azurenative.network.SubnetArgs(
resourceGroupName = resourceGroupName,
virtualNetworkName = virtualNetwork.name,
addressPrefix = "10.0.1.0/24"
)
)

// Now allocate a public IP and assign it to our NIC.
val publicIp = azurenative.network.PublicIpAddress(
name = "server-ip",
azurenative.network.PublicIpAddressArgs(
resourceGroupName = resourceGroupName,
publicIPAllocationMethod = azurenative.network.enums.IpAllocationMethod.Dynamic
)
)

val networkInterface = azurenative.network.NetworkInterface(
name = "server-nic",
azurenative.network.NetworkInterfaceArgs(
resourceGroupName = resourceGroupName,
ipConfigurations = List(
azurenative.network.inputs.NetworkInterfaceIpConfigurationArgs(
name = "webserveripcfg",
subnet = azurenative.network.inputs.SubnetArgs(id = subnet.id),
privateIPAllocationMethod = azurenative.network.enums.IpAllocationMethod.Dynamic,
publicIPAddress = azurenative.network.inputs.PublicIpAddressArgs(id = publicIp.id)
)
)
)
)

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

// Now create the VM, using the resource group and NIC allocated above.
val vm = azurenative.compute.VirtualMachine(
name = "server-vm",
azurenative.compute.VirtualMachineArgs(
resourceGroupName = resourceGroupName,
networkProfile = azurenative.compute.inputs.NetworkProfileArgs(
networkInterfaces = List(
azurenative.compute.inputs.NetworkInterfaceReferenceArgs(id = networkInterface.id)
)
),
hardwareProfile = azurenative.compute.inputs.HardwareProfileArgs(
vmSize = azurenative.compute.enums.VirtualMachineSizeTypes.Standard_A0
),
osProfile = azurenative.compute.inputs.OsProfileArgs(
computerName = "hostname",
adminUsername = username,
adminPassword = password,
customData = base64.encode(initScript),
linuxConfiguration = azurenative.compute.inputs.LinuxConfigurationArgs(
disablePasswordAuthentication = false
)
),
storageProfile = azurenative.compute.inputs.StorageProfileArgs(
osDisk = azurenative.compute.inputs.OsDiskArgs(
createOption = azurenative.compute.enums.DiskCreateOptionTypes.FromImage,
name = "myosdisk1"
),
imageReference = azurenative.compute.inputs.ImageReferenceArgs(
publisher = "canonical",
offer = "UbuntuServer",
sku = "16.04-LTS",
version = "latest"
)
)
)
)

// TODO uncomment when bug https://github.com/VirtusLab/besom/issues/432 will be fixed
// val ipAddress =
// azurenative.network
// .getPublicIPAddress(
// azurenative.network.GetPublicIpAddressArgs(
// resourceGroupName = resourceGroupName,
// publicIpAddressName = publicIp.name
// )
// )
// .ipAddress

Stack(publicIp).exports(
vmName = vm.name
// TODO uncomment when bug https://github.com/VirtusLab/besom/issues/432 will be fixed
// ipAddress = ipAddress
)
}

object base64:
def encode(v: String): String =
java.util.Base64.getEncoder.encodeToString(v.getBytes)
3 changes: 3 additions & 0 deletions examples/azure-webserver/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: azure-webserver
description: Azure web server example
runtime: scala
66 changes: 66 additions & 0 deletions examples/azure-webserver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# 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.

## Deploying the App

To deploy your infrastructure, follow the below steps.

### Prerequisites

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

## Running the App

1. Create a new stack:

```
$ pulumi stack init dev
```

2. 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).

The supplied password must be between 6-72 characters long and must satisfy at least 3 of password complexity
requirements from the following:
- Contains an uppercase character
- Contains a lowercase character
- Contains a numeric digit
- Contains a special character
- Control characters are not allowed

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

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

3. Stand up the cluster by invoking pulumi
```bash
$ pulumi up
```

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

Go to azure portal to service `Virtual Machines`.
Find newly created virtual machine and get `Public IP address` from it.

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

```
$ curl http://<Public IP address from virtual machine>
Hello, World!
```
6. From there, feel free to experiment. Simply making edits and running `pulumi up` will incrementally update your
stack.

7. Once you've finished experimenting, tear down your stack's resources by destroying and removing it:

```bash
$ pulumi destroy --yes
$ pulumi stack rm --yes
```
4 changes: 4 additions & 0 deletions examples/azure-webserver/project.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//> using scala "3.3.1"
//> using options -Werror -Wunused:all -Wvalue-discard -Wnonunit-statement
//> using dep "org.virtuslab::besom-core:0.3.0"
//> using dep "org.virtuslab::besom-azure-native:2.37.0-core.0.3"
Loading