gifted-sandwich-69781
08/29/2024, 6:14 PMjobs:
# Run Infracost on pull requests
- job: infracost_pull_request_checks
displayName: Run Infracost on pull requests
pool:
vmImage: ubuntu-latest
steps:
- bash: |
curl -fsSL <https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh> | sh
infracost configure set api_key ${{ parameters.infracostApiKey }}
displayName: Setup Infracost
- bash: |
git clone $(Build.Repository.Uri) --branch=$(System.PullRequest.TargetBranchName) --single-branch /tmp/base --config http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)"
displayName: Checkout Base Branch
- bash: |
infracost breakdown --path=. \
--format=json \
--out-file=/tmp/infracost-base.json
displayName: Generate Infracost Cost Estimate Baseline
workingDirectory: /tmp/base/${{ parameters.terraformDirectory }}
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
- bash: |
infracost diff --path=. \
--format=json \
--compare-to=/tmp/infracost-base.json \
--out-file=/tmp/infracost.json
displayName: Generate Infracost Diff
workingDirectory: $(System.DefaultWorkingDirectory)/${{ parameters.terraformDirectory }}
env:
SYSTEM_ACCESSTOKEN: $(System.AccessToken)
My repo layout is like this:
root
--> terraform/main.tf
--> terraform/(dev|qa|uat|prd).tfvarswhite-airport-8778
infracost breakdown --path terraform
locally on your machine, does it show something like this in the output? That’ll tell us if the auto-detect is working ok or if we need to add a config file to tell the CLI what the projects are.
INFO Autodetected 4 Terraform projects across 1 root module
INFO Found Terraform project dev, qa, uat, prd at directory terraform
gifted-sandwich-69781
08/29/2024, 6:49 PMINFO Autodetected 4 Terraform projects across 1 root module
INFO Found Terraform project main-dev at directory . using Terraform var files dev.tfvars
INFO Found Terraform project main-prd at directory . using Terraform var files prd.tfvars
INFO Found Terraform project main-qa at directory . using Terraform var files qa.tfvars
INFO Found Terraform project main-uat at directory . using Terraform var files uat.tfvars
Unfortunately changing my Azure Devops pipeline to use the passed in terraform directory (terraform) (and adjust the workingDirectory for the bash commands) I get the following output for Generate Baseline:
2024-08-29T18:46:54Z INFO Autodetected 4 Terraform projects across 1 root module
2024-08-29T18:46:54Z INFO Found Terraform project "main-dev" at directory "." using Terraform var files "dev.tfvars"
2024-08-29T18:46:54Z INFO Found Terraform project "main-prd" at directory "." using Terraform var files "prd.tfvars"
2024-08-29T18:46:54Z INFO Found Terraform project "main-qa" at directory "." using Terraform var files "qa.tfvars"
2024-08-29T18:46:54Z INFO Found Terraform project "main-uat" at directory "." using Terraform var files "uat.tfvars"
2024-08-29T18:46:55Z INFO Output saved to /tmp/infracost-base.json
And the Generate Diff I get:
2024-08-29T18:46:56Z INFO Autodetected 4 Terraform projects across 1 root module
2024-08-29T18:46:56Z INFO Found Terraform project "main-dev" at directory "." using Terraform var files "dev.tfvars"
2024-08-29T18:46:56Z INFO Found Terraform project "main-prd" at directory "." using Terraform var files "prd.tfvars"
2024-08-29T18:46:56Z INFO Found Terraform project "main-qa" at directory "." using Terraform var files "qa.tfvars"
2024-08-29T18:46:56Z INFO Found Terraform project "main-uat" at directory "." using Terraform var files "uat.tfvars"
Error: Invalid --compare-to Infracost JSON, found duplicate project name entity/functions/Event.Publisher/terraform
white-airport-8778
entity/functions/Event.Publisher/terraform
mean anything to you? is there such a path in the repo?gifted-sandwich-69781
08/30/2024, 1:21 PMgifted-sandwich-69781
08/30/2024, 1:23 PMversion: 0.1
projects:
{{- range $project := matchPaths "terraform/:env.tfvars" }}
- path: terraform
name: {{ $project.env }}
terraform_var_files:
- {{ $project.env }}.tfvars
{{- end }}
I was able to get my ADO pipeline into a working state and now I need to add the components for checking out private modules.
Unless you see something overtly wrong in general I'd consider this resolved and I'll just head down the path of setting up my repos to use the tmpl file and then generate config during the infracost analysis from thatwhite-airport-8778
#infracost.yml.tmp
version: 0.1
autodetect:
env_names:
- dev
- qa
- uat
- prd
infracost generate config --repo-path . --template-path infracost.yml.tmp --out-file infracost.yml
infracost breakdown --config-file infracost.yml
If the above works ok locally, you can use that in CI/CD too. If all of your repos have one or many of those 4 envs then you can put that template in the pipeline instead of adding it to every repo.white-airport-8778
gifted-sandwich-69781
08/30/2024, 3:18 PMwhite-airport-8778
terraform init
to work, you can re-use those creds for the Infracost CLI as it uses the same method to download the modules.gifted-sandwich-69781
09/03/2024, 8:15 PMjobs:
- deployment: TerraformApply${{parameters.environment}}
displayName: Terraform Apply ${{ parameters.environment }}
environment: ${{ parameters.environment }}
strategy:
runOnce:
deploy:
steps:
- checkout: self
path: self
- ${{ each repository in parameters.module_repositories }}:
- checkout: ${{ repository }}
persistCredentials: true
- script: |
git config --global http.https://dev.azure.com.extraheader "Authorization: Bearer $(System.AccessToken)"
displayName: Git Authentication
- task: TerraformInstaller@1
inputs:
terraformVersion: ${{ parameters.terraform_version }}
- task: TerraformTaskV4@4.227.24
displayName: Terraform Init
inputs:
provider: azurerm
command: init
backendServiceArm: ${{ parameters.backend_service_connection }}
backendAzureRmResourceGroupName: ${{ parameters.backend_resource_group }}
backendAzureRmStorageAccountName: ${{ parameters.backend_storage_account }}
backendAzureRmContainerName: ${{ parameters.backend_container }}
backendAzureRmKey: ${{ parameters.environment }}.tfstate
workingDirectory: $(Build.Repository.LocalPath)/${{ parameters.terraform_dir }}white-airport-8778
module "
what is the source
param of one of them set to? that’ll tell us how the modules are being accessed (HTTPS or SSH, the following is an SSH example), it might be a mixture of SSH and HTTPS as you also have the git config --global
command set above
> module “my-module” {
> source = “git@github.com:my-org/terraform-private-module.git”
> ref = “v1.2.3”white-airport-8778