Learn Terraform - How can we make the Linux VM become a Web Server

The next iteration of the VM is to configure a Web Server running on the VM and add an auto-scaling function as well as a load balancer. Due to the point, that I’m not so aware of Linux, I took a little bit different approach to have a Web Server running on the VM. Yevgeniy uses in his book the following “user_data” option to have a web site been served by our VM.

1
2
3
4
5
user\_date = <<-EOF
#!/bin/bash
echo "Hello, World" > index.html
nohup busybox httpd -f -p 8080 &
EOF

I tried to get this as a script running in the VM just deployed. But I did not find out what will be the best way. So maybe this is a challenge for later, but take it the other way around, what is the normal Way in Azure to get something running in a VM just deployed. I normally use the custom script extensions to run a command in a machine. Especially in a Windows VM I would use any desired state configuration with this option. If you want to learn more about custom script extension focusing on a Linux VMs visit this DOCs article.

With this knowledge we now can add a section in our script to deploy a custom script extension:

1
2
3
4
5
6
7
8
9
10
11
12
13
resource "azurerm\_virtual\_machine\_extension" "myFirstTerraform" {
name = "myFirstTerraform-Script"
virtual\_machine\_id = azurerm\_linux\_virtual\_machine.myFirstTerraform.id
publisher = "Microsoft.Azure.Extensions"
type = "CustomScript"
type\_handler\_version ="2.0"

settings = <<SETTINGS
{
"commandToExecute" : "apt-get -y update && apt-get install -y apache2"
}
SETTINGS
}

The important configuration is made in the settings section. I added the command to install an apache web server on the machine and then we will have the standard website been served in port 80 on the Linux VM. The only trouble we get is, our network security group (NSG) we deployed, was only opening the ssh port. So we must add an additional rule in the NSG. So our NSG will look like this:

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
 resource "azurerm\_network\_security\_group" "myFirstTerraform" {
name = "myFirstTerraform"
location = azurerm\_resource\_group.myFirstTerraform.location
resource\_group\_name = azurerm\_resource\_group.myFirstTerraform.name

security\_rule {
name = "SSH"
priority = 1001
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source\_port\_range = "\*"
destination\_port\_range = "22"
source\_address\_prefix = "\*"
destination\_address\_prefix = "\*"
}

security\_rule {
name = "WebServer"
priority = 1002
direction = "Inbound"
access = "Allow"
protocol = "Tcp"
source\_port\_range = "\*"
destination\_port\_range = "80"
source\_address\_prefix = "\*"
destination\_address\_prefix = "\*"
}
}

If we now would run our script, we will be able to see the default apache web site on our Linux VM running in Azure:

Default apache website

To connect to this website it would be great to know on which public IP assigned to our Linux VM. As we learn in the book, we can use the output variables to achieve this. But there is one important difference. In Azure, a public IP is a resource on his own and will be attached to a network interface that then will be assigned to a VM. So we need to reference the IP in our output and not the VM.

What does that mean for our script:

1
2
3
4
output "public\_ip" {
value = azurerm\_public\_ip.myFirstTerraform.ip\_address
description = "This is the assigned public ip to our VM"
}

If we have added this output to our script we can afterwards just get the ip after you apply your script again:

1
2
3
4
5
$ terraform apply

Outputs:

public\_ip = 51.136.162.193

If you need the output of your latest terraform deployment again you just can call:

1
2
3
$ terraform output public\_ip

51.136.162.193

So now you know the ip to browse to, if you want to see your apache 2 default website.