Build, Change, and destroy Azure infrastructure using Terraform

C.J. Shields
5 min readAug 12, 2020

Install Terraform

I’m running Windows update 2004 so in this instance, I will be using Chocolatey to install Terraform.

I ran this command in Powershell as administrator to install Chocolatey

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString(‘https://chocolatey.org/install.ps1'))

Once Chocolatey is installed, run the next command to install the latest version of Terraform. At the time of this article, the latest version is 13.0

choco install terraform

In this instance, I already had Terraform versions 0.12.28 installed on my machine so I used the next command to upgrade to 0.13.0

choco upgrade terraform

In Powershell, I created myself a Terraform directory and created the file name main.tf and pasted the following contents

terraform {
required_providers {
docker = {
source = “terraform-providers/docker”
}
}
}

provider “docker” {
host = “npipe:////.//pipe//docker_engine”
}

resource “docker_image” “nginx” {
name = “nginx:latest”
keep_locally = false
}

resource “docker_container” “nginx” {
image = docker_image.nginx.latest
name = “tutorial”
ports {
internal = 80
external = 8000
}
}

Next I Initialized the project using terraform init. This downlaods a plugin that allows Terraform to interact with Docker.

terraform init

Once that step is complete, I provisioned my NGINX server container with apply. Output listed below

terraform apply

Verify your NGINX container by visiting localhost:8000 in your web browser.

You can also type in docker ps to see the container

docker ps

To stop our container, Ill run the following command

terraform destroy

Create configuration (The Juicy stuff)

To create my configuration I’ll need to create another main.tf file. I will use the following config to configure my azure infrastrucure

terraform {
required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “~>1.32.0”
}
}
}

# Configure the Azure provider
provider “azurerm” {}

# Create a new resource group
resource “azurerm_resource_group” “rg” {
name = “myTFResourceGroup”
location = “westus2”
}

I then ran my terraform init command

terraform init

Next I ran the following command to generate an execution plan. This specifies what actions Terraform will take to achieve the desired state defined in the config as well as the order in which the actions occur.

terraform plan

Finally I ran my terraform apply. The output shows the execution plan and will prompt for an approval before proceeding. If anything is incorrect, it’s safe to abort without any changes.

terraform apply

Change Infrastructure

In the directory where my main.tf file sits, I ran the command code main.tf. This opened my config file in a shell editor, in this case for me it is visual studio code. I added the following tags to my configuration

resource “azurerm_resource_group” “rg” {
name = “myTFResourceGroup”
location = “westus2”

tags = {
Environment = “Terraform Getting Started”
Team = “DevOps”
}
}

Since I’ve made a change Terraform will show what actions that will take effect. I ran the following command to update the plan. The -out argument tells Terraform to save the plan in a new file.

terraform plan -out=newplan

Next I applied my changes using the following command:

terraform apply “newplan”

Destroy Infrastructure

This command shows the actions it will take to destroy your infrastructure

terraform plan -destroy

This next command is important if you don’t want to accrue any charges on your Azure bill. This will remove the resource group.

terraform destroy

Create Resource Dependencies

# Create a virtual network
resource “azurerm_virtual_network” “vnet” {
name = “myTFVnet”
address_space = [“10.0.0.0/16”]
location = “westus2”
resource_group_name = azurerm_resource_group.rg.name
}

Apply Configuration

terraform init

Then apply changes

terraform apply

We have just successfully deployed an Azure virtual machine using infrastructure as a code

Defining Variables

First I created a variables.tf file and added the following contents.

variable “location” {}

variable “admin_username” {
type = “string”
description = “Administrator user name for virtual machine”
}

variable “admin_password” {
type = “string”
description = “Password must meet Azure complexity requirements”
}

variable “prefix” {
type = “string”
default = “my”
}

variable “tags” {
type = “map”

default = {
Environment = “Terraform GS”
Dept = “Engineering”
}
}

variable “sku” {
default = {
westus2 = “16.04-LTS”
eastus = “18.04-LTS”
}
}

I then created another main.tf file and uploaded both to the file folder I was working in.

Assigning Variables

From a file

I created a file named terraform.tfvars and assigned the following variables. Terraform can populate variables using values from a file.

location = “westus2”
prefix = “tf”
admin_username = “plankton”
admin_password = “Password1234!”

--

--