I am working with programmatically-generated Terraform configuration files using the JSON syntax. I am trying to update the generator to emit configuration for Terraform v0.12, but can’t figure out how to interpolate a complex expression.
The documentation says,
When a JSON string is encountered in a location where arbitrary expressions are expected, its value is first parsed as a string template and then it is evaluated to produce the final result.
If the given template consists only of a single interpolation sequence, the result of its expression is taken directly, without first converting it to a string. This allows non-string expressions to be used within the JSON syntax…
But when I try to run this configuration:
"locals": {
"transition": {
"days": 366,
"storage_class": "STANDARD_IA"
},
"expiration": {
"days": 732
},
"lifecycle_rule": {
"enabled": true,
"transition": [
"${local.transition}"
],
"expiration": [
"${local.expiration}"
]
}
},
"resource": {
"aws_s3_bucket": {
"alb_logs": {
"bucket": "my-alb-logs",
"lifecycle_rule": [
"${local.lifecycle_rule}"
]
},
"elb_logs": {
"bucket": "my-elb-logs",
"lifecycle_rule": [
"${local.lifecycle_rule}"
]
}
}
}
Terraform errors out:
Error: Incorrect JSON value type
on terraform.tf.json line 118, in resource.aws_s3_bucket.alb_logs.lifecycle_rule:
118: "${local.lifecycle_rule}"
Either a JSON object or JSON array of objects is required here, to define
arguments and child blocks.
Error: Missing required argument
on terraform.tf.json line 118, in resource.aws_s3_bucket.alb_logs.lifecycle_rule:
118: "${local.lifecycle_rule}"
The argument "enabled" is required, but no definition was found.
Error: Incorrect JSON value type
on terraform.tf.json line 118, in resource.aws_s3_bucket.alb_logs.lifecycle_rule:
118: "${local.lifecycle_rule}"
Either a JSON object or JSON array of objects is required here, to define
arguments and child blocks.
Error: Incorrect JSON value type
on terraform.tf.json line 124, in resource.aws_s3_bucket.elb_logs.lifecycle_rule:
124: "${local.lifecycle_rule}"
Either a JSON object or JSON array of objects is required here, to define
arguments and child blocks.
Error: Missing required argument
on terraform.tf.json line 124, in resource.aws_s3_bucket.elb_logs.lifecycle_rule:
124: "${local.lifecycle_rule}"
The argument "enabled" is required, but no definition was found.
Error: Incorrect JSON value type
on terraform.tf.json line 124, in resource.aws_s3_bucket.elb_logs.lifecycle_rule:
124: "${local.lifecycle_rule}"
Either a JSON object or JSON array of objects is required here, to define
arguments and child blocks.
So the doc says that the result of the evaluation of the template should be an object, since it consists only of a single interpolation sequence. But judging by the error messages, it seems like Terraform is evaluating the template as a string.
Is this a bug in Terraform’s interpreter, or have I missed something?