Title
#general
g

Gurpal Singh

09/13/2022, 4:55 PM
per account
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 4:55 PM
hey @Gurpal Singh 👋
4:56 PM
using a config file with a
breakdown
cmd is possible, however, I don’t think it’s currently possible to set the AWS profile per project. Let me double-check.
g

Gurpal Singh

09/13/2022, 4:56 PM
ok
4:56 PM
here is my current atlantis workflow fyi
workflows:
  terragrunt:
    plan:
      steps:
        - env:
            name: INFRACOST_OUTPUT
            command: 'echo "/tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM/$WORKSPACE-${REPO_REL_DIR//\//-}-infracost.json"'
        - env:
            name: TERRAGRUNT_TFPATH
            command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
        - run: direnv-allow-all
        - run: direnv exec . terragrunt plan -input=false -out=$PLANFILE
        - run: direnv exec . terragrunt show -json $PLANFILE > $SHOWFILE
        - run: |
            if [ ! -d "/tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM" ]; then
              mkdir -p /tmp/$BASE_REPO_OWNER-$BASE_REPO_NAME-$PULL_NUM
            fi
            
            /tmp/infracost breakdown --path=$SHOWFILE \
                                --format=json \
                                --log-level=info \
                                --out-file=$INFRACOST_OUTPUT
    apply:
      steps:
        - env:
            name: TERRAGRUNT_TFPATH
            command: 'echo "terraform${ATLANTIS_TERRAFORM_VERSION}"'
        - run: direnv-allow-all
        - run: direnv exec . terragrunt apply -input=false $PLANFILE
4:58 PM
the example on this page: https://www.infracost.io/docs/features/config_file/ indicates that you support setting a profile per folder
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 5:00 PM
yep, that’s correct, I just had a sneaky suspicion we’d removed this. But, it seems as though this is still supported
g

Gurpal Singh

09/13/2022, 5:02 PM
i just realised that
path
is not a filter, and is actually a relative path to a planfile. what i want is a way to say, any plan file under
aws/accounts/nonprod
will use X as the AWS_PROFILE and anything under
aws/accounts/prod
will use something else
5:03 PM
my folder structure is like the below.
aws/accounts/nonprod/eu-west-1/eks/terragrunt.hcl
aws/accounts/prod/ap-northeast-1/ec2/terragrunt.hcl
5:03 PM
for example^
5:03 PM
with many variations of ec2/eks and the region.
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 5:07 PM
the
path
field accepts both a terraform dir or plan JSON.
5:07 PM
if I understand correctly you want Infracost to read the plan JSON and use a different regions to estimate prices?
g

Gurpal Singh

09/13/2022, 5:08 PM
yeah understood
5:08 PM
does what i'm trying to do make sense?
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 5:08 PM
yes it makes sense
g

Gurpal Singh

09/13/2022, 5:09 PM
im hoping i can call:
/tmp/infracost breakdown --path=$SHOWFILE \
                                --format=json \
                                --log-level=info \
                                --out-file=$INFRACOST_OUTPUT
and have a config yaml that determines which AWS_PROFILE to use depending on the folder it's in.
5:09 PM
but that means having to declare the hundreds of folders in my repo
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 5:10 PM
you can just duplicate the keys with different envs
g

Gurpal Singh

09/13/2022, 5:10 PM
it would be cool if the
path
field in the config field accepted wildcards like
aws/accounts/nonprod/*
5:10 PM
@Hugo (Infracost) how do you mean?
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 5:10 PM
- path: aws/accounts/prod
    env:
      AWS_PROFILE: infracost-prod
 - path: aws/accounts/prod
    env:
      AWS_PROFILE: infracost-stag
5:11 PM
but i’m not sure this is what you want
5:11 PM
you basically want to set a wildcard on the folder
5:11 PM
which I don’t believe is supported
5:11 PM
as we don’t have project autodetection setup for the
path
config value
g

Gurpal Singh

09/13/2022, 5:14 PM
ok, but if i find some automated way to generate all the path keys, it should work right?
Hugo (Infracost)

Hugo (Infracost)

09/13/2022, 5:14 PM
yes it should do
5:40 PM
FYI @Gurpal Singh , this section https://www.infracost.io/docs/troubleshooting/#terragrunt shows how the config file can be auto-generated for terragrunt. You could tweak this for your specific use case.
g

Gurpal Singh

09/13/2022, 11:27 PM
just wrote this:
#!/bin/bash
echo -e "version: 0.1\n\nprojects:" > infracost-generated.yml

ACCOUNTS=( prod nonprod shared connectivity )

process(){
  for i in $(find aws/accounts/$1 -name "*.hcl" | grep -v "terragrunt-cache" ); do
      x=$(dirname $i)
      echo -e "  - path: $x" >> infracost-generated.yml
      echo -e  "    env:" >> infracost-generated.yml
      echo -e  "      AWS_PROFILE: infracost-$1\n" >> infracost-generated.yml
  done
}

for a in "${ACCOUNTS[@]}"; do
  process $a
done
will test it out tomorrow.
Hugo (Infracost)

Hugo (Infracost)

09/14/2022, 9:05 AM
👍
g

Gurpal Singh

09/14/2022, 2:07 PM
yep, the above works
2:07 PM
to generate a config yaml