In terraform, I'm using user_data to initiate a windows server instance. Locally, I have a folder with several files. Each file contains a single line.
In my user_data PowerShell script, I would like to create a single file containing the concatenation of all the lines in my local files.
An example is better than words:
locally, I have:
$ ls my_folder
file1
file2
$ cat file1
foo
$ cat file2
bar
In terraform user_data:
PS> New-Item C:\my_file
for file in my_folder
cat file in my_file
How can I do this in terraform?
Thanks
@Matt's answer should do the trick for you. In case you need a more structured approach, you can also take advantage of HashiCorp's
cloud-init
provider, which gives you more flexibility when building thecloud-init
config.To illustrate an example, this is the boilerplate project structure I used:
In
main.tf
, I use thecloudinit_config
resource do render theuser_data
from a template filebase.yml.tftpl
and pass the rendered contents to an instance.As I'm not too familiar with PowerShell, I kept the
base.yml.tftpl
quite generic. It demonstrates that you have access to both the filename and the file contents, should there be a need. I believe you can easily concat the file contents into a single string either by looping over the files and extracting the content or by using thejoin()
approach, as mentioned in the previous answer.