I am using the following code to hopefully retrieve the DNS name of the AWS EFS filesystem. The validation passes, the plan creation was fine but when I ran it, it errors out. I tried looking for the /tmp log file it mention but could not find them. The instance was not created so I was not able to ssh into the instance to have a look at the logs on the remote host.
If you are trying to pass this value into an EC2 instance, I’d recommend using user_data instead of provisioners, because then the data can travel over a channel provided by the EC2 API rather than all the complexity of connecting and logging in over SSH.
If you are using a common Linux distribution image with cloud-init installed (most do) then you can simply put a script to run on first boot directly in the user_data argument, and cloud-init will detect it and execute it for you:
cloud-init runs this script early during the boot process, and it runs as root so it should be able to write files anywhere on the system. cloud-config has some other capabilities too; if you configure it using its YAML format instead of just shell directly then you can directly ask it to create a file, giving more control over the ownership and permissions of that file:
An additional advantage of using cloud-init rather than provisioners is that by default cloud-init keeps a log of everything it did during boot, which you can analyze if things aren’t working as you expect:
cloud-init analyze show
Using that, it will likely be easier to debug what’s going on with creating your file in case you still have a similar problem after switching to using user_data with cloud-init. This easier troubleshooting is one of several reasons why Terraform provisioners are a last resort.
The file function interprets the content of the given file literally, so it will not perform any template processing.
However, the templatefile functiondoes interpret the given file as a template, using the map given as its second argument to provide the data. So for example you could write this to pass the EFS cluster name to the template:
Then the EFS hostname value in that template file would appear in expressions as efs_hostname instead of aws_efs_file_system.cluster_efs.dns_name.
If you intend to use the cloud config syntax instead of shell syntax like I showed in my second example in the previous comment, you can follow the advice under Generating JSON or YAML from a template by making your external file consist entirely of a call to jsonencode:
Thank you @apparentlymart , the templatefile approach worked out for my use case. I will continue with the templatefile rather than the jsonencode approach because there are a number of yum related commands I need to run and it feels more natural to run them via a script file.