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

Lookup function, doesn't #531

Closed
delitescere opened this issue Oct 31, 2014 · 6 comments
Closed

Lookup function, doesn't #531

delitescere opened this issue Oct 31, 2014 · 6 comments

Comments

@delitescere
Copy link

The lookup function isn't well documented, but it isn't doing what it should:

$ terraform apply ...
aws_instance.admin: Creating...
  ami:                         "" => "${lookup(\"images\", \"us-west-2\")}"
  associate_public_ip_address: "" => "1"
  availability_zone:           "" => "<computed>"
  instance_type:               "" => "t2.micro"
  key_name:                    "" => "admin"
  private_dns:                 "" => "<computed>"
  private_ip:                  "" => "<computed>"
  public_dns:                  "" => "<computed>"
  public_ip:                   "" => "<computed>"
  security_groups.#:           "" => "1"
  security_groups.0:           "" => "sg-..."
  subnet_id:                   "" => "subnet-..."
  tags.Name:                   "" => "admin"
aws_instance.admin: Error: Error launching source instance: Invalid id: "${lookup("images", "us-west-2")}" (expecting "ami-...") (InvalidAMIID.Malformed)

Repro source terraform file, including various combinations of trying to get lookup to work:

provider "aws" {
   region = "${var.region}"
}

variable "region" {
  description = "The mnemonic name of the Region. Typically set using $AWS_DEFAULT_REGION."
  #default = "${env.AWS_DEFAULT_REGION}"
}

variable "images" {
  default = {
    us-east-1 = "ami-123"
    us-west-2 = "ami-456"
  }
}

resource "aws_instance" "admin" {
#  ami = "${file("ami.txt")}" #works if file has content but might as well use an override file
#  ami = "${var.images.us-east-1}" #works but is not dynamic
#  ami = "${lookup("images", "us-east-1")}" #works but is not dynamic
#  ami = "${lookup(images, us-east-1)}" #errors
  ami = "${lookup("images", "${var.region}")}" #fails but interpolates region, escapes quotes
#  ami = "${lookup("images", ${var.region})}" #fails but interpolates region, escapes quotes
#  ami = "${lookup(images, "${var.region}")}" #fails but interpolates region, escapes quotes
#  ami = "${lookup(images, ${var.region})}" #fails but interpolates region
#  ami = "${lookup("images", "region")}" #errors
  security_groups = ["sg-..."]
  subnet_id = "subnet-..."
  instance_type = "t2.micro"
  key_name = "admin"
  associate_public_ip_address = true
  tags {
    Name = "admin"
  }
}

Using (v0.3.1)

@frntn
Copy link
Contributor

frntn commented Nov 1, 2014

+1 on this one.

Plus for the first file attempt, the ami.txt must also be one line with no LF.

echo -n "ami-123" > ami.txt

I found this annoying. Can't terraform trim the content on its own or something ?

@delitescere
Copy link
Author

You are correct @frntn. In this case, though, you may be better off creating "override" files that have the region-specific settings, with a file for each region. Then, temporarily copy the specific region's override file into the directory before using terraform apply, and remove it after. I think that's what I'm going to do for now. 😣

@sethvargo sethvargo changed the title lookup function, doesn't (v0.3.1) lookup function, doesn't Nov 19, 2014
@sethvargo sethvargo changed the title lookup function, doesn't Lookup function, doesn't Nov 19, 2014
@dhoppe
Copy link

dhoppe commented Feb 3, 2015

The lookup does not work because you have used images as a string and not as a variable.

The following example correspond to the current documentation and works with version 0.3.6.

variable "region" {
    default = "eu-west-1"
}

variable "ami" {
    default = {
        ap-northeast-1 = "ami-b25d44b3" # Asia Pacific (Tokyo)
        ap-southeast-1 = "ami-aeb49ffc" # Asia Pacific (Singapore)
        ap-southeast-2 = "ami-6b770351" # Asia Pacific (Sydney)
        eu-central-1 = "ami-98043785"   # EU (Frankfurt)
        eu-west-1 = "ami-61e56916"  # EU (Ireland)
        sa-east-1 = "ami-3d8b3720"  # South America (Sao Paulo)
        us-east-1 = "ami-e0efab88"  # US East (N. Virginia)
        us-west-1 = "ami-b4869ff1"  # US West (N. California)
        us-west-2 = "ami-431a4273"  # US West (Oregon)
    }
}

resource "aws_instance" "example" {
    ami = "${lookup(var.ami, var.region)}"
    ...
}

@delitescere
Copy link
Author

Thanks @dhoppe I'll give that a crack!

mitchellh added a commit that referenced this issue Feb 20, 2015
@mitchellh
Copy link
Contributor

I updated the website. Also, a lot more of your 2nd parameter cases work as of 0.3.7. :)

@mitchellh mitchellh self-assigned this Feb 20, 2015
yahyapo pushed a commit to yahyapo/terraform that referenced this issue Mar 13, 2015
@ghost
Copy link

ghost commented May 4, 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 May 4, 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

5 participants