Use Ternary expression and merge() like so resource "aws_cloudformation_stack" "tetra_resources" { name = "${local.name_prefix}-tetra-deployer-resources" template_body = jsonencode({ Resources = merge( { for pipeline in local.pipelines : pipeline["name"] => { Type = "AWS::CloudFormation::CustomResource" Properties = merge({ # The lambda function that will be invoked to deploy the pipeline ServiceToken = ... Config = ... }, # If dataweave is provided, then add the dataweave file to the template lookup(pipeline, "dataweave", null) != null ? { Dataweave = file("../config/${pipeline["dataweave"]}") # Get the last segment of path to get the filename if dataweave is provided DataweaveFileName = element(split("/", lookup(pipeline, "dataweave", null)), length(split("/", lookup(pipeline, "dataweave", null))) - 1) } : {} ) } }, ) }