Terraform local exec

I am using local -exec by running aws cli to retrieve list of lambdas to a file(since there is no aws resource to retrieve list of lambdas)

resource “null_resource” “get_lambda_func_name” {
provisioner “local-exec” {
command = “aws lambda list-functions --no-verify-ssl --region ap-south-1 --query ‘Functions[?starts_with(FunctionName, hel) == true].FunctionName’ > output.json”
}

It works fine, the output goes as json format into a file

output.json:

[
“hello”,
“hello2”,
“hello3”
]

But , how do I read it back this file back to terraform to make a “list” out of these values, I’ve used jsondecode(), but didn’t work. Is there any workaround for this?

Thanks in advance.

Hi @sasidharchalla,

It’s generally not recommended to try and generate files with terraform, and reference them within the configuration, because the file is not going to exist until after to apply the configuration, so reading it will fail in certain contexts.

If you want a resource which can execute arbitrary commands, there are some community providers like scottwinkler/shell that may work for your use case.

In this case however you may want to see if there is a data source from the aws provider itself which can read out the lambda functions without relying on shelling out to the the aws cli at all. If there is no such data source and the task of reading out a set of lambda functions is a common one, it may be worth submitting a feature requests to the provider team.

There is another option for edge use cases which don’t have provider support using the data external resource.

https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/data_source