Getting started with Azure deployment using Terraform
In this post, I will discuss on how you can obtain Terraform in your working
environment and briefly demonstrates on how to create a resource group with
Terraform *.tf
files containing those Terraform configuration language.
Setting up Terraform CLI
Since Terraform is a multiple platform tool for multiple cloud service providers and I have included Terraform tool deployment for macOS, Linux and Windows.
macOS
In this walkthrough section, I will demonstrate on how you can use terraform
on macOS Catalina to deploy in Azure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Go to your Downloads directory as your working directory
cd ~/Downloads
# Download the Terraform
curl -sSO https://releases.hashicorp.com/terraform/0.12.19/terraform_0.12.19_darwin_amd64.zip
# Unzip the downloaded compressed file
unzip terraform_0.12.19_darwin_amd64.zip
# Make a Terraform directory
mkdir -p ~/Terraform
# Move the terraform file to Terraform directory
mv terraform ~/Terraform
# Set up Terraform PATH
echo 'export PATH="/Users/$USER/Terraform:$PATH"' >> ~/.zshrc
Quit your current terminal and launch a new terminal window. Now you can check the terraform version from the terminal to determine if the file path is configured properly.
1
2
# Check the version
terraform -version
↑Top
Linux
In this walkthrough section, I will demonstrate on how you can use terraform
on linux to deploy in Azure.
1
2
3
4
5
6
7
8
9
10
11
# Go to your home directory as your working directory
cd ~
# Download the Terraform
curl -sSO https://releases.hashicorp.com/terraform/0.12.19/terraform_0.12.19_linux_amd64.zip
# Unzip the downloaded compressed file
unzip terraform_0.12.19_linux_amd64.zip
# Move the terraform file to Terraform directory
mv ~/terraform /usr/local/bin/terraform
Once you have moved the terraform
file to /usr/local/bin
directory, test if
you execute terraform
by checking its version.
1
2
# Check the version
terraform -version
↑Top
Windows
In this walkthrough section, I will demonstrate on how you can use terraform
on Windows to deploy in Azure.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Download the Terraform
Invoke-WebRequest `
-Uri "https://releases.hashicorp.com/terraform/0.12.19/terraform_0.12.19_windows_amd64.zip" `
-UseBasicParsing `
-OutFile "~\Downloads\terraform_0.12.19_windows_amd64.zip" ;
# Unzip the downloaded compressed file
Expand-Archive `
-Path "~\Downloads\terraform_0.12.19_windows_amd64.zip" `
-DestinationPath "~\Downloads" ;
# Make a Terraform directory
New-Item `
-Path "$env:ProgramFiles\Terraform" `
-ItemType "directory" ;
# Move the terraform file to Terraform directory
Move-Item `
-Path "~\Downloads\terraform.exe" `
-Destination "$env:ProgramFiles\Terraform\terraform.exe" ;
# Add to environment path
$env:Path += ";$env:ProgramFiles\Terraform"
Once you add it to the environment path, test if you execute terraform.exe
and checks its version.
1
2
# Check the version
terraform.exe -version
↑Top
Example - Creating resource group using Terraform
After obtaining terraform
, let us try to create a resource group using
terraform
as an example.
Note:
Depending on whether if you are using AzCLI or PowerShell, you will need to authenticate to Azure.
1 2# Login to Azure using AzCLI az login
or
1 2 # Login to Azure using PowerShell Connect-AzAccountOnce you have authenticated, proceed with the steps below with your preferred console or terminal.
1
2
# Create a folder name for the terraform example
mkdir -p ~/Terraform/example1
Firstly, define the provider in the main.tf
file.
1
2
3
4
5
echo \
'# Configure the Azure provider
provider "azurerm" {
version = "~>1.5"
}' > ~/Terraform/example1/main.tf
Secondly, define the resource in the resource.tf
file.
1
2
3
4
5
6
7
8
9
10
echo \
'# Create a new resource group
resource "azurerm_resource_group" "rg" {
name = "terraform-resource-group"
location = "southeastasia"
tags = {
Environment = "Development"
DeploymentType = "Terraform"
}
}' > ~/Terraform/example1/resource.tf
Once you have defined the configuration in main.tf
and resource.tf
files,
execute teeraform
with init
argument to initialize the working directory.
1
2
3
# Initialize a working directory containing Terraform configuration files
terraform init \
~/Terraform/example1
Next, use the plan
argument with the -out
parameter to generate the
execution plan file from the working directory.
1
2
3
4
# Create an execution plan
terraform plan \
-out ~/Terraform/example1/out.plan \
~/Terraform/example1
Finally, use the apply
argument with the execution plan file to apply
changes in Azure.
1
2
3
4
# Apply the changes
terraform apply \
-state ~/Terraform/example1/terraform.tfstate \
~/Terraform/example1/out.plan
Now, go to your Azure and check if the resource group has been created.
↑Top
References
- Terraform - Download
- Microsoft Docs - Azure Terraform Documentation
- Terraform CLI Docs - Terraform Commands (CLI)
- Terraform Docs - Configuration Language
- Terraform Docs - Getting Started - Azure
- Terraform Docs - Azure Provider
↑Top
Related Books
↑Top