Template_file bash not executing

Hello guys, i have a weird problem with my terraform template_file.

i am trying to automate a mongo replicaset, and i have a bash command script to execute this automation during the aws spin up instance, but for some weird reason, it appears that my bash file does not execute, and if i ssh inside my mongo instance, and input manually the code, it runs just fine.

this is the structure of my folder:

.
├── dev.terraform
├── dev.tf
├── output.tf
├── provider.tf
├── scripts
│   ├── init.cfg
│   └── volumes.sh
├── ssh
│   ├── aws
│   ├── aws.pub
│   ├── awssingle
│   ├── awssingle.pub
│   ├── hamza-elaouane-elkkey.pem
│   └── winetricks
├── template
│   ├── db
│   │   ├── init1.sh.tpl
│   │   └── init2.sh.tpl
│   ├── init1.sh.tpl
│   ├── init2.sh.tpl
│   └── init.sh.tpl
├── terraform.tfstate
├── terraform.tfstate.backup
├── variable.tf
└── versions.tf

4 directories, 21 files

and my bash command looks like this:

#!/bin/bash
exec &> /var/mylog
sleep 120;
sudo systemctl enable mongod
sudo systemctl start mongod



mongo --host mongodb://ip-10-0-1-100 --port 27017 --eval 'rs.initiate({_id: "rs0", members: [{_id: 0, host: "ip-10-0-1-100:27017", priority: 2}, {_id: 1, host: "ip-10-0-2-100:27017"}, {_id: 2, host: "ip-10-0-3-100:27017"}]})'
mongo --host mongodb://ip-10-0-1-100 --port 27017 --eval "db.isMaster().primary"
mongo --host mongodb://ip-10-0-1-100 --port 27017 --eval "rs.slaveOk()"


sleep 60; sudo systemctl restart metricbeat
sudo systemctl restart filebeat

sleep 180; sudo filebeat setup -e \
  -E output.logstash.enabled=false \
  -E output.elasticsearch.hosts=['10.0.105.100:9200'] \
  -E setup.kibana.host=10.0.105.101:5601 && sudo metricbeat setup

from my dev.tf in a template_file data i am calling the bash script:

data "template_file" "initdb1" {
  template = "./template/db/init1.sh.tpl"
}

data "template_file" "initdb2" {
  template = "./template/db/init2.sh.tpl"
}

in bash command i tried to exec the output to a mylog file so to try if its working, but nothing has been created and my code is not running.

I dont undersrtand what i am doing wrong as i dont have any error logs to troubleshoot. Any suggestin please?

If you need more info just please let me know, thank you very much

Hi @ElAouane,

I’m not sure I fully understand what you’re intending here. :confounded: I don’t see anything in what you’ve shared that would be attempting to execute the script. template_file just constructs a string by substituting some values into it, but your template doesn’t seem to include any template directives. :thinking:

Can you say a little more about your end goal here? For example, do you intend for this to be run on some virtual machine you’ve declared elsewhere in your Terraform configuration?

Hi, yeah you are right. What i was trying to do was totally wrong.

My attempt was to run a .sh file in a aws instance, but my template was pointing to a template instead of a “file”

so i had to change my:

data "template_file" "initdb2" {
  template = "./template/db/init2.sh.tpl"
}

to:

data "template_file" "initdb2" {
  template = file("./template/db/init2.sh.tpl")
}

Doing this i was able to run all the command inside my sh file.

Great! I’m glad you got it working.

By the way, using template_file without any vars is a bit redundant: it doesn’t do anything useful unless your file contains template directives, and I expect it may even make your life harder here because bash interpolation syntax like ${FOO} matches the Terraform template interpolation syntax, and so you’d end up needing to do extra escaping.

If you just need the literal contents of a file without any template processing, you can just use the file function directly where you need that data. That is, you can replace your data.template_file.initdb2 reference elsewhere in the configuration with file("${path.module}/template/db/init2.sh.pl") instead and get the same result without the data "template_file" block.

1 Like

unfortunatly if i change my user data to:

user_data = file("../dev/template/db/init2.sh.tpl")

and spin up my mongodb for the replicaset, it does not work. but if i run it as a template_file it works.

May i ask you a question. In one of my template_file. it looks like this:

#!/bin/bash

sleep 120

cd /home/ubuntu/AppFolder/app

export DB_HOST=mongodb://ip-10-0-1-100:27017,ip-10-0-2-100:27017,ip-10-0-3-100:27017?replicaSet=rs0


node seeds/seed.js

node app.js &

sudo filebeat modules enable nginx

this sh file, its a template_file linked to my app instance, after the spin up, if i ssh in my app instance and type:

printenv

i cant see:

export DB_HOST=mongodb://ip-10-0-1-100:27017,ip-10-0-2-100:27017,ip-10-0-3-100:27017?replicaSet=rs0

is this due to the fact that i am not exporting it as a env variable with $?