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

返回此错误,因为aws_instance.example.*.password_data是每个EC2实例中password_data结果的列表。每个都必须用rsadecrypt分别解密。

要在Terraform v0.11中执行此操作,需要使用null_resource作为解决方法来实现每个操作:

resource "aws_instance" "example" {
  count = 2
  ami                    = "ami-06f9d25508c9681c3"
  instance_type          = "t2.small"
  key_name               = "mykey"
  vpc_security_group_ids = ["sg-98d190fc","sg-0399f246d12812edb"]
  get_password_data      = true
}
resource "null_resource" "example" {
  count = 2
  triggers = {
    password = "${rsadecrypt(aws_instance.example.*.password_data[count.index], file("mykey.pem"))}"
  }
}
output "Administrator_Password" {
    value = "${null_resource.example.*.triggers.password}"
}

从Terraform v0.12.0开始,可以使用新的for表达式构造来简化这一点:

resource "aws_instance" "example" {
  count = 2
  ami                    = "ami-06f9d25508c9681c3"
  instance_type          = "t2.small"
  key_name               = "mykey"
  vpc_security_group_ids = ["sg-98d190fc","sg-0399f246d12812edb"]
  get_password_data      = true
}
output "Administrator_Password" {
  value = [
    for i in aws_instance.example : rsadecrypt(i.password_data, file("mykey.pem"))
  ]
}

相关内容

  • 没有找到相关文章

最新更新