Terraform module which calculates price of AWS infrastructure (from Terraform state and plan) πΊπ¦
APACHE-2.0 License
Terraform module, which calculates the AWS infrastructure cost in a variety of ways. This is not a traditional Terraform module because it does not create AWS infrastructure resources but using Terraform plan and Terraform states as input.
cost.modules.tf
is entirely free cost estimation service, which is part of modules.tf that is currently in active development.
Join the mailing list on modules.tf to stay updated!
If you are looking into alternative ways calculating AWS infrastructure costs (for free), you can use terraform-cost-estimation.com or terraform-cost-estimation.
This is not an official HashiCorp product.
provider "aws" {
region = "us-east-1"
}
module "pricing" {
source = "terraform-aws-modules/pricing/aws//modules/pricing"
# content can be Terraform state or plan as JSON fetched from any source (see examples)
content = jsondecode("{\"version\": 4, \"terraform_version\": \"0.14.4\", ...")
}
provider "aws" {
region = "us-east-1"
}
data "local_file" "local_plan" {
filename = "local_plan.json"
}
module "pricing" {
source = "terraform-aws-modules/pricing/aws//modules/pricing"
content = jsondecode(data.local_file.local_plan.content)
}
provider "aws" {
region = "us-east-1"
}
module "pricing" {
source = "terraform-aws-modules/pricing/aws//modules/pricing"
resources = {
"aws_instance.this#5" = { # Note: This means 5 instances (`count = 5`)
instanceType = "c5.xlarge"
location = "eu-west-2"
}
"aws_instance.this2" = {
instanceType = "c4.xlarge"
location = "eu-central-1"
}
}
}
@todo: Describe in more details...
# Project1 (with real EC2 resources):
terraform plan -out=plan.tfplan > /dev/null && terraform show -json plan.tfplan > plan.json
# Project2 (terraform-aws-pricing module):
TF_VAR_file_path=plan.json terraform apply
HOURLY_PRICE=$(terraform output -raw total_price_per_hour)
if HOURLY_PRICE < 10 then
terraform apply plan.json # (from Project1)
else
echo "Crash! Boom! Bang!"
end
Set AWS provider's region to us-east-1
, ap-south-1
or eu-central-1
when using modules/pricing because AWS Pricing service is only available in these regions.
You can also pass provider explicitly as described in the official documentation.
debug_output = true
will return more output which is often helpful only for development and debug purposes.
call_aws_pricing_api = false
will not call AWS Pricing API. Wrong filters produce a lot of noise, so it makes sense to disable this option when developing new filters.
AWS Pricing API should always return one response for the filter. Running these commands can help identify available filters to put into modules/pricing/filters.tf
(see dev
directory also):
aws pricing describe-services --service-code AmazonEC2 --format-version aws_v1 --max-items 1 --region us-east-1
aws pricing get-products --region us-east-1 --filters file://filters.json --format-version aws_v1 --service-code AmazonEC2
Sometimes, you may want to not store Terraform state in backend when dealing with pricing, you can use backend "inmem":
terraform {
backend "inmem" {}
}
When you use this type of backend, there is no way to run terraform output
.
Module created and managed by Anton Babenko.
Please reach out to Betajob if you are looking for commercial support for your Terraform, AWS, or serverless project.
Apache 2 Licensed. See LICENSE for full details.