How to fix “rsadecrypt: argument 1 should be type

2019-08-18 11:45发布

问题:

At the beginning I want to build just one windows machine, so this code works fine at the beginning:

output "Administrator_Password" {
  value = "${rsadecrypt(aws_instance.new_instance.password_data, file("${module.ssh_key_pair.private_key_filename}"))}"
}

But once I introduce count to resource "aws_instance" "new_instance" {, I have to add * to the expression aws_instance.new_instance.*.password_data.

But then I start to get this error:

Error: Error running plan: 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.new_instance.*.password_data, file("${module.ssh_key_pair.private_key_filename}"))}

I have tried the count.index syntax but they do not work. The variants are

aws_instance.new_instance.password_data[count.index]

and

aws_instance.new_instance.password_data[aws_instance.new_instance.count.index]

回答1:

Try to use template_file resource,

data "template_file" "decrypted_keys" {
  count = "${aws_instance.new_instance.count}"

  template = "${rsadecrypt(element(aws_instance.new_instance.*.password_data, count.index), file(module.ssh_key_pair.private_key_filename))}"
}

output "Administrator_Password" {
  value = "${data.template_file.decrypted_keys.*.rendered}"
}


标签: terraform