DEV Community

Cover image for Automate Your Cloud with Terraform and Azure CLI: Step-by-Step Tutorial
Emmanuel Essien
Emmanuel Essien

Posted on

Automate Your Cloud with Terraform and Azure CLI: Step-by-Step Tutorial

What is Terraform?
Terraform is a tool that helps you create and manage cloud resources like virtual machines, networks, databases, and more using code. Instead of manually clicking through a cloud provider's interface, you write a file describing what you want,and Terraform makes it happen.

Why Use Terraform?

  1. Automation: No need to manually configure resources.

  2. Consistency: The same code can be reused to deploy environments.

  3. Version Control: Store configurations in Git to track changes.

  4. Multi-Cloud Support: Works with AWS, Azure, Google Cloud, and more.

STEPS TO INSTALL,CONFIGURE AND USE TERRAFORM

Info

Info

  • Move to the next code, copy and paste in your environment.

Info

Info

  • Copy the next code also and do the same.

Info

Info

  • Do the same also for the next code.

Info

Info

  • Copy the next two code simultaneously and paste on your terminal in your local environment.

Info

Info

  • Next we need to verify that we have successfully installed terraform we type terraform -help

Info

What is AZURE CLI

Azure Command-Line Interface is a tool u can use to manage Azure Resources directly from your command line. It is a way to communicate with Azure using text commands instead of clicking around in a web portal.

Why Use Azure CLI?

  1. Efficiency: Quickly perform tasks without navigating through a web interface.

  2. Automation: Easily script and automate repetitive tasks.

  3. Cross-Platform: Works on Windows, macOS, and Linux.

Getting Started

Info

Info

  • To Prove that it worked we type the code az login then u will get a url at the end of the page to redirect you to your azure login page

Info

Info

  • After successfully logging into your Azure account from the URL provided you should get this popping up.

Info

  • You choose your current subscription if you have multiple.

Info

  • Next we are going to create a RG (resource group), using the command az create --name (rg name) --location (Preferred location)

Info

If u check your azure account, you will see that u have a created a resource group and location.

Info

CREATING A TERRAFORM / Running Terraform

  1. Initialize: terraform init

  2. Plan: terraform plan

  3. Apply: terraform apply

  • We are going to create a directory using mkdir (name of the directory) and CD (name of the directory) into the directory.

Info

Info

  • Next we create a terraform file using our text editor VIM to create and edit the file i am going to name it main.tf

Note: TOUCH command only creates the file, But VIM allows use to create and edit the text file press I to be in insert mode to be able to type in our text file

Info

Info

  • Next we generate a main.tf file that will deploy a resource group to azure in our text file making sure your azure subscription is added. To save this and exit our text editor we hit Shift column wq

Info

  • Next we hit the code terraform init this will initialize a working directory containing. It is the first command you run when you start working with Terraform.

Info

  • Next we need to hit terraform plan this helps you preview the changes Terraform will make to your infrastructure before actually applying them.

Info

  • Terraform Apply is the next code will implement the terraform plan.

Info

You will be asked to confirm by typing Yes

Info

Info

For confirmation head to your azure to confirm that u have successfully created a resource group

Info

Top comments (0)