How to create a Makefile with a templatefile?

Hello,

I am trying the create the following Makefile locally on a server created through terraform,

template user_data.sh.tpl creates a user_data shell script created at the start of the server.
One of the things this script does is to create a Makefile :

[...]
cat << EOF > /opt/Makefile
### DB  helpers
echo_db_dump:
  jq '.db' config.json
  date '+%Y-%m-%d_%H-%M-%S%z_%Z'
  echo "pg_dump $$$(jq '.db' config.json) > db-$$$(shell date '+%Y-%m-%d_%H-%M-%S%z_%Z').sql -Fc"
  echo "tar cvzf <>.tgz <previousfile.sql>
EOF

data "template_file" "userdata" {
    template = "${file("${path.module}/files/user_data.sh.tpl")}"
}

[...]
  user_data = data.template_file.userdata.rendered
[...]

template resulting Makefile…

echo_db_dump:
  jq '.db' config.json
  date '+%Y-%m-%d_%H-%M-%S%z_%Z'
  echo "pg_dump 1050 > db-1050.sql -Fc"
  echo "tar cvzf <>.tgz <previousfile.sql>

so my question is :slightly_smiling_face: how should I escape the Makefile $$() so that they are actually printed by the shell script, and not interpreted anytime before ?

Thanks in advance to anyone with some clues…

Hi @copolycube,

The character sequence $( is not special in Terraform’s template language. Only the combination ${ (with an opening brace rather than a parenthesis) is recognized by Terraform as the beginning of an interpolation sequence.

Therefore as far as Terraform is concerned no special escaping should be needed here. I can’t say whether any further escaping would be needed for the underlying Makefile language itself.

1 Like

Ah!
Thanks a lot, this will help me debug this :slight_smile: