How do you output the node_port value with Terraform's kubernetes_service?

2/14/2019

I'm trying to create in output in my Terraform template.

output "port" {
  value = "${kubernetes_service.postgres_service.spec}"
}

will produce

ports = {
  cluster_ip = 10.245.175.57
  external_ips = []
  external_name =
  load_balancer_ip =
  load_balancer_source_ranges = []
  port = [map[node_port:30344 protocol:TCP name: port:5432 target_port:5432]]
  selector = map[app:postgres]
  session_affinity = None
  type = NodePort
}

I tried "${kubernetes_service.postgres_service.spec}.port" but it produces a syntax error.

I also tried "${lookup(kubernetes_service.postgres_service.spec[0], "port")}" but it can only be used with flat maps.

Am I missing something? This is my first attempt at Terraform and it's a lot different than what I'm used to (CloudFormation). Any help will be greatly appreciated. I'm just looking to get the value of node_port (30344) to output to the screen after running apply.

Terraform doc that I'm using for Kubernetes

-- billabrian6
terraform
terraform-provider-kubernetes

1 Answer

2/14/2019

This should work:

output "port" {
  value = "${kubernetes_service.postgres_service.spec.0.port.0.port}"
}

Reference:

https://github.com/terraform-providers/terraform-provider-kubernetes/blob/188074bb842e0d708c3a95e095c555cd4fec5e03/kubernetes/resource_kubernetes_service_test.go#L42

resource.TestCheckResourceAttr("kubernetes_service.test", "spec.0.port.0.port", "8080"),
-- BMW
Source: StackOverflow