The goal of this README and accompanying code is demonstrate how to automate Terraform (IaC tool) using GitHub Actions (CI/CD) to create an Azure Storage Account.
Note: You could easily modify the Terraform script to deploy infrastructure across any cloud provider. For the purpose of this example, I am using Azure.
- Example using OpenTofu as IaC tool
- Use of security scanning tools overtop CI/CD
- Example of policy enforcement around IaC within CI/CD
- Azure CLI
- Azure Account
- Development environment, I am using VSCode as it has best in class extensions for GitHub and Azure
- GitHub - fork this repo (or clone and grab required files)
.github/workflows/terraform-orchestrator.ymldocs/terraform
- Login to Azure with
az login- it will open a browser - Create a Resource Group
az group create --name "<NAME>" --location "West US" - Get SubscriptionId of Resource Group
az account list --output table - Create a Service Principal
az ad sp create-for-rbac --name "<NAME>" --role Contributor --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>
Make sure to save the credentials that are shown after the creation - DO NOT COMMIT TO VERSION CONTROL! - Create GitHub Secrets for the following:
ARM_CLIENT_ID = appId in step4 ARM_CLIENT_SECRET = password in step4 ARM_SUBSCRIPTION_ID ARM_TENANT_ID = tenant in step4 - Modify
resourcesection ofdocs/terraform/main.tf- use the resource group name you created in step2 and give the storage account a name - Push code up to your repository
- Since the
terraform-orchestrator.ymlis setup to be run as a workflow dispatch (i.e. gui) it needs to be merged to main to see the option under the Actions tab in the GitHub portal. Once this is done you will see something like below and can launch the job.
- Navigate to the Azure Portal and confirm the Resource Group created in step2 contains the new storage account
main.tf- this file defines the Terraform. In this case it says to create an Azure Storage Account. This could contain additional infrastructure to create or could call other.tffiles to chain together a bunch of work. Refer to hashicorp for templates and syntaxterraform-orchestrator.yml- this is the GitHub action workflow to run our Terraform and create the Azure Storage Account. The key commands here areterraform planandterraform apply -auto-approve. If the plan fails then apply does not go ahead.