DEV Community

Cover image for CLOUD RESOURCE DEPLOYMENT ON AZURE USING TERRAFORM
Omoniyi
Omoniyi

Posted on

CLOUD RESOURCE DEPLOYMENT ON AZURE USING TERRAFORM

TERRAFORM
it is an infrastructure as code (IaC) tool that lets you build, change, and version cloud and on-prem resources safely and efficiently. It leverages HashiCorp Configuration Language (HCL) syntax to construct configuration files, allowing users to specify details about their cloud infrastructure, including the cloud provider such as Azure, GCP, AWS etc. You can then use a consistent workflow to provision and manage all of your infrastructure throughout its lifecycle using the configuration file.

MAIN STAGES IN TERRAFORM
The core Terraform workflow consists of three stages:

Write: You define resources, which may be across multiple cloud providers and services. For example, you might create a configuration file to deploy an application on virtual machines in a Virtual Private Cloud (VPC) network with security groups and a load balancer.

Plan: Terraform creates an execution plan describing the infrastructure it will create, update, or destroy based on the existing infrastructure and your configuration file.

Apply: On approval, Terraform performs the proposed operations in the correct order, respecting any resource dependencies. For example, if you update the properties of a VPC and change the number of virtual machines in that VPC, Terraform will recreate the VPC before scaling the virtual machines.

HOW TO DEPLOY A VIRTUAL MACHINE IN AZURE USING TERRAFORM
PREREQUISITES
Before we begin, make sure you have the following prerequisites:
Azure Subscription: You need an active Azure subscription ID, Tenant ID, Azure Portal login details.
Azure CLI: Install the Azure Command-Line Interface to interact with Azure from the command line.
Terraform: Install Terraform on your local machine.

STEPS TO DELOYMENT

  • Create a working directory (folder) eg Terraform. located in C:\Users\Presh\Documents. This can also be created by using mkdir command.
  • Open Virtual Studio Code, Go to File menu and open the directory you created above

Image description

Image description

  • Create a file in the directory and name it main.tf

Image description

Image description

  • Customize as desired, edit the VM Parameters, Resource Group Parameters, Network Parameters etc and save.

On Terminal,

  • run the command az login to confirm active azure subscription and set up authentication. Choose the account you want to use Choose which of the desired subscriptions by inputting the corresponding figure.

Image description

Image description

  • run the command terraform init to initialize Terraform in the working directory. this will create a .terraform folder and .terraform,lock,hcl file in the working directory

Image description

  • run the command terraform plan. This reviews the contents of the configuration files and give a summary for validation.

Image description

  • run the command terraform apply. This performs the configuration file proposed operation in the correct order.

Image description

Image description

Go to the Azure Portal and navigate to the Resource Groups section. You should see the newly created resource group, "acctestrg".

Image description

To remove the resource created using terraform,

  • run the command terraform destroy
  • type yes at the subsequent prompt

Image description

Image description

Conclusion
Following through the step-by-step procedures, we have successfully deployed a cloud infrastructure (virtual machine in this case),it could be any cloud infrastructure. Exploring through additional resources and hand-on will definitely firm you grip.

Its been nice having you again. Its been Sunday Omoniyi aka Presh

Top comments (0)