Home > Enterprise >  Upgrading from Terraform 0.13 to latest breaks my code as there are quotes now put around outputs
Upgrading from Terraform 0.13 to latest breaks my code as there are quotes now put around outputs

Time:01-10

I'm having an issue when upgrading from Terraform 0.13 to the latest version, as in Terraform 0.13, there were no quotes around variable outputs.

In 0.13, the following code:

output "bucket-name-output" {
  value = "${aws_s3_bucket.logos-bucket.id}"
}

produced this value:

logo-horizon-bucket

In the latest version of Terraform (v 1.3.7), the same code gives this value:

"logo-horizon-bucket"

So you can see that quotes are now being added around the string.

This is then causing my Golang application code, which is trying to upload an object to an S3 bucket, to fail as it's setting the name of the bucket to the output value (which is surrounded by the quotes):

bucketName := terraform.Output(t, terraformOptions, "bucket-name-output")
params := &s3manager.UploadInput{
        Bucket: aws.String(bucketName),
        Key:    aws.String("imageFileName"),
        Body:   getImage(),
    }

I get the following error as S3 bucket names cannot contain quotes.

InvalidBucketName: The specified bucket is not valid.
    status code: 400, request id:

So my question is how to do I remove those quotes from inside the Terraform file? I could add some logic to remove them in the Golang application code, but this is pretty messy so I would like to avoid this if possible...

Thanks!

CodePudding user response:

I assume that something in your system is running terraform output bucket-name-output to get the value of this output value.

The terraform output command was always intended primarily for human consumption and at some point since v0.13 the human-oriented output changed to show values using a syntax similar to how they would appear inside the Terraform language itself, because it helps the reader to understand what type of value they have exported.

There are two different ways to tell terraform output that it should produce data in a form suitable for consumption by external software rather than by humans:

  1. terraform output -json bucket-name-output produces a JSON description of the output value. This option will work for values of any type, by following the same encoding decisions as Terraform's jsonencode function.
  2. terraform output -raw bucket-name-output produces just a raw string version of the output value. Technically what it is doing is converting the output value to a string with the same meaning as the tostring function and then printing the resulting string. This means it only works with output values of types that tostring can convert: strings, numbers, and boolean values.

Since your program was originally trying to use the human-readable output as if it was a raw string value, I think the second of these options would be the easiest for you to retrofit into your system. Your program already seems to be expecting a raw string version of the output value and the -raw option designed for exactly that purpose.

  • Related