7

Hi EC2 instance is created, but commands as part of userdata.sh are not gettingexecuted. When I manually connect to EC2 via putty, i found that nginx is not installed in EC2 instance. To verify if the script is getting executed or not I added echo message, but no output is display in command prompt when i run terraform apply. How can i verify if the user-data is getting executed or not?

I have installed Terraform in C drive and below script are present in same folder C:/Terraform/userdata.sh, C:/Terraform/main.tf, i tried giving path as ${file("./userdata.sh")}" but still it does not work.

Please advice as I am just learning terraform. Thanks.

#!/bin/bash -v
echo "userdata-start"
sudo apt-get update -y
sudo apt-get install -y nginx > /tmp/nginx.log
sudo service nginx start
echo "userdata-end"

This is been called in my terraform program [main.tf] as below:

# resource "template_file" "user_data" {
#    template = "userdata.sh"
# }

data "template_file" "user_data" {
template = "${file("userdata.sh")}"
}

resource "aws_instance" "web" {
instance_type = "t2.micro"

ami = "ami-5e8bb23b"

key_name = "sptest"

vpc_security_group_ids = ["${aws_security_group.default.id}"]
subnet_id              = "${aws_subnet.tf_test_subnet.id}"

user_data               = "${data.template_file.user_data.template}"
#user_data              = "${template_file.user_data.rendered}"
#user_data              = "${file("userdata.sh")}"
#user_data              = "${file("./userdata.sh")}"


tags {
Name = "tf-example-ec2"
}
} 
Smi
  • 183
  • 1
  • 4
  • 12

3 Answers3

10

I could see one issue with the code you have posted, the user_data variable should be like

user_data = "${data.template_file.user_data.rendered}"

Moreover, as a suggestion i will recommend you to try creating a log file in your script to check what all steps have been executed. It will also benefit you to know whether the script ran at all or not.

One sample from our code, you can modify it based on your standards

logdir=/var/log
logfile=${logdir}/mongo_setup.log
exec >> $logfile 2>&1

Hope this helps.

Sorabh Mendiratta
  • 911
  • 12
  • 23
  • 6
    You don't need a separate log file, cloud-init already logs things under `/var/log/cloud-init-output.log`. Also user data runs as root so you don't need sudo. – ydaetskcoR Aug 17 '18 at 09:20
  • First I tried without using sudo , but still it did not worked – Smi Aug 17 '18 at 19:45
  • I agree with @ydaetskcoR that you do not need to specify sudo in your scripts. I missed that initially. Can you please share some logs etc so that we could check and suggest further, what could be the problem here. – Sorabh Mendiratta Aug 19 '18 at 23:52
  • Please find the logs here https://gist.github.com/smi2018/0c83424aa7acc93725759ac252c387c6 – Smi Aug 20 '18 at 18:11
  • 1
    Hey Guys, thanks . Issue got resolved. I used .tpl file instead of .sh. – Smi Aug 22 '18 at 20:50
  • 2
    can you mention your entire solution and mark that as answer? – Baskar Lingam Ramachandran Mar 20 '19 at 14:10
0

Why so complicated?

user_data = file("user_data.sh")

This file must exist near other tf.files of the project. That will be enough

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
0

just an alternate, create variable.tf with contents

variable "script" {
default = <<-EOF
#!/bin/bash -v
echo "userdata-start"
sudo apt-get update -y
sudo apt-get install -y nginx > /tmp/nginx.log
sudo service nginx start
echo "userdata-end"
EOF
}

call it in user_data = var.script and check apache2 status as sudo systemctl status nginx

Gautam Savaliya
  • 1,403
  • 2
  • 20
  • 31
konark111
  • 1
  • 3