I have a JSON template used for a resource in the terraform project. My problem is that I cannot successfully reference the value from a parameters block in my json template when it is deployed in Azure Portal logic app code view
For my resource (azapi_resource type Microsoft.Logic/workflows@2019-05-01, I have this body:
body = jsonencode({
properties = {
definition = jsondecode(templatefile("${path.module}/xyz.json",
{
.
.
uri = "https://****"
requestBody = var.params_obj
}))
parameters = {
.
.
uri = { value = "https://****" } #it is a string;
reqBody = { value = var.params_obj #it is an object
}
state = "Enabled"
}
})
In my variables.tf I have the var declared:
variable "params_obj" {
type = object({
pipeline = object({
calculation_date = string
name = string
abc = optional(object({
.
.
})
})
})
}
In my sample json xyz.json I have to reference the var.params_obj but is not working and it is putting in Logic App Code View as it is and not the Object values of my variable.
In my json sample xyz.json I tried to reference as below:
"inputs": {
"subscribe": {
"body": "${reqBody}" #not working; I tried also with "[parameters('reqBody')]" #I tried with another forms... not working;
,
"method": "POST",
"uri": "${uri}" #it works to reference the string uri value;
"unsubscribe": {}
My question is how to reference that reqBody correctly in my json template? because it is a bit strange.
My sample json contains definition and schema for Microsoft.Logic/workflows;
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#"
Since you didn't really provide a clear example that I could use to reproduce I have taken a guess at what you are trying to achieve and simplified the problem.
Assuming you want the request body as a valid json string in the template you need to use the jsondecode
function to turn the var into a json string before you try to use it in the template
main.tf
variable "params_obj" {
type = object({
pipeline = object({
calculation_date = string
name = string
abc = optional(object({
foo = string
}))
})
})
default = {
pipeline = {
calculation_date = "monday"
name = "chris"
}
}
}
output "this" {
value = templatefile("mytemp.tftpl", {
reqBody = jsonencode(var.params_obj)
uri = "bar"
})
}
template
{
"inputs": {
"subscribe": {
"body": ${reqBody},
"method": "POST",
"uri": "${uri}"
},
"unsubscribe": {}
}
}
OUTPUT
Outputs:
this = <<EOT
{
"inputs": {
"subscribe": {
"body": "{"pipeline":{"abc":null,"calculation_date":"monday","name":"chris"}}" ,
"method": "POST",
"uri": "bar"
},
"unsubscribe": {}
}
}
EOT
without using jsondecode
function and trying to pass the var directly to template I would get the error
│ Error: Error in function call
│
│ on main.tf line 20, in output "this":
│ 20: value = templatefile("mytemp.tftpl", {
│ 21: reqBody = var.params_obj
│ 22: uri = "bar"
│ 23: })
│ ├────────────────
│ │ while calling templatefile(path, vars)
│ │ var.params_obj is object with 1 attribute "pipeline"
│
│ Call to function "templatefile" failed: mytemp.tftpl:4,29-36: Invalid template interpolation value; Cannot include the given value in a string template: string required..