Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interpolation function CONCAT shouldn't add empty element if a provided list is empty. #3119

Closed
BrunoBonacci opened this issue Aug 29, 2015 · 3 comments

Comments

@BrunoBonacci
Copy link

Problem:

list1 = [a,b,c]
list2 = []
concat(list1, list2) = [a,b,c, ]  # wrong

# but should be
concat(list1, list2) = [a,b,c]  # without empty element at the end

# similarly
concat(list2,list1) = [ ,a,b,c] # wrong empty element 0

Scenario:
Assume I want to have a number of different CIDR as variable and then use there in security groups.

variable "cidr_general_access" {
    default = "1.1.1.1/32,2.2.2.2/32"
}
variable "cidr_service1_access" {
    default = "3.3.3.3/32"
}


output "concat-lists" {
    value = ["${join(" | ", concat( split(",", var.cidr_general_access), split(",", var.cidr_service1_access)))}"]
}

If all CIDR variable contains at least 1 element then everything works fine:

$ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  concat-lists = 1.1.1.1/32 | 2.2.2.2/32 | 3.3.3.3/32

however if one of the var is empty "" then an empty element is added

variable "cidr_general_access" {
    default = "1.1.1.1/32,2.2.2.2/32"
}
variable "cidr_service1_access" {
    default = ""
}


output "concat-lists" {
    value = ["${join(" | ", concat( split(",", var.cidr_general_access), split(",", var.cidr_service1_access)))}"]
}

Output:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  concat-lists = 1.1.1.1/32 | 2.2.2.2/32 |
                                           ^^^^^^^^^^^^^^
                                           empty element

The empty element causes an error during the setup of the security group.

@phinze
Copy link
Contributor

phinze commented Oct 12, 2015

Yeah this is definitely a bug. This is probably related to #3437. We'll get this fixed up.

@mitchellh
Copy link
Contributor

Thanks for the report and the detailed repro cases! For really wonky backwards compat reasons, I'm keen to not change this behavior in light of the fact we just introduced first class lists!

With first class lists, this issue goes away:

variable "cidr_general_access" {
    default = ["1.1.1.1/32", "2.2.2.2/32"]
    type = "list"
}

variable "cidr_service1_access" {
    default = []
    type = "list"
}


output "concat-lists" {
    value = ["${join(" | ", concat(var.cidr_general_access, var.cidr_service1_access))}"]
}

Hurray!

@ghost
Copy link

ghost commented Apr 23, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants