Terraform deployment with Github actions

Our Cloud School
6 min readJul 14, 2023

In this blog I will share the template to deploy and manage azure infrastructure with terraform

Photo by Linus Mimietz on Unsplash

Lets say I have following azure infra code written in terraform and I would like to deploy this from github actions workflow


terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.64.0"
}
azuread = {
version = "=2.36.0"
}
}
}


provider "azurerm" {
features {
resource_group {
prevent_deletion_if_contains_resources = false
}
key_vault {
purge_soft_delete_on_destroy = true
}

api_management {
purge_soft_delete_on_destroy = true
}
}
}

resource "azurerm_resource_group" "rg" {
name = "rg-demo-01"
tags = {}
location = "uksouth"
}

Github Action/Workflow: You can define the pipeline in github action inside your repository.

To create an action you need to create a yaml (or yml) file inside you repository (.github/workflow/myaction.yml or .github/workflow/deployment.yaml)

Terraform deployment pipeline:

Here is the pipeline code:

Triggers:

first you are going to add a trigger point of pipeline

name: 'Terraform Deployment'

on:
pull_request:
types:
- opened
- edited
- reopened
- synchronize
push:
branches:
- main
- '!**/readme.md'
- 'source/**'

Here 👆we are adding trigger to run pipeline on pull request of in addition/modification of code on specific folder location.

workflow_call:

You also need ` workflow_call` in case if want you pipeline to run manually.

Jobs:

You can add one or more jobs in the job collection node.

jobs:
terraform-plan:
name: 'Terraform Plan'
runs-on: ${{ inputs.pipeline_agent }}
defaults:
run:
working-directory: ${{ inputs.working_directory }}
shell: bash

you 👆 specify the job attributes like

runs-on: which agent you want use to run you job (windows, linux, github managed or…

--

--

Our Cloud School

Rakesh Suryawanshi — Azure Solutions Architect, DevOps with Terraform