Skip to content

A CommentOps-based Terraform Pipeline: From Plan to Apply

Interactive Diagram

μƒˆ νƒ­ β†—
  • Azure CLI login via az login
  • The Terraform provisioning environment uses OIDC-based authentication (via an Azure user-assigned managed identity)
Terminal window
export ARM_CLIENT_ID=<clientId>
export ARM_SUBSCRIPTION_ID=<subscriptionId>
export ARM_TENANT_ID=<tenantId>

Commands run from the directory containing providers.tf. tfvars usage is optional (managed via a digger project on the TACOS tool).

Terminal window
### install the terraform provider
terraform init
### validate and generate the plan
terraform plan
### apply based on the generated plan
terraform apply

In the local environment, we configure a plugin cache so the Terraform provider runs via symbolic links.

Terminal window
> cat ~/.terraformrc
plugin_cache_dir = "~/.terraform.d/plugin-cache/"

Managed with a module + stacks-based structure.

  • digger.yml
  • Directorymodules
    • cache
    • container-registry
    • cosmosdb
    • kubernetes
    • network
    • sql-database
    • sql-server
    • vault
    • vm-ubuntu
  • Directorystacks
    • Directorydev
      • backend.tf
      • configurations
      • data.tf
      • main.tf
      • outputs.tf
      • providers.tf
      • variables.tf

The main flow for deploying infra from a Terraform code repository is as follows.

Infra Provisioning Flow

Using TACOS (Terraform Automation & Collaboration Tools)

Section titled β€œUsing TACOS (Terraform Automation & Collaboration Tools)”

TACOS is a collective term for tools that support collaboration and automation for Terraform.

  • Pull Requests were originally intended for code review and change-history tracking on development features/hotfixes.
  • Integrating a TACOS tool extends this beyond reviewing Terraform code to tracking the history of the actual changes applied.
  • Key items managed:
    • terraform plan execution and its expected results
    • terraform apply execution and what was actually applied
    • (Optional) Estimated cost simulations for infra changes via other APIs
Capability/Toolterraform Cloudterraform EnterpriseScalrEnv0Spacelift
ComplianceISO 27001, SOC 2ISO 27001, SOC 2❓SOC 2ISO 27001, SOC 2
GitLab Integrationβœ…βœ…βœ…βœ…βœ…
HostingSaaSSaaS, On-PremSaaS, On-PremSaaSSaaS
Policy as CodeSentinelSentinelOPAOPAOPA
Pricing ModelUnpredictable in highers tiersStill figuring it outMixedMixedPer capabilities and users
Private Agentsβœ…βœ…βœ…βœ…βœ…
Private Module Registryβœ…βœ…βœ…βœ…βœ… - with CI/CD
RBACβœ…βœ…βœ”οΈ - hierarchicalβœ”οΈ - hierarchicalβœ”οΈ - also extensible with policies
Remote operations CLIβœ…βœ…βœ…βœ…βœ…
Remote operations VCS/GitOpsβœ…βœ…βœ…βœ…βœ…
SLA99.9% for highers tierN/A❓❓❓
SSOβœ… - only in high paid tiersβœ…βœ… - only in high paid tiersβœ… - only in high paid tiersβœ…
Secrets ManagementInternalVault integratedInternalInternal, AWS, GCP, AzureInternal, also file based
Short lived environments supportβŒβŒβŒβœ…βœ…
State Managementβœ…βœ…βœ…βœ”οΈ - only hidden stateβœ… - also external
terraform Providerβœ…βœ…βœ…βœ…βœ…
Webhooksβœ…βœ…βœ…βœ…βœ…

Atlantis integrates tightly with PR workflows on Git solutions like GitHub, and is run self-hosted on a separate K8s environment. It follows the GitHub Flow model.

Atlantis flow

  • Pros
    • You can manage Terraform functionality (Plan, Apply) directly from the GitHub comment thread
    • Only a single MR is allowed at a time, keeping the infra environment consistently managed
  • Cons
    • Because only one MR can exist at a time, having multiple contributors creates a bottleneck
    • Establishing a variety of branching strategies is relatively difficult
    • A separate self-hosted environment is required

Digger is offered as a public service called Digger Cloud, and is mainly used integrated into a GitHub Actions pipeline.

  • Pros
    • Security: supports RBAC via OIDC and OPA, allowing different permissions per user
    • Cost efficiency: reuses your existing CI tooling, requiring no additional compute resources
    • Flexibility: supports both apply-on-merge and the traditional approach
    • CI/backend separation: the CI tool that performs the Terraform work is separate from the orchestrator that coordinates it, so you keep the benefits of your existing CI tool
  • Cons
    • Community size: relatively small user base and reference material compared to Atlantis
    • Performance: since Atlantis processes the entire workflow on its backend, Digger can be somewhat slower for certain tasks
  1. Execution environment
    • Atlantis: runs Terraform on a single VM
    • Digger: uses your existing CI infrastructure, spinning up a new Docker container per job
  2. Security
    • Digger: does not directly access cloud accounts or sensitive state data
    • Atlantis: executes Terraform directly, so it needs broader access permissions
  3. Server management
    • Digger: in most cases, no self-hosting required
    • Atlantis: typically requires maintaining your own server
  4. Cost
    • Digger: cost-efficient by reusing existing CI compute resources
    • Atlantis: requires a dedicated, powerful VM instance
  5. When apply happens
    • Digger: supports both pre-merge and post-merge apply
    • Atlantis: mainly supports pre-merge apply
  6. Access control
    • Digger: flexible RBAC via OPA policies
    • Atlantis: GitHub role-based access control via server options

