broad-zoo-34077
12/21/2021, 3:38 PMmysterious-teacher-68276
12/21/2021, 3:45 PMmysterious-teacher-68276
12/21/2021, 3:46 PMmysterious-teacher-68276
12/21/2021, 3:47 PMtfplan
has been lost in between steps - or the tfdir context is incorrectplain-ocean-92335
12/21/2021, 3:50 PM--- # Run Terraform via Terragrunt
# Requires ENV variable ACCOUNT to be set
- name: Run Terraform
env:
ACCOUNT: ${{ env.AWS_ACCOUNT }}
CONFIG_PATH: terraform/${{ env.AWS_ACCOUNT }}
SSH_PRIVATE_KEY: ${{ secrets.GIT_SSH_PRIVATE_KEY }}
run: |-
echo "Add ssh key to access private github repo for terraform modules\n";
source ./scripts/terragrunt/ssh-agent.source;
scripts/terragrunt/init_terraform_plugins.sh
echo "Run Terraform via Terragrunt\n";
scripts/terragrunt/run_terragrunt run-all plan --terragrunt-non-interactive
- name: Create infracost config to reuse terraform.tfplan
run: |
cfgfile="/tmp/infracost-config.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 ${GITHUB_WORKSPACE} -name "terraform.tfplan" | tr '\n' ' '))
for planfile in "${planfiles[@]}"; do
echo "Adding $planfile to infracost config";
echo -e " - path: $planfile\n terraform_binary: terragrunt" >> $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
echo "infracost_comment_tag=$GITHUB_WORKFLOW" >> $GITHUB_ENV
- name: Setup Infracost
uses: infracost/actions/setup@v1
with:
api-key: ${{ secrets.INFRACOST_API_KEY }}
- name: Run Infracost
run: infracost breakdown --config-file=${{ env.infracost_configfile}} --format=json --out-file=/tmp/infracost.json
- name: Post Infracost comment
uses: infracost/actions/comment@v1
with:
path: /tmp/infracost.json
tag: ${{ env.infracost_comment_tag }}
behavior: update # Create a single comment and update it.
plain-ocean-92335
12/21/2021, 3:51 PMplain-ocean-92335
12/21/2021, 3:52 PMterraform {
extra_arguments "plan_args" {
commands = ["plan"]
arguments = concat(
[
"-lock=false" # do not lock on plan - useful in CI to use plan to validate code
],
tobool(get_env("CI", "false")) ? ["-out", "${get_terragrunt_dir()}/terraform.tfplan"] : [] # in github actions autogenerate plan into terragrunt dir to be used by infracosts
)
}
}
plain-ocean-92335
12/21/2021, 3:53 PMmysterious-teacher-68276
12/21/2021, 3:53 PMplain-ocean-92335
12/21/2021, 3:54 PMterraform show …
is not accessing the state.plain-ocean-92335
12/21/2021, 3:54 PMterraform show
has no options to skip state accessmysterious-teacher-68276
12/21/2021, 3:58 PMplain-ocean-92335
12/21/2021, 3:59 PMmysterious-teacher-68276
12/21/2021, 4:02 PMmysterious-teacher-68276
12/21/2021, 4:03 PMCreate infracost config to reuse terraform.tfplan
step you could modify it to something like so:mysterious-teacher-68276
12/21/2021, 4:04 PMplanfiles=($(find ${GITHUB_WORKSPACE} -name "terraform.tfplan" | tr '\n' ' '))
for planfile in "${planfiles[@]}"; do
scripts/terragrunt/run_terragrunt show -json > "${planfile}.json"
echo "Adding $planfile.json to infracost config";
echo -e " - path: $planfile.json\n terraform_binary: terragrunt" >> $cfgfile
done
mysterious-teacher-68276
12/21/2021, 4:04 PMrun_terragrunt
script would be performing something like terrform show
mysterious-teacher-68276
12/21/2021, 4:08 PMscripts/terragrunt/run_terragrunt show -json > "${planfile}.json" --terragrunt-working-dir="$(dirname "$planfile")"
mysterious-teacher-68276
12/21/2021, 4:12 PMplain-ocean-92335
12/21/2021, 4:20 PMinfracosts breakdown
command to use terragrunt?plain-ocean-92335
12/21/2021, 4:21 PMplain-ocean-92335
12/21/2021, 4:22 PMmysterious-teacher-68276
12/21/2021, 4:33 PMinfracosts breakdown
to use terragrunt but it seems in this case that from the run_terragrunt
script we can’t seem to infer the terraform directory of the plan so infracost barfs. I think the most complete solution for this would be to allow you to specify dir context in the config file - but obviously this isn’t done yet (we can open an issue and work on this asap). In the meantime, converting the output to plan.json for the multiproject run should work, but if you have any issues please post again in this threadplain-ocean-92335
12/22/2021, 7:30 AMinfracost breakdown
.
We point infracost to a directory where the terragrunt.hcl and the terraform.plan file exists. If we don’t use the plan file, infracosts autodetects that this is terragrunt.little-author-61621
time="2021-12-21T153131Z" level=info msg="Detected Terraform plan file at /home/runner/work/path/to/terraform.planwhat files are in
/home/runner/work/path/to/
? - trying to work out why we can't find the context dir.plain-ocean-92335
12/22/2021, 1:13 PMterragrunt.hcl
.terraform.lock.hcl. # optional
terraform.plan
The first two files are from repository code, the plan is generated in a github action stepplain-ocean-92335
12/22/2021, 1:14 PM