Deploying Terraform in Azure using GitHub Actions Step by Step
--
GitHub Actions is a CI/CD (continuous integration and continuous delivery) platform that allows us to automate our build, test, and deployment pipeline right in our repository.
In this story, we will learn how to set up GitHub Actions to deploy Terraform code in Azure.
1. Prerequisites
This is the list of prerequisites required to create a DevOps pipeline:
- Azure Subscription: If you don’t have an Azure subscription, create a free account at https://azure.microsoft.com before you start.
- Azure Service Principal (SPN): is an identity used to authenticate to Azure. See below (Point #2) for instructions to create one.
- Azure Remote Backend for Terraform: we will store our Terraform state file in a remote backend location. We will need a Resource Group, Azure Storage Account, and a Container. Check Point #3 to learn how to create the storage account for Terraform state.
- GitHub Account and GitHub Repository: we need a GitHub Account to create the GitHub Repository and GitHub Actions.
2. Prerequisite: Creating an Azure Service Principal
Using a Service Principal, also known as SPN, is a best practice for DevOps or CI/CD environments.
First, we need to authenticate to Azure. To authenticate using Azure CLI, we type:
az login
The process will launch the browser, and we will be ready to go after the authentication is complete.
We will use the following command to get the list of Azure subscriptions:
az account list --output table
Next, we can select the subscription using the following command (both subscription id and subscription name are accepted):
az account set --subscription <Azure-SubscriptionId>
Then create the principal service account using the following command: