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? Automation: No need to manually configure resources. Consistency: The same code can be reused to deploy environments. Version Control: Store configurations in Git to track changes. Multi-Cloud Support: Works with AWS, Azure, Google Cloud, and more. STEPS TO INSTALL,CONFIGURE AND USE TERRAFORM First step is to install Terraform on our local environment (Linux) to do that we click the link https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli Then we click on Linux and we copy the first code and paste on our environment terminal. Move to the next code, copy and paste in your environment. Copy the next code also and do the same. Do the same also for the next code. Copy the next two code simultaneously and paste on your terminal in your local environment. Next we need to verify that we have successfully installed terraform we type terraform -help 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? Efficiency: Quickly perform tasks without navigating through a web interface. Automation: Easily script and automate repetitive tasks. Cross-Platform: Works on Windows, macOS, and Linux. Getting Started Install Azure CLI: Follow the installation guide for your operating system, for this we are using LINUX operating System https://learn.microsoft.com/en-us/cli/azure/install-azure-cli-linux?pivots=apt copy the code and paste in ur local environment 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 After successfully logging into your Azure account from the URL provided you should get this popping up. You choose your current subscription if you have multiple. Next we are going to create a RG (resource group), using the command az create --name (rg name) --location (Preferred location) If u check your azure account, you will see that u have a created a resource group and location. CREATING A TERRAFORM / Running Terraform Initialize: terraform init Plan: terraform plan 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. 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 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 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. Next we need to hit terraform plan this helps you preview the changes Terraform will make to your infrastructure before actually applying them. Terraform Apply is the next code will implement the terraform plan. You will be asked to confirm by typing Yes For confirmation head to your azure to confirm that u have successfully created a resource group

Jan 21, 2025 - 01:05
 0
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

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow