Pushing config to an Ubuntu 24.04 LTS Machine in Azure Using Terraform


Managing infrastructure as code is a crucial practice in modern IT operations, and Terraform is a powerful tool to achieve this. In this blog post, we will walk through the process of pushing user data cloud config to an Ubuntu 24.04 LTS machine in Azure using Terraform. We’ll highlight the key components of the Terraform code and explain the templating mechanism used to inject user data.

Setting Up the Virtual Machine

We start by defining our Azure virtual machine using the azurerm_linux_virtual_machine resource. This resource is configured to deploy an Ubuntu 24.04 LTS machine.

resource "azurerm_linux_virtual_machine" "MyTestLinuxVM" {
  name                = var.virtualmachinename
  resource_group_name = data.azurerm_resource_group.MyTestRG.name
  location            = data.azurerm_resource_group.MyTestRG.location
  size                = "Standard_B1s"` 

source_image_reference {
    publisher = "Canonical"
    offer     = "ubuntu-24_04-lts"
    sku       = "server"
    version   = "latest"
  } ... 

In the source_image_reference block, we specify the image details for Ubuntu 24.04 LTS. The publisher is set to “Canonical,” the offer is “ubuntu-24_04-lts,” and the sku is “server,” with the version set to “latest” to ensure we always use the most recent LTS version.

Adding User Data

One of the critical aspects of this setup is the user data configuration. User data allows us to provide initialization scripts that run when the VM first boots. In this example, we use a shell script template to customize the initialization process.

user_data = base64encode(templatefile("${path.module}/user_data.sh", { ADMINUSER = var.adminuser })) }

We use the templatefile function to load a shell script from a file named user_data.sh and inject variables using a templating mechanism. The base64encode function encodes the script content to base64, as required by the user_data attribute.

Here’s an example of what the user_data.sh might look like:

#!/bin/bash
# cloud init runs as root user
set -ex # exit on error
apt update -y # update package list
apt upgrade -y # upgrade packages
apt install -y docker.io # install docker
usermod -a -G docker ${ADMINUSER} # add user to docker group
systemctl enable --now docker # start and enable docker service
date >> /cloud-init-done.txt # write date to file
echo "Cloud Init Done" >> /cloud-init-done.txt # write message to file

In this script, the ${ADMINUSER} placeholder is replaced with the actual admin username provided in the Terraform variables.

So those are the two aspects you need in your configuration to use the latest LTS and setup cloud init

Conclusion

Using Terraform to push user data cloud config to an Ubuntu 24.04 LTS machine in Azure simplifies and automates the setup process. By leveraging Terraform’s templating and encoding features, we ensure that our VMs are initialized correctly and securely. This approach not only streamlines deployment but also enhances the reproducibility and manageability of our infrastructure.