We ultimately adopted Digger, since it lets us reuse our existing CI infrastructure and removes the burden of maintaining a separate server.

We manage the history of Infra Provisioning Plan/Apply through GitHub Pull-Request-based CommentOps, and integrate the Infracost API to manage cost estimation alongside it.

Built around GitHub-hosted runners and APIs that support public access.

Digger integration for public access

Built around GitHub self-hosted runners and a self-managed Digger backend service.

Digger integration for private access

The Digger workflow is configured via digger.yml. Below is an example that adds an Infracost API call step on top of the base plan/apply workflow.

digger.yml
projects:
- name: platform-infra-dev
dir: stacks/dev
workflow: with-infracost
workflows:
with-infracost:
plan:
steps:
- init
- run: terraform output --raw aks_kube_config > .kubeconfig || true
- plan
- run: infracost breakdown --path=. | tee -a $DIGGER_OUT
apply:
steps:
- init
- run: terraform output --raw aks_kube_config > .kubeconfig || true
- apply
auto_merge: false
pr_locks: true
  • Infra deployment plan validation triggered by opening a Pull Request

    PR-based plan validation

  • Infra deployment execution via Pull Request review and comments

    Comment-based apply execution

Below is an example output from calling the Infracost API against the Terraform code.

Project: main
Name Monthly Qty Unit Monthly Cost
module.kubernetes_module.azurerm_kubernetes_cluster.aks
└─ default_node_pool
β”œβ”€ Instance usage (Linux, pay as you go, Standard_d2as_v5) 730 hours $77.38
└─ os_disk
└─ Storage (P10, LRS) 1 months $19.71
module.cache_redis_module.azurerm_redis_cache.cache_redis
└─ Cache usage (Basic_C0) 1 nodes $20.44
module.cache_redis_module.azurerm_private_endpoint.pe
β”œβ”€ Private endpoint 730 hour $7.30
β”œβ”€ Inbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
└─ Outbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
module.cosmosdb_mongo_module.azurerm_private_endpoint.pe
β”œβ”€ Private endpoint 730 hour $7.30
β”œβ”€ Inbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
└─ Outbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
module.sqlserver_module.azurerm_private_endpoint.pe
β”œβ”€ Private endpoint 730 hour $7.30
β”œβ”€ Inbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
└─ Outbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
module.vault_module.azurerm_private_endpoint.pe
β”œβ”€ Private endpoint 730 hour $7.30
β”œβ”€ Inbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
└─ Outbound data processed (first 1PB) Monthly cost depends on usage: $0.01 per GB
module.acr_app_module.azurerm_container_registry.acr
β”œβ”€ Registry usage (Basic) 30 days $5.00
β”œβ”€ Storage (over 10GB) Monthly cost depends on usage: $0.10 per GB
└─ Build vCPU Monthly cost depends on usage: $0.0001 per seconds
module.acr_base_module.azurerm_container_registry.acr
β”œβ”€ Registry usage (Basic) 30 days $5.00
β”œβ”€ Storage (over 10GB) Monthly cost depends on usage: $0.10 per GB
└─ Build vCPU Monthly cost depends on usage: $0.0001 per seconds
module.cache_redis_module.azurerm_private_dns_zone.pdz
└─ Hosted zone 1 months $0.50
module.cosmosdb_mongo_module.azurerm_private_dns_zone.pdz
└─ Hosted zone 1 months $0.50
module.sqlserver_module.azurerm_private_dns_zone.pdz
└─ Hosted zone 1 months $0.50
module.vault_module.azurerm_private_dns_zone.pdz
└─ Hosted zone 1 months $0.50
module.sqldb_module.azurerm_mssql_database.sqldb
β”œβ”€ Compute (serverless, GP_S_Gen5_2) Monthly cost depends on usage: $0.59 per vCore-hours
β”œβ”€ Storage 2 GB $0.27
β”œβ”€ Long-term retention (LRS) Monthly cost depends on usage: $0.0295 per GB
└─ PITR backup storage (LRS) Monthly cost depends on usage: $0.12 per GB
OVERALL TOTAL $159.00
*Usage costs can be estimated by updating Infracost Cloud settings, see docs for other options.
──────────────────────────────────
27 cloud resources were detected:
βˆ™ 13 were estimated
βˆ™ 14 were free
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Project ┃ Baseline cost ┃ Usage cost* ┃ Total cost ┃
┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━╋━━━━━━━━━━━━┫
┃ main ┃ $159 ┃ - ┃ $159 ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━┻━━━━━━━━━━━━┛

Terraform Graph

Continue with Azure Resource Deployment Patterns with Terraform.