-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Length of list or map? #1389
Comments
👍 |
Agreed. IMO some sort of count / length function should be in core. |
I just hit the need for getting the length of a list too. |
See #1495 |
This has been implemented & merged in #1495 , so the issue can be closed I guess? |
It appears like #1495 references strings and lists but not maps, which was part of the original request. |
@mikattack The original example can be reworked to the following: variable "zones" {
default = "us-west-2a,us-west-2b,us-west-2c"
}
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "public" {
vpc_id = "${aws_vpc.main.id}"
cidr_block = "10.0.${count.index * 4}.0/22"
availability_zone = "${element(split(",", var.zones), count.index)}"
count = "${length(split(",", var.zones))}"
} not as elegant as if we'd have support for real lists I don't believe zones nor regions are good candidates for maps, it should really IMO be a list instead (whether that's a pseudo-list we've got now or real list in the future). That said, if there is any valid use-case for |
This is exactly the problem we ran into. Your workaround with delimited strings and Agree that there maybe isn't an immediate need for map lengths. Though, if we had |
Going to close this since the core of the issue is implemented and there hasn't been activity since then. Anybody on this thread can follow up here or file a new thread if there's additional work to be tracked here! 👍 |
@radeksimko: Actually I expected Maps with count, as described in the doc, are arguably a way more elegant solution than split-parsed comma-separated lists, pending actual lists. The map technique also has the advantage over the list (comma-separated or actual) that it lets you specify several attributes varying over your otherwise similar resources (say a username and a path). Though doable with several lists, you need to keep them in sync, which is harder and uglier to express, and more error-prone:
is more declarative than
👍 for map length |
@ejoubaud Does that
|
@matthughes: No, good point, it doesn't, not sure why, maybe nested maps. I tend to wish it did though, as it does in JSON. I think I just came up with the example on the spot to illustrate how maps are more expressive than parseable string lists. |
You can use the map helper functions to get the length of a map variable: e.g.: variable "aws_az_subnet_map" { |
If I'm right, it should be: |
what if we need to add another availability zone? Say: default = {
us-west-2a = "subnet-abcd1234"
us-west-2b = "subnet-efab5678"
us-west-2c = "subnet-cdef9012"
us-west-2d = "subnet-11223344"
} when I run |
The problem raised by @tuannvm still occurs -- I think @phinze may have closed this issue a little too quickly. For instance, the following resource "azurerm_storage_share" "storage_shares" {
count = "${length(var.storage_shares)}"
name = "${element(keys(var.storage_shares), count.index)}"
resource_group_name = "${var.resource_group_name}"
storage_account_name = "${azurerm_storage_account.storage_account.name}"
quota = "${lookup(var.storage_shares, element(keys(var.storage_shares), count.index))}"
} The problem also occurs when replacing the quota line with the following interpolation expression: "${element(values(var.storage_shares), count.index))}"
{
"someshare": "100",
"someothershare": "10"
} |
@LukeCarrier I found out that terraform map will follow
So, if you add new item that potentially to sit between |
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. |
I figure I'm missing something here. How can I calculate the length of a list/map variable?
I'd like to specify a list of AWS availability zones, and generate a subnet per zone (either via a map of values, or the
split()
hack for now)The text was updated successfully, but these errors were encountered: