What is the proper syntax to access attribute (public_ip) of a resource (aws_instance) with count from another resource (null_resource)

0

I am new to terraform and this issue is perhaps addressed earlier. Unfortunately, I am not able to find one.

Question: What is the proper syntax to access attribute (public_ip) of a resource (aws_instance) with count from another resource (null_resource).

terraform { required_version = ">= 0.12, < 0.13" }

provider "aws" { region = "us-east-1"

# Allow any 2.x version of the AWS provider version = "~> 2.0" }

variable instances { default = 2 }

variable username { }

variable password { }

resource "null_resource" "test-null" { count = var.instances

provisioner "file" { content = "foo" destination = "~/bar"

connection {
  type = "ssh"
  host = aws_instance.test-instance.*.public_ip
  user = var.username
  password = var.password
}

} }

resource "aws_instance" "test-instance" { count = var.instances

ami = "ami-c50e37be" instance_type = "t2.micro"

}

Applying this gives the following error:

Error: Incorrect attribute value type: Inappropriate value for attribute "host": string required.

Based on my understanding, it is because "aws_instance.test-instance.*.public_ip" gives list of public_ip, not specific element.

I tried many variations, but none works.

Please suggest the right syntax in this scenario.

Thank You!

Sam

Posted 2020-02-17T01:14:50.867

Reputation: 1

Answers

0

Found the answer, it should have been:

host = aws_instance.test-instance.*.public_ip[count.index]

Please let me know if there are better alternatives.

Also, I am not sure I posted it on the right forum. If not, please suggest alternative.

Thank You!

Sam

Posted 2020-02-17T01:14:50.867

Reputation: 1