Skip to main content

GitLab CI migration

Follow this page to migrate your Infracost GitLab CI integration to use Infracost v0.10.

If you encounter any issues while migrating, please join our community Slack channel, we'll help you very quickly 😄

What's new?

With the v0.10 release, we'll support two ways to run Infracost with Terraform via --path:

  1. Parsing HCL code (recommended): this is the default and recommended option as it has 5 key benefits. This page describes how you can migrate to this option.

    # Terraform variables can be set using --terraform-var-file or --terraform-var
    infracost breakdown --path /code
  2. Parsing plan JSON file: this will continue to work as before. There are examples here of generating Terraform plan JSON files in GitLab CI and passing them to Infracost.

    cd /code
    terraform init
    terraform plan -out tfplan.binary
    terraform show -json tfplan.binary > plan.json

    infracost breakdown --path plan.json

Infracost GitLab CI migration guide

Changing your workflow to work with the parse HCL option requires the following changes:

  1. Remove the Terraform and Terragrunt dependencies:

    • Delete any stages and jobs that runs terraform or terragrunt, e.g. "terraform init", "terraform plan" and "terraform show" are no longer needed.
    • If you are not using the fetch usage from CloudWatch feature, delete any steps that set cloud credentials.
  2. Bump the version of the Infracost Docker image from infracost/infracost:ci-0.9 to infracost/infracost:ci-0.10:

    stage: infracost
    name: infracost/infracost:ci-0.10
    entrypoint: [""]
  3. Update the infracost job's script to add the following two steps for generating a cost estimate baseline from the main/master branch.

    - git clone $CI_REPOSITORY_URL --branch=$CI_MERGE_REQUEST_TARGET_BRANCH_NAME --single-branch /tmp/base

    - |
    infracost breakdown --path=/tmp/base/${TF_ROOT} \
    --format=json \

    You should replace any --terraform-plan-flags flags with either --terraform-var to add variables or --terraform-var-file to point to var files. These work similarly to Terraform's -var and -var-file flags and can be repeated.


    If you have variables stored on Terraform Cloud/Enterprise Infracost will pull these in automatically if you add the following environment variables to your job:

    # ...
    # Change this if you're using Terraform Enterprise

    If you have a Terraform mono-repo and you want to pass separate variables to each Terraform project you can create a config file and pass that with the --config-file flag as per this example

  4. After the above, add the following two steps for comparing against the Infracost cost estimate baseline. If you added any required variable or config file flags in step 3, also add them to the infracost diff command below.

      - |
    infracost diff --path=${TF_ROOT} \
    --compare-to=infracost-base.json \
    --format=json \

    # Posts a comment in the same way as before
    - |
    infracost comment gitlab --path=infracost.json ...
  5. See our full examples that use the new parsing HCL option. You can find one that is the closest to your use-case and adapt as required.