Terraform Cloud estimates costs for many resources found in your Terraform configuration. It displays an hourly and monthly cost for each resource, and the monthly delta. It also totals the cost and delta of all estimable resources.
In this tutorial, you will enable cost estimation then define policy to check whether the total monthly delta is less than one hundred dollars a month.
Note: This functionality is available in the Terraform Cloud Team & Governance tier, as well as Enterprise. Organization owners can enable a 30-day free trial in their settings under "Plan & Billing". Cost estimates are not available for workspaces using versions of Terraform less than 0.12.0.
»Prerequisites
This tutorial assumes that you are familiar with Terraform Cloud and you have an existing test Terraform Cloud workspace configured with AWS access credentials. Do not apply this policy to a production workspace as it may impact your production environment.
If you don’t, refer to the Create a Workspace tutorial and Set Up Workspace tutorial to learn more about Terraform Cloud and set up a Terraform Cloud workspace configured with AWS access credentials.
Note: Terraform Cloud will not estimate cost on runs or applies targeted against a subset of resources.
»Verify costs using policies
To verify cost estimates using policies, you need to update your policy set and define your policy. Fork the example repository cost-estimation branch to review the final configuration.
Add the following configuration to your sentinel.hcl
file to declare your new policy in your policy set.
policy "less-than-100-month" {
enforcement_level = "soft-mandatory"
}
Then, create a new file named less-than-100-month.sentinel
that contains the following policy configuration.
import "tfrun"
import "decimal"
delta_monthly_cost = decimal.new(tfrun.cost_estimate.delta_monthly_cost)
main = rule {
delta_monthly_cost.less_than(100)
}
This policy uses the tfrun import to check that the new cost delta is no more than \$100. (A new t3.nano
instance should cost well below that.) The decimal
import is used for more accurate math when working with currency numbers.
Finally, save the updated policy configurations to source control. Terraform Cloud will run both policies defined in the sentinel.hcl
policy set.
»Add an estimable Terraform configuration
You will need a valid configuration with cost estimable resources. Add the following terraform configuration in a main.tf
to your workspace repository.
The Ubuntu ami provided in the sample configuration is for us-west-1
. Refer to the Amazon EC2 AMI Locator to select a similar ami for your AWS region.
resource "aws_instance" "basic" {
ami = "ami-0ee1a20d6b0c6a347"
instance_type = "t3.nano"
}
Then, save the updated configuration to source control and queue a run in Terraform Cloud.
For a full list of supported resources in Terraform Cloud cost estimation, refer to the AWS, Azure, and Google Cloud Cost Estimation Documentation.
»View cost estimate
After queueing a new run, Terraform Cloud will estimate your resource cost, which it displays in a phase in the run UI. There you'll find the list of resources, their price details, and the list of un-estimated resources. You'll also find the totals so you can get a sense of the proposed overall monthly cost once the run is applied.
Note: This is just an estimate; some resources don't have cost information available or have unpredictable usage-based pricing.
Click "Discard run" to cancel the run.
»Next steps
Congrats — you've enabled cost estimation and used it in a policy check! This provides another tool to manage your infrastructure spending.
To learn more about cost estimation, refer to the Cost Estimation documentation.
If you would like to learn more about Terraform Cloud, refer to the following links.
- Learn how to migrate terraform state to Terraform Cloud
- Learn more about Terraform Cloud workspaces
- Read the Terraform Cloud documentation
- Learn about the free and paid features of Terraform Cloud
- Learn more about Cost Estimation Documentation
- Read the Run States Documentation.