Infracost distinguishes the price of a resource from its cost. Price is the per-unit price published by a cloud vendor. The cost of a resource is calculated by multiplying its price by its usage. For usage-based resources such as AWS Lambda or Google Cloud Storage, Infracost will show prices:
Infracost enables you to describe usage estimates in a file called
infracost-usage.yml, which can be passed to Infracost using the
--usage-file option so it can calculate costs. This flag should not be confused with the
--config-file option that is used to configure how Infracost runs.
Instead of using cloud vendor cost calculators, spreadsheets or wiki pages, you can check-in usage estimates into git alongside other code, get cost estimates from them, and adjust them when needed. This enables quick "what-if" analysis to be done too; for example, what happens to the cost estimate if a Lambda function gets 2x more requests.
Follow these simple steps to use this feature:
--sync-usage-file option to generate a new usage file or update an existing one. This option is a safe sync: it adds any missing resources (with zeros for the usage estimates), it does not overwrite any lines that you have changed in the YAML, and it deletes any resources that are not used in the Terraform project.
As of Infracost CLI
v0.9.8, we're experimenting with fetching the following usage file values from CloudWatch when
--syng-usage-file is used (falling back to using 0). If the CLI can fetch the following values from CloudWatch, it will overwrite them in the usage file. Please contact us if you find this useful or have feedback.
aws_dynamodb_table: data storage, read capacity and write capacity units
aws_lambda_function: function duration and requests
Edit the generated usage file with your usage estimates, for example a Lambda function can have the following parameters. You can check-in usage estimates into git alongside other code, get cost estimates from them, and adjust them when needed.
infracost breakdown or
infracost diff with the usage file to see monthly cost estimates:
The reference file infracost-usage-example.yml contains the list of all of the available parameters and their descriptions.
Usage for resources inside modules can be specified using the full path of the resource. This is the same value as Infracost outputs in the Name column, for example:
The wildcard character
[*] can be used for resource arrays (resources with
count meta-argument) and resource maps (resources with
for_each meta-argument), such as AWS CloudWatch Log Groups. Infracost will apply the usage values individually to each element of the array/map (they all get the same values). If both an array element such as
this (or map element such as
[*] are specified for a resource, only the array/map element's usage will be applied to that resource. This enables you to define default values using
[*] and override specific elements using their index or key.
- Using array or map wildcard
- Array without wildcard
- Map without wildcard
What-if anlaysis can be done on AWS EC2 Reserved Instances (RI) using the usage file. The RI type, term and payment option can be defined as shown below, to quickly get a monthly cost estimate. This works with
aws_instance as well as
aws_autoscaling_group as they also create EC2 instances. Let us know how you'd like Infracost to show the upfront costs by creating a GitHub issue.