使用 Terraform 时从实例获取 EC2 Windows 密码

使用 Terraform 时从实例获取 EC2 Windows 密码

使用 terraform 时,我很难从几个新的 ec2 实例中获取密码。我读了几篇帖子,以为我找到了,但一无所获。

这是我的配置:

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"))}"
}

设法清除所有语法错误但现在运行时出现以下错误:

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

如果您处于尝试执行的资源内,则可以使用:

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

如果你在外面,你必须循环遍历数组。在 terraform >12

output "Administrator_Password" {
   value = [
     for g in aws_instance.example : rsadecrypt(g.password_data,file("path"))
   ]
 }

相关内容