I want to be able to access a specific resource variable within that resource.
For example : run a provisioner for an instance and supply it with the instance private ip (or id or anything else).
This is already supported, for example you can do:
# Join the newly created machine to our Consul cluster
resource "aws_instance" "web" {
...
provisioner "local-exec" {
command = "echo ${aws_instance.web.private_ip} >> private_ips.txt"
}
}
This is from the docs: http://www.terraform.io/docs/provisioners/local-exec.html
in 0.2.2 or in 0.3?
On Tue, Oct 14, 2014 at 12:45 PM, Armon Dadgar [email protected]
wrote:
This is already supported, for example you can do:
Join the newly created machine to our Consul cluster
resource "aws_instance" "web" {
...
provisioner "local-exec" {
command = "echo ${aws_instance.web.private_ip} >> private_ips.txt"
}
}This is from the docs:
http://www.terraform.io/docs/provisioners/local-exec.html—
Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/398#issuecomment-59076942.
and if let's say I have a count=5 for this instance, will the syntax be the
same, or do i need to access the specific index?
On Tue, Oct 14, 2014 at 12:45 PM, Armon Dadgar [email protected]
wrote:
This is already supported, for example you can do:
Join the newly created machine to our Consul cluster
resource "aws_instance" "web" {
...
provisioner "local-exec" {
command = "echo ${aws_instance.web.private_ip} >> private_ips.txt"
}
}This is from the docs:
http://www.terraform.io/docs/provisioners/local-exec.html—
Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/398#issuecomment-59076942.
@ronpanto This syntax has been supported since 0.1. If you are using count then it depends. You can access a specific index like:
${aws_instance.web.0.private_ip}
Or you can "splat" all the instances:
${aws_instance.web.*.private_ip}
http://www.terraform.io/docs/configuration/interpolation.html
I'll explain what i mean by example. Let's say I have 5 instances of the
type web and I want to write the id of each of them to file (just
hypothetical) on that machine.
What should I replace XXXXX in this example with? obviously i can't use
aws_isnstance.web.0 (or 1 or any number). if i will just use
aws_instance.web.id will that know to get the current one? or do i need to
wait for terraform 0.3 and use the count.index in some way?
resource "aws_instance" "web" {
...
count = 5
provisioner "remote-exec" {
inline = ["echo ${XXXXX} >> my_id.txt"]
}
}
On Tue, Oct 14, 2014 at 12:51 PM, Armon Dadgar [email protected]
wrote:
@ronpanto https://github.com/ronpanto This syntax has been supported
since 0.1. If you are using count then it depends. You can access a
specific index like:${aws_instance.web.0.private_ip}
Or you can "splat" all the instances:
${aws_instance.web.*.private_ip}
http://www.terraform.io/docs/configuration/interpolation.html
—
Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/398#issuecomment-59077969.
I am also curious if you can have access to the "current count" as some sort of local iterator variable. The use case we wanted was to have some way to inject into the node what it's hostname is. For example if we want the hostnames to correspond to the count like this: node0, node1, node2, ... we don't currently have that ability without creating individual aws_instance resources in the tf file.
@ronpanto You can't write the ID of all 5 from within a provisioner, otherwise it would create an impossible dependency set (all 5 servers depend on the other 4 being started first). One option is to have each node reference it's own ID, or to have one of them depend on the other 4, thus breaking the cycle.
@knuckolls You can do that with 0.3 using the ${count.index} variable.
I don't want to write all 5. That I can do later using a provisioner for a
dns record or something
(although it would be awesome to have a provisioner running after all of
them were created)
What i meant is running the ID of instance 1, on Instance 1 (not all of
them).
How do i do that?
On Sat, Oct 18, 2014 at 6:00 PM, Armon Dadgar [email protected]
wrote:
@ronpanto https://github.com/ronpanto You can't write the ID of all 5
from within a provisioner, otherwise it would create an impossible
dependency set (all 5 servers depend on the other 4 being started first).
One option is to have each node reference it's own ID, or to have one of
them depend on the other 4, thus breaking the cycle.@knuckolls https://github.com/knuckolls You can do that with 0.3 using
the ${count.index} variable.—
Reply to this email directly or view it on GitHub
https://github.com/hashicorp/terraform/issues/398#issuecomment-59631221.
@ronpanto You can't do a dynamic lookup yet, i.e. "this index's private IP". I think there is another issue tracking that as well.
Fixed in master with self variables! ${self.foo}
@mitchellh does this work within the resource scope? I tried in master and it seems to only work within provisioners.
Why is self.ATTRIBUTE syntax only valid within provisioners?
I would like to do something like the following:
resource "aws_ami_copy" "base_img" {
name = "deb9-base-img"
source_ami_id = "${var.aws_amis["deb9_base"]}"
source_ami_region = "${var.aws_regions["pro"]}"
tags = {
Name = "${self.name}"
}
}
At present I have to repeat the name in the resource scope and the tags.
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.
Most helpful comment
Why is
self.ATTRIBUTEsyntax only valid within provisioners?I would like to do something like the following:
At present I have to repeat the name in the resource scope and the tags.