python_price_for_terraform_.../README.md

1.6 KiB
Executable File

What is this?

Test to see if a cloud billing API can be used to predict the price of a deployment from a terraform plan file, Hetzner cloud has a small/easily-parsable pricing endpoint - some amateur Python.

Install terraform

curl https://releases.hashicorp.com/terraform/0.12.29/terraform_0.12.29_linux_amd64.zip --output terraform_0.12.29_linux_amd64.zip
unzip terraform_0.12.29_linux_amd64.zip
rm -f terraform_0.12.29_linux_amd64.zip
sudo mv terraform /usr/local/bin
sudo chown root.root /usr/local/bin/terraform
terraform -help

Install provider

This is automatic on terrafrom init for the hetzner provider.

Setup provider and bearer token

The main.tf file should list the provider at the top.

provider "hcloud" {
  token = var.hcloud_token
  endpoint = var.hcloud_endpoint
  poll_interval = var.hcloud_poll_interval
}

The variables.tf file should include the required variables.

variable "hcloud_token" {default = "<your bearer token here>"}
variable "hcloud_endpoint" {default = "https://api.hetzner.cloud/v1"}
variable "hcloud_poll_interval" {default = "500ms"}

main.tf - the terraform code

variables.tf - the variables

plan, apply, destroy

terraform plan
terraform apply
terraform destroy

get resources in json format

Use the plan file to work out the cost of of the objects to be provisioned by terraform Get json output of a terraform plan

terraform plan -out=./planfile terraform show -json planfile.tf

run python script to parse the terraform planfile and fetch costs from the hetzner billing API

python3 main.tf