Member-only story

How to Deploy an Ubuntu Linux VM in Azure using Terraform

--

In this story, we will learn how to deploy an Ubuntu Linux VM in Azure using Terraform.

Prerequisites

Before creating our Azure Virtual Machine, we will need an Azure SPN (Service Principal) to execute our Terraform code (check step 1 of this story if you need help creating an SPN).

Creating a Terraform file for Azure Authentication

First, we create a file called provider-variables.tf, used by Azure authentication variables.

We will use a Service Principal with a Client Secret. Check the link below for more info about Azure authentication for Terraform: https://www.terraform.io/docs/providers/azurerm/guides/service_principal_client_secret.html

variable "azure_subscription_id" {
type = string
description = "Azure Subscription ID"
}
variable "azure_client_id" {
type = string
description = "Azure Client ID"
}
variable "azure_client_secret" {
type = string
description = "Azure Client Secret"
}
variable "azure_tenant_id" {
type = string
description = "Azure Tenant ID"
}

After that, we edit the file terraform.tfvars and add the Azure credential information:

azure_subscription_id = "your-azure-subscription-id"
azure_client_id = "your-azure-client-id"
azure_client_secret = "your-azure-client-secret"
azure_tenant_id = "your-azure-tenant-id"

Finally, we create the provider-main.tf, used to configure Terraform and the Azure provider:

# Define Terraform provider
terraform {
required_version = "~> 1.0"
}
# Configure the Azure provider
provider "azurerm" {
features {}
environment = "public"
subscription_id = var.azure-subscription-id
client_id = var.azure-client-id
client_secret = var.azure-client-secret
tenant_id = var.azure-tenant-id
}

Creating a Terraform file for the Network

In this step, we will create the file network-variables.tf to configure network variables and add the following code:

variable…

--

--

Guillermo Musumeci
Guillermo Musumeci

Written by Guillermo Musumeci

Certified AWS, Azure & GCP Architect | HashiCorp Ambassador | Terraform SME | KopiCloud Founder | ex-AWS | Entrepreneur | Book Author | Husband & Dad of ✌

No responses yet

Write a response