0

I'm struggling to get the password from a couple of new ec2 instances when using terraform. Been reading up through a couple of posts and thought i had it but not getting anywhere.

Here's my config:

resource "aws_instance" "example" {
  ami = "ami-06f9d25508c9681c3"
  count         = "2"
  instance_type = "t2.small"
  key_name = "mykey"
  vpc_security_group_ids =["sg-98d190fc","sg-0399f246d12812edb"]
 get_password_data = "true"
}

output "public_ip" {
    value = "${aws_instance.example.*.public_ip}"
}

output "public_dns" {
    value = "${aws_instance.example.*.public_dns}"
}

output "Administrator_Password" {
    value = "${rsadecrypt(aws_instance.example.*.password_data, file("mykey.pem"))}"
}

Managed to clear up all the syntax errors but now when running get the following error:

PS C:\tf> terraform apply
aws_instance.example[0]: Refreshing state... (ID: i-0e087e3610a8ff56d)
aws_instance.example[1]: Refreshing state... (ID: i-09557bc1e0cb09c67)

Error: Error refreshing state: 1 error(s) occurred:

* output.Administrator_Password: At column 3, line 1: rsadecrypt: argument 1 should be type string, got type list in:

${rsadecrypt(aws_instance.example.*.password_data, file("mykey.pem"))}

1 Answer 1

1

if you are inside the resource you are trying to execute you can just use:

${rsadecrypt(self.password_data,file("path"))}

If you are outside you will have to loop over the array. In terraform >12

output "Administrator_Password" {
   value = [
     for g in aws_instance.example : rsadecrypt(g.password_data,file("path"))
   ]
 }
1
  • Should mention that the file is private key corresponding to the public key that was uploaded to AWS to create a key pair in EC2 console.
    – Oliver
    Jun 1, 2021 at 20:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .