Skip to content

Instantly share code, notes, and snippets.

@antonbabenko
Created May 19, 2020 11:58
Show Gist options
  • Save antonbabenko/7ec3debf52bb2d9a9a4a83869d75253c to your computer and use it in GitHub Desktop.
Save antonbabenko/7ec3debf52bb2d9a9a4a83869d75253c to your computer and use it in GitHub Desktop.
Terraform Cost Estimation + Open Policy Agent
# Terraform Cost Estimation + Open Policy Agent
#
# This code snippet supports terraform state for now.
#
# Get the whole response:
# opa eval --data terraform-cost-estimation.rego --input terraform.tfstate --format pretty data.terraform_cost_estimation
#
# Get boolean response. Return false if state (per hour) is too expensive:
# opa eval --data terraform-cost-estimation.rego --input terraform.tfstate --format pretty data.terraform_cost_estimation.response.allowed
package terraform_cost_estimation
default max_hourly_cost = 0.05
response := output {
response_cost := http.send({"method": "post", "url": "https://cost.modules.tf", "headers": {"Content-type": "application/json"}, "body": input})
output := {
"allowed": max_hourly_cost >= to_number(response_cost.body.hourly),
"hourly": response_cost.body.hourly,
"monthly": response_cost.body.monthly,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment