0
votes

I'm relatively new to Terraform - I have a module setup as below, the issue I'm having is with the outputs if the module count is '0' when running a terraform plan. Output PW works fine now that I've used the element(concat workaround but the Output I'm having issues with is DCPWUn, I get the following error:

Error: Error refreshing state: 1 error(s) occurred:
* module.PrimaryDC.output.DCPWUn: At column 21, line 1: rsadecrypt: argument 1 should be type string, got type list in:
${element(concat("${rsadecrypt(aws_spot_instance_request.PrimaryDC.*.password_data,file("${var.PATH_TO_PRIVATE_KEY}"))}", list("")), 0)}

Code:

resource "aws_spot_instance_request" "PrimaryDC" {
  wait_for_fulfillment = true

  provisioner "local-exec" {
    command = "aws ec2 create-tags --resources ${self.spot_instance_id} --tags Key=Name,Value=${var.ServerName}0${count.index +01}"
  }

  ami                    = "ami-629a7405"
  spot_price             = "0.01"
  instance_type          = "t2.micro"
  count                  = "${var.count}"
  key_name               = "${var.KeyPair}"
  subnet_id              = "${var.Subnet}"
  vpc_security_group_ids = ["${var.SecurityGroup}"]
  get_password_data      = "true"

  user_data = <<EOF
  <powershell>
  Rename-computer -NewName "${var.ServerName}0${count.index +01}"
  </powershell> 
  EOF

  tags {
    Name = "${var.ServerName}0${count.index +01}"
  }
}

output "PW" {
 value = "${element(concat("${aws_spot_instance_request.PrimaryDC.*.password_data}", list("")), 0)}"
}

output "DCPWUn" {
 value = "${element(concat("${rsadecrypt(aws_spot_instance_request.PrimaryDC.*.password_data,file("${var.PATH_TO_PRIVATE_KEY}"))}", list("")), 0)}"
}
2

2 Answers

0
votes

As the error says, rsadecrypt has an argument that is of type list, not string as it should be. If you want to ensure that the argument is a string, you need to invert your function call nesting to make sure that rsadecrypt gets a string:

output "DCPWUn" {
    value = "${rsadecrypt(element(concat(aws_spot_instance_request.PrimaryDC.*.password_data, list("")), 0),file("${var.PATH_TO_PRIVATE_KEY}"))}"

}

-1
votes

The problem lies within this line

${element(concat("${rsadecrypt(aws_spot_instance_request.PrimaryDC.*.password_data,file("${var.PATH_TO_PRIVATE_KEY}"))}", list("")), 0)}

What are you trying to achieve? Let's break it down a little

  • element(, 0): Get the first element of the following list.
  • concat(,list("")): Concatenate the following list of strings and then append the concatenation of a list containing the empty string (Note that the second part is not useful, since you are appending an empty string).
  • rsadecrypt(,file("${var.PATH_TO_PRIVATE_KEY}")): decrypt the following expression with the private key (Error: The following thing needs to be a string, you will be supplying a list)
  • aws_spot_instance_request.PrimaryDC.*.password_data This is a list of all password data (and not a string).

I don't know what your desired output should look like, but with the above list, you may be able to mix-and-match the functions to suit your needs.

edit: Fixed a mistake thanks to the comment by rahuljain1311.