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

Map helper functions: keys(), values() #1915

Closed
gtrevg opened this issue May 11, 2015 · 6 comments · Fixed by #2198
Closed

Map helper functions: keys(), values() #1915

gtrevg opened this issue May 11, 2015 · 6 comments · Fixed by #2198

Comments

@gtrevg
Copy link

gtrevg commented May 11, 2015

Is there an easy way to get only the keys or values defined in a map? For example, given:

variable "aws_az_subnet_map" {
  description = "The subnet id for each availability zone"
  default = {
    us-west-2a = "subnet-abcd1234"
    us-west-2b = "subnet-efab5678"
    us-west-2c = "subnet-cdef9012"
  }
}

It would allow you to easily just dump either the keys or values into other parts of the configuration.

resource "aws_elb" "my_elb" {
    subnets = [ "${values(var.aws_az_subnet_map)}" ]
}

resource "aws_autoscaling_group" "my_autoscale_group" {
    availability_zones = [ "${keys(var.aws_az_subnet_map)}" ]
    vpc_zone_identifier = [ "${values(var.aws_az_subnet_map)}" ]
}

resource "aws_instance" "my_instance" {
    count = "${length( keys(var.aws_az_subnet_map))}"
    subnet_id = "${ element(values(var.aws_az_subnet_map)), count.index }"
}

This keeps one form having to maintain two variables that then must remain in sync with the correct number of items in each.

Thanks

@radeksimko
Copy link
Member

That's actually an interesting idea. There isn't AFAIK a such built-in function yet.

@mikeyhill
Copy link

+1 this would be much more graceful than hardcoding those individual values in

@phinze
Copy link
Contributor

phinze commented May 12, 2015

Yep I think this would be a great idea. A PR would be welcome, or one of us core folk will try to pick it up when we can.

Worth noting that the functionality would be limited to direct variable access w/ defaults until we enhance the config language to fully support complex types. Still worth adding IMO.

phinze added a commit that referenced this issue Jun 2, 2015
they work on maps with both keys and values that are string types, which
AFAICT are the only types of maps we have right now.

closes #1915
@edmundcraske
Copy link
Contributor

Doesn't seem like these functions are documented in the main Terraform docs yet?

@radeksimko
Copy link
Member

@edmundcraske See 8036528 It was documented about a month ago, docs website will be updated along with the next release. 😉

@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

Successfully merging a pull request may close this issue.

5 participants