per account
# general
w
per account
m
hey @worried-soccer-67640 👋
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.
w
ok
here is my current atlantis workflow fyi
Copy code
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
the example on this page: https://www.infracost.io/docs/features/config_file/ indicates that you support setting a profile per folder
m
yep, that’s correct, I just had a sneaky suspicion we’d removed this. But, it seems as though this is still supported
w
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
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
for example^
with many variations of ec2/eks and the region.
m
the
path
field accepts both a terraform dir or plan JSON.
if I understand correctly you want Infracost to read the plan JSON and use a different regions to estimate prices?
w
yeah understood
does what i'm trying to do make sense?
m
yes it makes sense
w
im hoping i can call:
Copy code
/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.
but that means having to declare the hundreds of folders in my repo
m
you can just duplicate the keys with different envs
w
it would be cool if the
path
field in the config field accepted wildcards like
aws/accounts/nonprod/*
@mysterious-teacher-68276 how do you mean?
m
Copy code
- path: aws/accounts/prod
    env:
      AWS_PROFILE: infracost-prod
 - path: aws/accounts/prod
    env:
      AWS_PROFILE: infracost-stag
but i’m not sure this is what you want
you basically want to set a wildcard on the folder
which I don’t believe is supported
as we don’t have project autodetection setup for the
path
config value
w
ok, but if i find some automated way to generate all the path keys, it should work right?
m
yes it should do
FYI @worried-soccer-67640 , 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.
w
just wrote this:
Copy code
#!/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.
m
👍
w
yep, the above works
to generate a config yaml