Deploying Your First Azure VM using Terraform: Super Fast

Photo by Petr Slováček on Unsplash

If you’re new to the Hashicorp world for deployments or perhaps have been using something like Heroku for all cloud setups then this might be a useful guide to how to get rolling with infra setups for your apps.

I’d like to take you through the process of using Hashicorp’s Terraform for setting up some infra in the cloud. Here what we’re doing is using Azure as a cloud provided and setting up a simple Ubuntu VM and then connecting to it. The idea is to give you a clear idea on what the steps are and how you can optimize this yourself for you particular setup.

Step 1 What you need

Get a Azure subscription
Run Azure Cloud Shell, check all is cool using

az account show

Step 2 — Create a Terraform Config File

mkdir terraform && cd $_

Using the following code

provider "azurerm" {
version = "1.38.0"

#create resource group
resource "azurerm_resource_group" "rg" {
name = "rg-MyFirstTerraform"
location = "westus2"
tags = {
Environment = "Terraform Demo"

#Create virtual network
resource "azurerm_virtual_network" "vnet" {
name = "vnet-dev-westus2-001"
address_space = [""]
location = "westus2"
resource_group_name =

# Create subnet
resource "azurerm_subnet" "subnet" {
name = "snet-dev-westus2-001 "
resource_group_name =
virtual_network_name =
address_prefix = ""

# Create public IP
resource "azurerm_public_ip" "publicip" {
name = "pip-vmterraform-dev-westus2-001"
location = "westus2"
resource_group_name =
allocation_method = "Static"

# Create network security group and rule
resource "azurerm_network_security_group" "nsg" {
name = "nsg-sshallow-001 "
location = "westus2"
resource_group_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 = "*"

# Create network interface
resource "azurerm_network_interface" "nic" {
name = "nic-01-vmterraform-dev-001 "
location = "westus2"
resource_group_name =
network_security_group_id =

ip_configuration {
name = "niccfg-vmterraform"
subnet_id =
private_ip_address_allocation = "dynamic"
public_ip_address_id =

# Create virtual machine
resource "azurerm_virtual_machine" "vm" {
name = "vmterraform"
location = "westus2"
resource_group_name =
network_interface_ids = []
vm_size = "Standard_B1s"

storage_os_disk {
name = "stvmpmvmterraformos"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Premium_LRS"

storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04.0-LTS"
version = "latest"

os_profile {
computer_name = "vmterraform"
admin_username = "terrauser"
admin_password = "Password1234!"

os_profile_linux_config {
disable_password_authentication = false

Step 3 Now, let’s run terraform init to initialize our directory.

terraform plan -out=myterraplan

With the command terraform show we can specify our plan file name and review the plan or even have another team member review it.

Step 4

Next we want to do the Run our terraform apply against the actions in the plan file by specifying the file name. Notice that we won’t be prompted to confirm that we want to apply the changes when we run apply this way:

terraform apply myterraplan

The infra will start getting built.

Step 5 Play with your newly built infra

Start having some fun and play with the stuff you’ve build by connecting int first.

ssh terrauser@

Step 5 Blow the infra away

The first thing you want to do is do a plan for the destroy action

terraform plan -destroy -out=myplanfiledestroy

Carefully review this as it includes a bunch of actions, including destoying all the infra associated to the VM including public address

terraform apply myplanfiledestroy

What to do next

Thats really about it. What you do want to do is add this to your git pipleine and use Visual Studio code for edits and use the associated reference files for terraform code optimisation.

I hope you’ve found this a simple guide to setting up some infra for your apps.

Photo by Pankaj Patel on Unsplash

Useful commands

terraform init — Used to initialise the Terraform directory
terraform plan — Shows the Terraform plan
terraform apply — Applies the Terraform code
terraform destroy — Destroys resources deployed via Terraform

Useful Links




I build stuff at

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Match Town Makeover MOD Apk Download **2021

Match Town Makeover MOD Apk Download **2021

CS371P Fall 2021 Week 8: Vishal Tak

Kim’s Top 5 Hacks for Bridging Business and Technology

7 symptoms of insufficient traceability in your software project

5 Programming Languages That Google Uses

Using Vim to Create some Test Data

How to use Heroku to deploy your PHP website.

Top 10 Questions For Getting Started With Docker Training

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Haseeb Chaudhary

Haseeb Chaudhary

I build stuff at

More from Medium

Runbooks to improve the operability of services

I Attempted Terraform Remote and Imploded VS Code — Here’s How I Fixed It.

Move your resources in Terraform state

Custom Policies in Checkov