Hello, hopefully a quick question - should Infraco...
# help
g
Hello, hopefully a quick question - should Infracost (v0.10.19) correctly handle for_each constructs within the terraform configuration? I'm trying to analyse the cost of a k8s cluster with a variable number of non-default node pools. I'm only seeing one line for these in the output, and the node type is wrong for it, resulting in zero cost
azurerm_kubernetes_cluster.k8s
├─ Uptime SLA                                                   730  hours         $73.00
└─ default_node_pool
└─ Instance usage (pay as you go, Standard_E8ds_v4)          730  hours        $420.48
azurerm_kubernetes_cluster_node_pool.worker
├─ Instance usage (pay as you go, Standard_)                    730  hours          $0.00
└─ os_disk
└─ Storage (P10, LRS)                                          1  months        $19.71
A cutdown version of the terraform looks like:
resource "azurerm_kubernetes_cluster_node_pool" "worker" {
for_each              = var.worker_node_pools
name                  = each.key
kubernetes_cluster_id = azurerm_kubernetes_cluster.k8s.id
zones                 = null
vm_size               = each.value.node_type
node_count            = each.value.node_count
min_count             = each.value.node_count
max_count             = var.max_node_count
enable_auto_scaling   = each.value.autoscaling
os_type               = "Linux"
os_disk_size_gb       = 100
os_disk_type          = each.value.disk_type
vnet_subnet_id        = var.subnet_create ? azurerm_subnet.subnet[0].id : data.azurerm_subnet.extantsubnet[0].id
}
And the command line:
./infracost breakdown --path . --terraform-var "worker_node_pools={'poola': {'autoscaling': True, 'node_count': 10, 'node_type': 'Standard_D8ads_v5', 'disk_type': 'Ephemeral'}, 'poolb': {'autoscaling': True, 'node_count': 10, 'node_type': 'Standard_D16ads_v5', 'disk_type': 'Ephemeral'}}"
m
@gifted-journalist-92338 I think this might be due to the terraform var flag being unable to parse this complex value. Does this zero price still appear if you place the worker node pools variable in a terraform var file and load it that way?
g
Yes, that does work - once I had converted from JSON format (which I normally pass to terraform as an environment variable) Does the command line flag want JSON, or terraform HCL format?
m
So I believe right now the command line flag only accepts simple key value pairs in hcl format. I’ll raise an issue to track supporting more complex hcl and json structures and paste here so you can follow
g
Brilliant, thanks for the help.
I'll have to do some head scratching now as far as my pipeline goes - obviously the vars file in the repo that I am reading from does not have any values set for the variables, so I'll have to re-write it, or replace it to make this work.
m
Hmm, could you just echo the json into a terraform.tfvars.json file and then get Infracost to read that
Obviously not ideal, but at least provides a stop gap
g
Yes, that looks like it will work! It is "painful" to have to write to the input folder, but might actually be cleaner than the massive set of command-line options I was using for the full thing. It is even nicer once I ignore the help text and just give --terraform-var-file an absolute path ;)
w
@gifted-journalist-92338 you might benefit from a config-file with the new template syntax so you can define var-files for the projects. Myself or @mysterious-teacher-68276 would be happy to help write one for your repo too. Depending on where your repo lives (github/gitlab/azure-repos/bitbucket), I can send you an example of how to use the config file with template syntax in your pipeline too. This docs page shows an example with gitlab, the steps for other source control systems would be similar.
m
@gifted-journalist-92338 here’s an issue for you to follow on the
terraform-var
improvements: https://github.com/infracost/infracost/issues/2396
g
@white-airport-8778 I'll have a read of the documentation - I think, with @mysterious-teacher-68276's help 👍, I have it working for my first few simple test cases.