A template repository to deploy an azure function using terraform and CI/CD
The terraform folder structure is meant to work with multiple environments, hence the inclusion of the terraform "environments" folder. The Github Actions env specific workflow is meant to work hand in hand with the respective terraform environment directory. Furthermore, Azure suggests that we should seperate environments per subscription. As part of the Github Actions workflow, we are passing the subscription id as a secret to select the respective Azure subscription.
Sidenote: If we desire to support multiple environments within the same subscription, it is advisable to generate separate Terraform state files for each environment. Additionally, establishing a resource group for each environment is required.
We can also consider the following folder structure given we want to centralize the terraform scripts into one repository:
tf/
βββ modules/ # Reusable modules
β βββ networking/
β βββ compute/
β βββ storage/
βββ environments/ # Separate environments & shared infra
β βββ dev/
β β βββ main.tf
β β βββ variables.tf
β β βββ backend.tf
β β βββ outputs.tf
β βββ prod/
β β βββ main.tf
β β βββ variables.tf
β β βββ backend.tf
β β βββ outputs.tf
β βββ shared/ # Shared project/subscription for shared resources
β β βββ main.tf # Defines shared resources (e.g., artifact registry)
β β βββ variables.tf
β β βββ backend.tf
β β βββ outputs.tf
βββ global/ # Global configurations like IAM, VPC
β βββ main.tf # Calls networking.tf & security.tf modules
β βββ networking.tf# Defines VPC, subnets, etc.
β βββ security.tf # Defines IAM roles, policies, etc.
β βββ variables.tf # Variables for global resources
β βββ outputs.tf # Outputs for reference
βββ services/ # Specific services using modules
β βββ service-a/
β βββ service-b/
βββ terraform.tfvars # Global default variables (optional)
βββ README.md # Documentation
We can also re-use modules from our centralized repo by pushing the terraform modules onto azure blob storage and referring to them like below:
module "vpc" {
source = "https://mystorageaccount.blob.core.windows.net/terraform-modules/vpc-module.zip"
# Example input variables
region = "us-east-1"
cidr = "10.0.0.0/16"
}