6

Automate Infrastructure in Azure using Terraform and Azure DevOps

 7 months ago
source link: https://microsoftgeek.com/?p=3414
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Automate Infrastructure in Azure using Terraform and Azure DevOps

Automating infrastructure setup in Azure Cloud using Terraform and Azure DevOps is a powerful approach to managing your infrastructure as code (IaC). This allows you to define and manage your Azure resources programmatically, version control your infrastructure configurations, and automate the deployment process. 

We will be creating a Java based WebApp(app service) in Azure cloud using Terraform and automate the process using Azure Devops Pipelines.

Screenshot%202023-09-07%20at%203.30.42%20PM.png

Pre-requisites:

  • Azure subscription
  • Azure DevOps organization and project
  • Terraform Build & Release Tasks extension in Azure DevOps
  • Terraform files created and stored in SCM for creating resources in Azure cloud

Implementation Steps:

  • Create a resource group first in Azure cloud
  • Create storage account, container for Terraform to store state information in Azure cloud remotely.
  • Create pipeline and add Terraform tasks
  • Execute pipeline to deploy resources(App service plan and WebApp) in Azure cloud
  • Verify resources are created in Azure cloud
  • Confirm if the terraform.tfstate file is updated

Create Resource Group in Azure cloud

Login to Azure portal, Create a new RG, or you can skip this step if you already have existing group.

Create Storage Account in Azure cloud

Create a new resource, type storage account

Screenshot%202023-09-05%20at%209.51.22%20AM.png

Click on create

Screenshot%202023-09-05%20at%209.52.44%20AM.png

select RG, enter unique account name, select standard as performance, select locally redundant storage

Screenshot%202023-09-05%20at%209.55.51%20AM.png

Click on create

Create a container inside the storage account

Select the storage account you just created, click on containers under Data storage

Screenshot%202023-09-05%20at%209.59.24%20AM.png

enter a name for the container

Screenshot%202023-09-05%20at%2010.01.09%20AM.png

Create a pipeline in Azure DevOps

Login to Azure Devops, select Pipelines, select use the classic editor to create a pipeline

Screenshot%202023-09-05%20at%2011.46.07%20AM.png

Configure Pipeline with tasks

Add Terraform installer task to install Terraform on build agent

Screenshot%202023-09-05%20at%2012.40.22%20PM.png

Add Terraform task 

Screenshot%202023-09-05%20at%2012.41.55%20PM.png

Configure the task

Screenshot%202023-09-05%20at%2012.44.42%20PM.png

enter storage account, container, key information as below:

Screenshot%202023-09-05%20at%2012.47.16%20PM.png

Add terraform tasks to plan, apply

Add Terraform task for plan

Screenshot%202023-09-05%20at%2012.41.55%20PM.png

change the command to plan from drop down and also select azure subscription from drop down

Screenshot%202023-09-05%20at%2012.57.04%20PM.png

Add Terraform task for apply

Screenshot%202023-09-05%20at%2012.41.55%20PM.png

change the command to apply from drop down and also select azure subscription from drop down

Screenshot%202023-09-05%20at%201.00.49%20PM.png

Now verify to make sure if we have correct values in each/every task. Now click on Save + Queue.

Screenshot%202023-09-05%20at%201.10.51%20PM.png

Click on Save and Run

Screenshot%202023-09-05%20at%201.15.49%20PM.png

This confirms that pipeline have successfully created resources in Azure cloud. You can login to Azure portal to see the resources – app service plan and web app.

Screenshot%202023-09-05%20at%201.17.50%20PM.png

You can also verify terraform state info which has resources entry for all the resources created

Click on Containers, mytfstatecontainer

Screenshot%202023-09-06%20at%2012.34.28%20PM.png

Click on terraform.tfstate

Screenshot%202023-09-06%20at%2012.36.23%20PM.png

Click on Edit to view the content of terraform state file

Screenshot%202023-09-06%20at%2012.36.38%20PM.png

Clean up resources created in Azure using the pipeline – destroy command

change the command to destroy instead of apply from the drop down

Screenshot%202023-09-06%20at%2012.39.37%20PM.png

You can either add a new task for destroy or modify to destroy from apply in the existing task.

Now save the pipeline and run the pipeline. check the output of destroy task

Screenshot%202023-09-06%20at%2012.44.35%20PM.png

Check terraform.tfstate file after destroy.

Screenshot%202023-09-06%20at%2012.45.58%20PM.png

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK