https://infracost.io logo
#general
Title
# general
p

plain-ocean-92335

12/14/2021, 9:13 AM
Hi all, I appreciate that infracost auto detects terragrunt. But could it also autodetect if there is a already a tf.plan file present? Thus infracosts could skip the creation of the plan file and directly run the
show
command against the current file? We already create a terraform plan file like this:
Copy code
terragrunt run-all plan -out terraform.tfplan
Our goal is to speed up infracost run since it adds 2x terragrunt run time to pipeline
l

little-author-61621

12/14/2021, 9:17 AM
Hi, if you pass the exact plan path in like
--path=/path/to/tf.plan
this should work. Is the issue that you want to do this for multiple plan files at the same time?
p

plain-ocean-92335

12/14/2021, 9:19 AM
Since we use terragrunt we have a multiple projects folder design. I see that we could create a config file for this structure, but I would love to skip this management overhead.
l

little-author-61621

12/14/2021, 9:20 AM
Are you running this in GitHub actions/GitLab CI or anything else?
p

plain-ocean-92335

12/14/2021, 9:20 AM
Github Actions
I guess this is a feature request to autodetect terragrunt and if there is a tfplan file next to the terragrunt.hcl?
s

stocky-salesmen-15167

12/14/2021, 9:23 AM
@plain-ocean-92335 So you have multiple folders which do each have a
terraform.tfplan
? Which would mean you would need to create a config file like here https://www.infracost.io/docs/multi_project/config_file/, right?
p

plain-ocean-92335

12/14/2021, 9:24 AM
@stocky-salesmen-15167 yes. AFAIK this is the only way to pass in multiple tfplan files.
s

stocky-salesmen-15167

12/14/2021, 9:25 AM
You could probably work around with some intermediate step and a little bash script to โ€œauto-generateโ€ the config file.
p

plain-ocean-92335

12/14/2021, 9:26 AM
Simply put, I would like to skip the
terragrunt run-all plan
step which
infracost breakdown
action does, since I already have one present
l

little-author-61621

12/14/2021, 9:26 AM
The other option just now might be to do something like we used to do before we had native Terragrunt support: https://github.com/infracost/infracost/blob/master/scripts/terragrunt/breakdown_all.sh Basically, the idea is to find the plan files, and run
infracost breakdown
for each of them, and then combine them using
infracost output
.
What does your existing GitHub action look like?
p

plain-ocean-92335

12/14/2021, 9:32 AM
Copy code
name: Terraform Validate dev

on:
  pull_request:
    paths:
      - ".terra*-version"
      - "terraform/**"
      - ".github/workflows/*-dev.yml"
      - "scoma/**"
    branches:
      - develop

defaults:
  run:
    shell: bash

env:
  AWS_REGION: "eu-central-1"
  ACCOUNT: scoma-dev
  CONFIG_PATH: terraform/scoma
  AWS_ROLE_TO_ASSUME: ${{ secrets.AWS_ROLE_TERRAFORM_DEV }}
  XRAY_ENABLED: ${{ github.event.inputs.x-ray }}
  TERRAGRUNT_PARALLELISM: 4 # limit CPU usage

jobs:
  terraform-validate:
    name: TF code in scoma-dev
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
      - name: Set variables
        run: |
          TF_VER=$(cat .terraform-version)
          TG_VER=$(cat .terragrunt-version)
          echo "TERRAFORM_VERSION=$TF_VER" >> $GITHUB_ENV
          echo "TERRAGRUNT_VERSION=$TG_VER" >> $GITHUB_ENV
      # Setup terraform
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v1.3.2
        with:
          terraform_version: ${{ env.TERRAFORM_VERSION }}
          terraform_wrapper: false
      - name: Terragrunt Binary Installer Action
        uses: autero1/action-terragrunt@v1.0.1
        with:
          terragrunt_version: ${{ env.TERRAGRUNT_VERSION }}
      - name: Config Terraform plugin cache
        run: |
          TF_PLUGIN_CACHE_DIR="$HOME/.terraform.d/plugin-cache"
          echo "TF_PLUGIN_CACHE_DIR=$TF_PLUGIN_CACHE_DIR" >> $GITHUB_ENV
          mkdir --parents $TF_PLUGIN_CACHE_DIR
          TERRAGRUNT_DOWNLOAD="$HOME/.terragrunt-cache/${{ env.ACCOUNT }}/$(date +%F)"
          echo "TERRAGRUNT_DOWNLOAD=$TERRAGRUNT_DOWNLOAD" >> $GITHUB_ENV
          mkdir --parents $TERRAGRUNT_DOWNLOAD
      - name: Cache Terraform
        uses: actions/cache@v2
        with:
          path: |
            ~/.terraform.d/plugin-cache
          key: ${{ runner.os }}-terraform-${{ hashFiles('**/.terraform.lock.hcl') }}
      - name: Cache Terragrunt
        uses: actions/cache@v2
        with:
          path: |
            ~/.terragrunt-cache/
          key: ${{ runner.os }}-${{ env.TERRAGRUNT_DOWNLOAD }}-${{ hashFiles('**/project.hcl', '**/.terraform.lock.hcl') }}
      - name: Configure AWS Credentials
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ env.AWS_REGION }}
          role-to-assume: ${{ env.AWS_ROLE_TO_ASSUME }}
          role-duration-seconds: 1800
          role-session-name: scoma-${{ github.run_id }}
      - name: Plan and validate for ${{ env.ACCOUNT }}
        run: |
          scripts/terragrunt/init_terraform_plugins.sh
          source scripts/terragrunt/ssh-agent.source
          scripts/terragrunt/run_terragrunt run-all validate
          scripts/terragrunt/run_terragrunt run-all plan -out terraform.tfplan
        env:
          SSH_PRIVATE_KEY: ${{ secrets.GIT_SSH_PRIVATE_KEY }}
      - name: Setup Infracost
        uses: infracost/actions/setup@v1
        with:
          api-key: ${{ secrets.INFRACOST_API_KEY }}
      - name: Run Infracost
        run: infracost breakdown --path=${{ env.CONFIG_PATH}} --format=json --out-file=/tmp/infracost.json
      - name: Post Infracost comment
        uses: infracost/actions/comment@v1
        with:
          path: /tmp/infracost.json
          behavior: update # Create a single comment and update it.
s

stocky-salesmen-15167

12/14/2021, 9:35 AM
Just as an idea to generate the config file on the fly with a script like this.
Copy code
#!/usr/bin/env bash

cfgfile="/tmp/infracost.yml"

# write the infracost config file header
echo -e "version: 0.1\n\nprojects:\n" > $cfgfile

# Loop through plans and create Infracost config file
planfiles=($(find . -name "terraform.tfplan" | tr '\n' ' '))
for planfile in "${planfiles[@]}"; do
  echo "Adding $planfile to infracost config";
  echo "  - path: $planfile" >> $cfgfile
done

# export config file name to Github env
# use it in the next step as env.infracost_configfile
echo "infracost_configfile=$cfgfile" >> $GITHUB_ENV
You can then call infracost with ${{env.infracost_configfile}} as path to the config file.
๐Ÿ™Œ 1
p

plain-ocean-92335

12/14/2021, 9:38 AM
Yeah, that would work.
s

stocky-salesmen-15167

12/14/2021, 9:39 AM
(no warranty , not tested ๐Ÿ˜‰ )
p

plain-ocean-92335

12/14/2021, 9:41 AM
But would be nicer if I could tell infracosts the name of the plan files and it auto generates this config under the hood :d
๐Ÿ‘ 2
l

little-author-61621

12/14/2021, 9:44 AM
Yeah I think this is worthy of opening a GitHub issue for. I wonder if the
--path
parameter could accept some sort of glob pattern or something
๐Ÿ‘ 2
@plain-ocean-92335 once you've opened the issue ping the link here please. Or if you want me to open it let me know.
p

plain-ocean-92335

12/14/2021, 12:02 PM
I would appreciate if you could open it please ๐Ÿ˜„
๐Ÿ‘ 1
l

little-author-61621

12/14/2021, 12:33 PM
15 Views