This repo shows how Infracost can be used with Atlantis. It automatically adds a comment to the bottom of Atlantis' output showing the cost estimate difference. See this pull-request for a demo, expand the Show Output sections and scroll down to see the Infracost output.
This integration uses the latest version of Infracost by default as we regularly add support for more cloud resources. If you run into any issues, please join our community Slack channel; we'd be happy to guide you through it.
As mentioned in our FAQ, no cloud credentials or secrets are sent to the Cloud Pricing API. Infracost does not make any changes to your Terraform state or cloud resources.
There are two methods of integrating Infracost with Atlantis:
Use a custom Docker image that extends Atlantis' latest image to add Infracost. This is the recommended method.
Send the $PLANFILE from Atlantis to the Infracost API with curl. Whilst this API deletes files from the server after they are processed, it is a good security practice to remove secrets from the file before sending it to the API. For example, AWS provides a grep command that can be used to do this.
This method runs infracost diff using the $PLANFILE that Atlantis generates. The following steps describe how you can use this method:
This Docker image extends the Atlantis image by adding the Infracost CLI and the infracost_atlantis_diff.sh script. If you already use a custom Docker image for Atlantis, copy the RUN commands from this Dockerfile into your Dockerfile.
Update your Atlantis configuration to add a custom command that runs Infracost with the required environment variables, such as INFRACOST_API_KEY. The available environment variables are describe in the next section. The following example shows how this can be done, a similar thing can be done with the Atlantis yaml configs in either the Server Config file or Server Side Repo Config files.
docker run infracost/infracost-atlantis:latest server \
--gh-user=MY_GITHUB_USERNAME \
--gh-token=MY_GITHUB_TOKEN \
--gh-webhook-secret=MY_GITHUB_WEBHOOK_SECRET \
--repo-allowlist='github.com/myorg/*' \
--repo-config-json='
{
"repos": [
{
"id": "/.*/",
"workflow": "terraform-infracost"
}
],
"workflows": {
"terraform-infracost": {
"plan": {
"steps": [
"init",
"plan",
{
"env": {
"name": "INFRACOST_API_KEY",
"value": "MY_API_KEY"
}
},
{
"env": {
"name": "INFRACOST_TERRAFORM_BINARY",
"command": "echo \"terraform${ATLANTIS_TERRAFORM_VERSION}\""
}
},
{
"run": "/home/atlantis/infracost_atlantis_diff.sh"
}
]
}
}
}
}
'
Infracost generates the Terraform Plan JSON from the Atlantis $PLANFILE (Terraform binary file format). Use the following steps instead of the above steps if you'd rather do that before Infracost runs. The infracost_atlantis_diff.sh script checks for a $PLANFILE.json file before checking for $PLANFILE.
- run: terraform show -no-color -json $PLANFILE > $PLANFILE.json
- run: /home/atlantis/infracost_atlantis_diff.sh
- run: rm -rf $PLANFILE.json
Send a new pull request to change something in Terraform that costs money; a comment should be posted on the pull request by Atlantis, expand the Show Output section, at the bottom of which you should see the Infracost output. Set the atlantis_debug=true environment variable and see this page if there are issues.
This section describes the required environment variables. Other supported environment variables are described in the this page.
Terragrunt users should also read this page. Terraform Cloud/Enterprise users should also read this page.
INFRACOST_API_KEYRequired To get an API key download Infracost and run infracost register.
INFRACOST_TERRAFORM_BINARYRequired Used to change the path to the terraform binary with the current version, should be set to the path of the Terraform or Terragrunt binary being used in Atlantis (Infracost works with Terraform v0.12 and above). If you're using the infracost/infracost-atlantis image (which is based on the runatlantis/atlantis image), you can set this to:
/usr/local/bin/terraform0.12.30.<data-dir>/bin/terraform<version>. Terragrunt users could use 'echo "/atlantis-data/bin/terraform${ATLANTIS_TERRAFORM_VERSION}"'.usage_fileOptional Path to Infracost usage file that specifies values for usage-based resources, see this example file for the available options.
config_fileOptional If you need to set the Terraform version on a per-repo basis, you can define that in a config file and set this input to its path. In such cases, the usage_file input cannot be used and must be defined in the config-file too.
post_conditionOptional A JSON string describing the condition that triggers pull request comments, can be one of these:
'{"has_diff": true}': only post a comment if there is a diff. This is the default behavior.'{"always": true}': always post a comment.'{"percentage_threshold": 0}': absolute percentage threshold that triggers a comment. For example, set to 1 to post a comment if the cost estimate changes by more than plus or minus 1%.show_skippedOptional Show unsupported resources, some of which might be free (default is false).
SLACK_WEBHOOK_URLOptional Set this to also post the pull request comment to a Slack Webhook, which should post it in the corresponding Slack channel.
atlantis_debugOptional Enable debug mode in infracost_atlantis_diff.sh so it shows the steps being run in the Atlantis pull request comment (default is false).
MY_API_KEY to your Infracost API key. A similar thing can be done with the Atlantis yaml configs in either the Server Config file or Server Side Repo Config files. Optionally add a step to remove secrets from the plan JSON file before sending it to the API.docker run infracost/infracost-atlantis:latest server \
--gh-user=MY_GITHUB_USERNAME \
--gh-token=MY_GITHUB_TOKEN \
--gh-webhook-secret=MY_GITHUB_WEBHOOK_SECRET \
--repo-allowlist='github.com/myorg/*' \
--repo-config-json='
{
"repos": [
{
"id": "/.*/",
"workflow": "terraform-infracost"
}
],
"workflows": {
"terraform-infracost": {
"plan": {
"steps": [
"init",
"plan",
{
"run": "terraform show -json $PLANFILE > $PLANFILE.json"
},
{
"run": "echo \"#####\" && echo && echo Infracost output:"
},
{
"run": "curl -s -X POST -H \"x-api-key: MY_API_KEY\" -F \"ci-platform=atlantis\" -F \"terraform-json-file=@$PLANFILE.json\" -F \"no-color=true\" https://pricing.api.infracost.io/terraform-json-file"
},
{
"run": "rm -rf $PLANFILE.json"
}
]
}
}
}
}
'
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Content type
Image
Digest
sha256:5359162c8…
Size
93.5 MB
Last updated
11 days ago
docker pull infracost/infracost-atlantis