3

What is the way of execute a sh script into ec2 instance when terraform is building resources? I created an ami with some files in directory for execute, if i enter via ssh i can execute file as follow:

sh /home/resources/wso/bin/wso.sh

I have a start.tpl file with follow content:

#!/bin/bash

# update ubuntu
sudo apt-get update
# install nginx
sudo apt-get install nginx -y
sudo service nginx start

#start wso2
sh /home/resources/wso/bin/wso.sh

In my main.tf i have this:

data "template_file" "start" {
  template = "${file("start.tpl")}"  
}

resource "aws_instance" "wnginx" {
  ami                    = "${var.instance_ami}"
  instance_type          = "${var.instance_type}"    
  user_data = "${data.template_file.start.rendered}"
}

Nginx start good, but my start script wso.sh can't start.

Exist some configuration of terraform for debug my start.tpl?

mleaf
  • 145
  • 1
  • 1
  • 5
  • did you check the cloud-init output log on the instance? – Mike Oct 21 '18 at 11:16
  • Yes, i already check the cloud-init but the log is empty, i think save into text file the result of sh file ex: sh /home/resources/wso/bin/wso.sh > result.txt. can bee useful thank you – mleaf Oct 22 '18 at 19:48
  • I see my user_data in launch_configuration into ec2 console, but don't work when i started a group scale . – mleaf Oct 24 '18 at 03:38
  • I found a issue, when script launch i have this: "Caused by: java.net.SocketTimeoutException: connect timed out" , for some reason elb or auto escaling block egress connection or something block out connecion. – mleaf Oct 24 '18 at 06:26

3 Answers3

3

You are using user data the wrong way. Please see the cloud-init documentation.

Your template should then contain something like this (YAML Format):

#cloud-config
write_files:
- path: /home/resources/wso/bin/wso.sh
    content: |
    #!/bin/bash

    # update ubuntu
    sudo apt-get update
    # install nginx
    sudo apt-get install nginx -y
    sudo service nginx start
runcmd:
- ["sh", "/home/resources/wso/bin/wso.sh"]

Since this can be improved with Little effort, I would propose to use this as template:

#cloud-config
packages:
  - nginx
package_update: true
runcmd:
  - [systemctl, daemon-reload]
  - [systemctl, enable, nginx]
  - [systemctl, start, nginx]

It will achieve the same as your script but use the System provided to provision a machine, and also remove the need e.g. for maintaining your own AMI, as you could then just apply a cloud-init configuration via user data and rely on the Debian/Ubuntu Images.

If this did not work, you can verify /var/log/cloud-init.log. Since the file format is YAML, be aware of it being broken by using improper indention.

marenkay
  • 311
  • 1
  • 3
0

This seems to be not really terraform related. The userdata mechanism is a EC2 function. To debug userdata, you can check /var/log/ and have a look for files named cloud-init. There is at least on CentOS/AWS based images a file calledcloud-init-output which does contain stdout/stderr of the userdata script.

Another way would be to use terraforms remote-exec provisioner, which allow to execute commands from terraform on the remote machine.

https://www.terraform.io/docs/provisioners/remote-exec.html

hargut
  • 3,848
  • 6
  • 10
0

To verify the content of the rendered script you have several possibilities:

  • run terraform plan and verify the outputs
  • check the userdata from EC2 console
  • use e.g. a null_ressource with a local-exec provisioner which outputs the rendered template with e.g echo or cat.
hargut
  • 3,848
  • 6
  • 10