Automate Terraform Deployments: (2/2) Process¶
This reusable GitHub Actions workflow is invoked by the Detect Changed terraform.tfvars Files workflow. It processes one
.tfvars
file at a time, performing:
- Terraform environment setup
- Validation, linting, and security scanning
- Cost estimation
- Conditional
apply
ordestroy
actions based on CI mode
This modular design ensures targeted, environment-specific deployments to Azure.
Purpose¶
- Isolate processing for each changed
.tfvars
file. - Automate Terraform lifecycle (plan, validate, apply/destroy).
- Integrate security and cost checks into the deployment pipeline.
- Support multiple environments without affecting unrelated infrastructure.
Trigger Conditions¶
This workflow is not triggered directly by pushes or PRs.
It is invoked via workflow_call
from another workflow, with:
Inputs¶
Name | Type | Required | Description |
---|---|---|---|
tfvars_file |
string | ✅ | Path to the .tfvars file to process |
Secrets¶
Secret Name | Purpose |
---|---|
GRINNTEC_TERRAFORM_DEPLOYMENTS_AZURE_PAT |
GitHub token for private module access |
AZURE_SUBSCRIPTION_ID |
Azure subscription ID |
AZURE_TENANT_ID |
Azure tenant ID |
AZURE_CLIENT_ID |
Azure service principal client ID |
INFRACOST_API_KEY |
API key for Infracost cost estimation |
Concurrency Control¶
- Ensures only one run per.tfvars
file at a time.
- Prevents race conditions in state management.
Workflow Logic¶
-
Azure Login (OIDC)
- Authenticates to Azure without storing credentials.
- Uses azure/login@v1.
-
Checkout Repository
- Full history checkout for Terraform context.
-
Show Working File & Directory
- Logs the .tfvars file path and directory for traceability.
-
Read CI Mode
- Determines if the run should apply, destroy, or just plan.
-
Configure Git for Private Modules
- Ensures Terraform can pull private GitHub modules.
-
Cache Terraform Providers
- Speeds up runs by caching provider binaries.
-
Set Up Terraform
- Installs Terraform CLI.
-
Ensure Lockfile Exists
- Guarantees provider versions are pinned.
-
Extract Key Variables
- Reads app_name, env, and location from .tfvars.
-
Create Backend Config File
- Configures remote state in Azure Blob Storage.
-
Terraform Format
- Enforces formatting standards.
-
Run TFLint
- Lints Terraform code for best practices.
-
Terraform Validate
- Validates syntax and configuration.
-
Terraform Plan
- Generates execution plan.
-
Security Scan (Checkov)
- Detects misconfigurations and security risks.
-
Cost Estimation (Infracost)
- Estimates monthly costs of planned changes.
-
Terraform Apply (if CI mode = apply)
- Applies changes to Azure.
-
Terraform Destroy (if CI mode = destroy)
- Tears down infrastructure.
Security Considerations¶
- OIDC Authentication: No static Azure credentials stored.
- Scoped Secrets: Only required secrets are passed.
- Private Module Access: PAT is scoped to necessary repos.
- State Isolation: Backend config ensures environment separation.
Key Components¶
Component | Description |
---|---|
.github/actions/show-file |
Displays file path and directory |
.github/actions/read-ci-mode |
Reads deployment mode from .tfvars |
.github/actions/configure-git-private-modules |
Configures Git for private Terraform modules |
.github/actions/cache-terraform-providers |
Caches provider binaries |
.github/actions/ensure-tf-lockfile-exists |
Ensures lockfile is present |
.github/actions/extract-tfvars |
Extracts key variables from .tfvars |
.github/actions/create-terraform-backend-config-file |
Creates backend config for remote state |
.github/actions/terraform-fmt |
Formats Terraform code |
.github/actions/tflint |
Lints Terraform code |
.github/actions/terraform-validate |
Validates Terraform configuration |
.github/actions/terraform-plan |
Runs Terraform plan |
.github/actions/terraform-security-scan |
Runs Checkov security scan |
.github/actions/terraform-cost-estimate |
Runs Infracost |
.github/actions/terraform-apply |
Applies Terraform changes |
.github/actions/terraform-destroy |
Destroys Terraform-managed resources |
Example Flow¶
- Detect Changed
.tfvars
Files workflow findsenvironments/dev/terraform.tfvars
changed. - Calls this workflow with:
- Workflow runs:
- Validates and scans the configuration.
- Runs cost estimation.
- Applies or destroys resources based on CI mode.
Maintenance Notes¶
- Keep custom actions updated for compatibility with Terraform and Azure changes.
- Rotate secrets regularly.
- Review CI mode logic to ensure safe deployments.
- Update backend config logic if state storage changes.