Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Terraform: split function in output gives error when count is used while creating a resource

I am creating a resource by using count in it. When I use split function in the output it gives me error while normal output where split is not used just works fine.

I am running on stack=dev right now. I expect the same resource to not get created on dev stack but it should get created in prod stack. I am trying to write a code in such a way

Below is the piece of code which gives error

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

data "aws_cloudformation_stack" "some_name" {
      count = (local.stack == "dev" ? 0 : 1)
      name = "${local.stack}_some_name"
    }

output "public_alb_subnets" {
  value = split(",", "${data.aws_cloudformation_stack.some_name[*].outputs["PublicElbSubnets"]}")
 
}

It gives me error

Error: Invalid function argument

  on managed_alb.tf line 138, in output "public_alb_subnets":
 138:   value = split(",", "${data.aws_cloudformation_stack.some_name[*].outputs["PublicElbSubnets"]}")
    |----------------
    | data.aws_cloudformation_stack.some_name is empty tuple

Invalid value for "str" parameter: string required.

However below works

output "public_alb_security_groups" {
  value = [
    data.aws_cloudformation_stack.some_name[*].outputs["PublicElbSecurityGroup"],
    data.aws_cloudformation_stack.some_name[*].outputs["InternalElbSecurityGroup"]
  ]
}

I tried many different options on the web but none of them worked. What I am doing wrong here. Even using count.index or 0 in place of * doesn’t work

>Solution :

You have to make your output also conditional, based on your dev or prod environments:

output "public_alb_subnets" {
  value = length(data.aws_cloudformation_stack.some_name) > 0 ? split(",", "${data.aws_cloudformation_stack.some_name[*].outputs["PublicElbSubnets"]}") : null 
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading