How to use a context of data template_file in another resource?

Hi everyone! Could you help me? I’m a little confused.
Is there a way to use the context of data template_file resource without EOT tags?

So I have created Nginx Ingress in EKS from .yaml file. In turn, a load balancer appeared, but the terraform state knows nothing about it. And I created null_resource which getting the NLB description and sends the NLB ARN to file.

null_resource

resource “null_resource” “load_balancer_arn” {
provisioner “local-exec” {
command = “aws elbv2 describe-load-balancers | jq -r ‘.LoadBalancers.LoadBalancerArn’ > nlb_arn.tpl”
}
}

Then I created the data template_file where I set nlb_arn.tpl, created in the null_resource.

Data template_file

data “template_file” “nlb_arn” {
template = “${file(“templates/nlb_arn.tpl”)}”
}

And when I try to use this context in another resource I get the error about invalid format.

Resource with ARN

resource “aws_api_gateway_vpc_link” “vpc_link” {
name = var.vpc_link_name
target_arns = [data.template_file.nlb_arn.template]
}

When I use this case I get output like:

EOT
arn:1111111:111111111:11111111
EOT

So how to remove EOT tags and use only sting with arn?
(terraform output -json nlb_arn - provides necessary value without EOT, but no way to use it in the resource)

Thanks!

Hi @hlebauhusevichart,

The fact that Terraform is rendering the string onscreen using the multi-line string syntax suggests that you have at least one newline character in the string.

It can be difficult to avoid including a final newline character when rendering a template from an external file because many text editors and other tools expect text files to always end with a newline character, which Terraform will then include as part of the template.

If you want to use an external file to represent this template then it might help to pass the result through trimspace, which will remove any space characters – which includes newline characters – from the start and end of the string.

Note that the hashicorp/template provider and its template_file data source have been deprecated for a while now. I would recommend using the built-in function templatefile instead, and not using the hashicorp/template provider at all.

1 Like