Reputation: 125
I have multiple modules, and in each module there is some image with version updated in locals.tf. also have descriptor.json file where these same image versions should be updated.
Now, I wanted to ask if there is any way to use same locals.tf version to version used in descriptor.json.(without hardcoding)
Is there any way we can use variable in descriptor.json maybe?
//locals.tf
locals {
xyz_image = "/image.path"
xyz_image_version = "1.0.0"
}
//descriptor.json
{
"service": "xyz",
"external-resources": {
"images":
[
{
"name":"xyz_image",
"version":"1.0.0" //version that should match with locals.tf version
}
]
Upvotes: 0
Views: 2005
Reputation: 18108
For this you could use the templatefile
built-in function [1]:
resource "local_file" "descriptor" {
content = templatefile("${path.module}/descriptor.json.tpl",
{
image_version = local.xyz_image_version
}
)
filename = "${path.module}/descriptor.json"
}
The JSON template file (descriptor.json.tpl
) would then have to look like:
{
"service": "xyz",
"external-resources": {
"images":
[
{
"name":"xyz_image",
"version":"${image_version}"
}
]
}
The image_version
in the templated file is a placeholder variable which will be replaced by the value provided in the templatefile
function call. The templated file will be used to create a file and save it locally in the module path with the name descriptor.json
. You can add arbitrary number of variables to the templated file and as long as you provide the value for them in the function call, they will be replaced with that value. The JSON file can then be used as an input file where needed.
[1] https://www.terraform.io/language/functions/templatefile
Upvotes: 3