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

Variable interpolation of a multiple instance resource #633

Closed
Nalum opened this issue Dec 9, 2014 · 8 comments
Closed

Variable interpolation of a multiple instance resource #633

Nalum opened this issue Dec 9, 2014 · 8 comments

Comments

@Nalum
Copy link

Nalum commented Dec 9, 2014

I have the following aws_instance resource:

resource "aws_instance" "api" {
    ami = "${lookup(var.aws-amis, var.aws-region)}"
    instance_type = "${var.aws-instance-type}"
    ebs_optimized = true
    key_name = "${lookup(var.aws-keys, var.aws-region)}"

    security_groups = [
        "${aws_security_group.api.name}"
    ]

    tags = {
        Name = "API"
    }

    provisioner "local-exec" {
        command = "echo ${aws_instance.api.*.public_ip} >> public_ips"
    }

    count = 2
}

When run this resource is creating the public_ips file with the following content:

54.204.171.92
54.204.171.92B780FFEC-B661-4EB8-9236-A01737AD98B654.146.132.10

As you can see the IP of the first instance is duplicated and there is, what I assume to be, a Terraform ID followed by the IP of the second instance.

This is causing issues for me provisioning the machines using chef. Am I doing this correctly or should I be doing it another way?

@svanharmelen
Copy link
Contributor

@Nalum I assume you want to use the count.index here to get only a single IP for a specific resource right? I think (not tested) you could try something like this: ${concat("aws_instance.api.", count.index, ".public_ip")}"?

The UUID in the second string you get, is actually an internally used UUID that should not be exposed/shown. I think that could be considered a bug, but it's not really related to your issue.

@Nalum
Copy link
Author

Nalum commented Dec 9, 2014

@svanharmelen I've just tried to use that as follows:

${concat("aws_instance.api.", count.index, ".public_ip")}

And

${${concat("aws_instance.api.", count.index, ".public_ip")}}

The first one added the following to the file:

aws_instance.api.0.public_ip
aws_instance.api.1.public_ip

And the second one gave the following error:

aws_instance.api.0 (local-exec): Executing: /bin/sh -c "echo ${aws_instance.api.0.public_ip} >> public_ips"
aws_instance.api.0 (local-exec): /bin/sh: 1: Bad substitution

I also tried this:

${lookup(concat("aws_instance.api.", count.index), "public_ip")}

But got the following error:

* lookup(concat("aws_instance.api.", count.index), "public_ip"): lookup in 'aws_instance.api.1' failed to find 'public_ip'

@svanharmelen
Copy link
Contributor

@Nalum after digging some deeper I noticed this isn't supported yet... Sorry for the confusion! So this has to be added as a feature to TF and currently cannot be done...

@armon
Copy link
Member

armon commented Dec 9, 2014

The way to do this would be with "${element(aws_instance.api.*.public_ip, count.index}".
The element() function is new, but will be part of the latest release!

@svanharmelen
Copy link
Contributor

@armon you beat me to it 😉 Was looking to find any open issues and just found that PR #554 already fixed this and is merged into master...

@Nalum
Copy link
Author

Nalum commented Dec 9, 2014

@armon @svanharmelen Thanks guys. Do you know when this release is due?

@svanharmelen
Copy link
Contributor

@Nalum version 0.3.5 is just released and contains the new Element() function. See the CHANGELOG for more info...

@Nalum
Copy link
Author

Nalum commented Dec 10, 2014

@svanharmelen brilliant, thank you very much.

@Nalum Nalum closed this as completed Dec 10, 2014
@ghost ghost locked and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants