In previous blog, we discussed how to do infrastructure automation while maintaining the Terraform state file in Terraform cloud. But, teams can maintain terraform state file in Azure portal along with other resources. You can learn how to maintain terraform state file in azure blob in this blog.
You can create a resource group, storage account and container in azure to keep the terraform state file. Then add storage container details to Terraform script as follows.
Note the backend section of the provider.tf below which specifies the Azure blob storage and container to maintain the Terraform state. It is required to create the blob storage container manually before trying out the pipeline.
providers.tf
provider "azurerm" {
#version = "~>2.0"
subscription_id = "<<subscription id>>"
features {}
}
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=2.93.1"
}
}
backend "azurerm" {
resource_group_name = "<<state file resource group name>>"
storage_account_name = "<<storage account name>>"
container_name = "<<container name where state file store>>"
key = "terraform.tfstate"
}
}
Pipeline YAML file can be changed as follows.
name: 'Terraform' on: workflow_dispatch: jobs: terraform: name: 'Terraform' runs-on: ubuntu-latest env: ARM_CLIENT_ID: ${{ secrets.AZURE_AD_CLIENT_ID }} ARM_CLIENT_SECRET: ${{ secrets.AZURE_AD_CLIENT_SECRET }} ARM_SUBSCRIPTION_ID: ${{ secrets.AZURE_SUBSCRIPTION_ID }} ARM_TENANT_ID: ${{ secrets.AZURE_AD_TENANT_ID }} steps: # Checkout the repository to the GitHub Actions runner - name: Checkout uses: actions/checkout@v2 # Initialize a new or existing Terraform working directory by creating initial files, loading any remote state, downloading modules, etc. - name: Terraform Init run: terraform init # Generates an execution plan for Terraform - name: Terraform Plan run: terraform plan # deploy resources - name: Terraform Apply run: terraform apply -auto-approve
This blog explained how to maintain terraform state file in azure blob storage.
No comments:
Post a Comment