14

I am trying to deploy a shellscript script to install a program when autoscaling triggers on aws elastic beanstalk.

I have searched on google and it all points to the creation of .ebextensions to use a config file to install the program upon start up May i know how do i create this?

Many thanks for the help in advance

AWSLearner
  • 155
  • 1
  • 1
  • 6
  • What program do you want to install? Depending on your answer, there are several approaches to install stuff upon boot. – Tal Nov 10 '14 at 09:26
  • Hi Tal, I have subscribed to trend micro`s deep security as a service. They have provided me with a startup script. I wish to launch this start up script when an instance spawns due to autoscaling – AWSLearner Nov 11 '14 at 04:40

2 Answers2

14

You need to do the following (obviously catering for your requirements):

In the root on your packaged software create a folder called .ebextensions

drwxr-xr-x 4 root root     4096 Sep 30 13:39 .
dr-xr-x--- 7 root root     4096 Nov  4 15:22 ..
drwxr-xr-x 2 root root     4096 Nov  4 15:22 .ebextensions

In the .ebextensions folder create two files called 02_files.config and 03_container_commands.config

-rw-r--r-- 1 root root 11334 Jul 25 12:26 02_files.config
-rw-r--r-- 1 root root   960 Nov  4 11:22 03_container_commands.config

The contents of 02_files.config should contain the contents of your shell script:

files:

  "/path/to/your/shellscript/myscript.sh" :
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/sh
      echo "Hello World!" >> /var/log/myscript.out

The contents of 03_container_commands.config should contain the command to run your shell script:

container_commands:

  01_runmyshellscript:
    command: "/path/to/your/shellscript/myscript.sh"

Now when you upload your code it will create your shell script in /path/to/your/shellscript/myscript.sh and then execute it sending Hello World! to /var/log/myscript.out

George Rushby
  • 1,305
  • 8
  • 13
  • Correct me if i`m wrong, i am supposed to create the .ebextensions on my elastic beanstalk instance itself? As my application itself is hosted by trend micro – AWSLearner Nov 11 '14 at 04:41
  • You shouldn't create anything on the elastic beanstalk instance. You create the directory on you local machine, zip it, and upload that zip to EB via the console. You'll find it under Application Versions – George Rushby Nov 11 '14 at 08:43
  • 1
    @GeorgeRushby Is it mandatory to name the files as you have given in the answer? Like "02_files.config" and "03_container_commands.config" ? – Sandun Mar 10 '21 at 05:29
  • 1
    @Sandun it isn't but it helps me understand the order in which the various stages get executed. In this case, all files will be created before container commands are run. You can read more about the process order here https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html – George Rushby Mar 10 '21 at 06:41
7

You need to understand that Elastic Beanstalk is no magic. It simply launches EC2 servers and runs a bunch of shell scripts on them in a specific order. Once you realize that, you can ask Elastic Beanstalk to execute your script as well when it kick starts the server. Full documentation of how to ask EB to execute your stuff is here.
In your case, you need to ask EB to execute a container_commands which is basically a script to be run after your application is set up.
So first thing is to add the trend micro script to your application sources so it can be found at runtime. let's assume it's at scripts/trendmicro.sh Then, asking EB to execute a script is as simple as adding a file to your application sources.

You can add a file named /.ebextensions/container_commands.config with the following:

container_commands:

  trent_micro_startup:
    command: "scripts/trendmicro.sh"
Tal
  • 7,827
  • 6
  • 38
  • 61
  • "scripts" is in the root of the zip file you upload? – red888 Dec 16 '16 at 21:22
  • @red888 Yes, in Tal's example the scripts directory would be in the root of your project. You would commit your EB config files/scripts to source management just like your other code. – Ethan Hohensee Jan 28 '18 at 14:11