使用Terraform时从实例获取EC2 Windows密码
在使用terraform时,我正在努力从几个新的ec2实例中获取密码。我读了好几篇文章,以为我已经读到了,但没有任何进展 这是我的配置:使用Terraform时从实例获取EC2 Windows密码,terraform,terraform-provider-aws,infrastructure-as-code,Terraform,Terraform Provider Aws,Infrastructure As Code,在使用terraform时,我正在努力从几个新的ec2实例中获取密码。我读了好几篇文章,以为我已经读到了,但没有任何进展 这是我的配置: resource "aws_instance" "example" { ami = "ami-06f9d25508c9681c3" count = "2" instance_type = "t2.small" key_name = "mykey" vpc_security_group_ids =["sg-98d190fc",
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\u实例。示例。*.password\u data
是来自每个EC2实例的password\u数据的列表。必须使用rsadecrypt
分别对每个文件进行解密
要在Terraform v0.11中实现这一点,需要使用null_资源
作为一种解决方案,以实现“针对每个”操作:
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"))
]
}
我想您可能需要2个Administrator\u Password
输出,识别列表中的每个value=“${rsadecrypt(aws\u instance.example.*.Password\u data[0],file(“mykey.pem”))}”
将其放在一个实例中以保持简单并使其工作。使用此config output“Administrator_Password”{value=“${rsadecrypt(aws_instance.example.Password_数据,文件(“C:/TF/mykey.pem”))}
但最终得到以下响应-错误:错误刷新状态:发生1个错误:*output.Administrator_Password:rsadecrypt:crypto/rsa:解密错误在:${rsadecrypt(aws_实例.example.password_数据,文件(“C:/TF/mycert.pem”))
能够从aws cli使用密钥获取密码,以便知道密钥是否正确aws ec2获取密码数据--实例id i-09557bc1e0cb09c67--priv launch key mykey.pem
只需了解terraform设置感谢您的反馈,但仍会出错。'错误:错误刷新状态:1个错误发生:*null_resource.example:2个错误:*null_resource.example[1]:索引1超出列表aws_实例的范围。example.*.password_数据(最大值1)在:${rsadecrypt(aws_instance.example.*.password_数据[count.index],文件(“mycert.pem”)}*null_resource.example[0]:rsadecrypto/rsa:decryption错误在:${rsadecrypt(aws_instance.example.*.password_data[count.index],file(“mycert.pem”)}'我还尝试将其放在一个实例上,但仍然遇到了证书周围的同一个问题,因为我怀疑我可能试图用两个实例使其复杂化。我在执行上述表达式时遇到了此错误错误错误:尝试从null值获取属性│ │ 在ec2.tf第18行的输出“管理员密码”中:│ 18:for i in aws_instance.windowstest:rsadecrypt(i.password_数据,文件(“/home/qDownloads/terraform key.pem”))│ │ 此值为null,因此它没有任何属性。╵ ╷ │ 错误:不支持的属性│ │ 在ec2.tf第18行的输出“管理员密码”中:│ 18:对于aws_实例中的i.windowstest:RSA解密(i.password_数据,fi