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

Default value for map variables #2312

Closed
phrawzty opened this issue Jun 11, 2015 · 8 comments
Closed

Default value for map variables #2312

phrawzty opened this issue Jun 11, 2015 · 8 comments

Comments

@phrawzty
Copy link
Contributor

Hello,

As suggested by @jdotpz in the google group it would be handy if map variables were able to have a default value. Example:

variable "ec2_type" {
    default = {
        stage = "r3.large"
        prod = "r3.2xlarge"
        * = "m3.medium"
    }
}

Such that:

## defined (actual behaviour)
$environment="stage"
${lookup(var.ec2_type, var.environment)} == "r3.large"

## catch all (proposed behaviour)
$environment = "foo"
${lookup(var.ec2_type, var.environment)} == "m3.medium"

Another possible extension would be to allow for differentiating between "catch-all" and "empty / undefined" - this should be debated first, however. 😏 :

variable "ec2_type" {
    default = {
        stage = "r3.large"
        prod = "r3.2xlarge"
        default = "m3.medium"
        none = "t1.micro"
    }
}

Such that:

## defined (actual behaviour)
$environment="stage"
${lookup(var.ec2_type, var.environment)} == "r3.large"

## catch all (proposed behaviour)
$environment = "foo"
${lookup(var.ec2_type, var.environment)} == "m3.medium"

## undef (possible behaviour)
${lookup(var.ec2_type, var.environment)} == "t1.micro"
@ryangraham
Copy link

I was just looking for something like this for selecting PV versus HVM AMIs by instance_type.

@phinze
Copy link
Contributor

phinze commented Jun 11, 2015

@ryangraham you might be interested in https://github.com/terraform-community-modules/tf_aws_virttype 😀

@gozer
Copy link

gozer commented Mar 30, 2016

👍

@jfelixetcetera
Copy link

I'd find this incredibly handy, in either the form above, or as a third parameter to lookup as described in #1505.

@arshdeeptinna
Copy link

Isn't this functionality supported sine 0.7 in the following form?

lookup(var.amis, var.env, 'Default-AMI')

@jfelixetcetera
Copy link

Yes, it got implemented but many of the PRs asking for it were updated slowly. Here's another reference, demonstrating how you can use a * or default param in a map to achieve it (with the third param in lookup). Not quite as clean as an automatic fallback, but more explicit so maybe better.

@apparentlymart apparentlymart added config and removed core labels Nov 6, 2018
@apparentlymart
Copy link
Contributor

Hi all,

As @arshdeeptinna pointed out, the lookup function now has a third argument for specifying defaults. Terraform 0.7 added the first-class index operator like var.amis[var.env], which has been generalized for the forthcoming v0.12.0 release to work with any expression rather than just with variables, so from that v0.12.0 onwards the lookup function's primary purpose will be providing a fallback value, and the index operator should be used in all other cases.

Since there's already a solution for this, I'm going to close this out. Sorry for letting this sit here for so long.

@ghost
Copy link

ghost commented Mar 31, 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 Mar 31, 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

8 participants