Skip to main content

Automatically run bash script with every restart of AWS EC2 instance

We will be using User Data script to define our autorun script, which we intend to execute on every reboot or launch of our EC2 instance. I will be using this script to mount my elastic file system drive (EFS) and start to do application server configuration every time my server is rebooted and auto-scale using my autoscaling configuration.

To start here's little into of user data script. By default, user data scripts and cloud-init directives run only during the first boot cycle when an EC2 instance is launched. However, you can configure your user data script and cloud-init directives with a mime multi-part file. A mime multi-part file allows your script to override how frequently user data is executed in the cloud-init package. Then, the file executes the user script. For more information on mime multi-part files, see Mime Multi Part Archive on the cloud-init website.

Example of User Data script: 

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type: text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

!/bin/bash
/bin/echo "Hello World" >> /home/ec2-user/logscript.txt
yum update -y
/bin/echo "Hello World -2" >> /home/ec2-user/logscript.txt
mkdir /home/ec2-user/efs
/bin/mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 172.31.XX.XXX:/ /home/ec2-user/efs
/bin/echo "file server mounting completed, ready to start the server..." >> /home/ec2-user/logscript.txt
/home/ec2-user/efs/Tomcat/bin/startup.sh
/bin/echo "tomcat server started" >> /home/ec2-user/logscript.txt
--//

** important notes:- here I have created a file called 'logscript.txt' on my ec2 instance, to log all output of the script. As you can see, I am defining my bash script in this user data script (in Red). You can replace the Red text with your custom script. 
Add caption

Defining User Data on EC2:
  1. Go to your EC2 panel
  2. Select the instance, Go to Action --> Instance Settings --> View/Change User Data
  3. You can modify User data only when your instance is in the "Stop" state. Before stopping make use follow the process as stop may change your public IP address.
  4. Put the following script, click Save
  5. Go to Instance State and 'Start'  



You can verify the logscript.txt to check if your script executed or not. 

Happy deploying!

--
Deepesh Rajpal | c: 98282.38079 | rajpal.deepesh@gmail.com

Comments

Popular posts from this blog

Installing BEA Weblogic 10.3 on Mac/OSx

Updated post:- I have to go through a long hassle of making Weblogic work on my MacBook. Few things which I learned and would like to share: -Before starting to install you have to trick the installer into thinking that the local JDK is the generic Sun JDK. If you skip this step the installer will not accept the default Mac OS X JDK and complain that it is Invalid. $ cd /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home $ sudo mkdir -p jre/lib $ cd jre/lib $ sudo touch rt.jar $ sudo touch core.jar -To Install, run the following command from terminal : (Updated for the "fatal error occurred while installing 10.3.2") $ java -Xms256m -Xmx1024m -XX:PermSize=128m -XX:MaxPermSize=256m -jar -Dos.name=unix -jar server103_generic.jar -Once installation is completed, Create a new domain through "./config.sh" from "/bea/wlserver_103/common/bin" -Once the domain is created: --Need to update the env setting in " .setDomainEnv.sh " file i.e. locat...

DIABETES - ITS EARLY SYMPTOMS

Diabetes, especially Type-2 diabetes is often termed as the 'silent killer disease'. Why? The reason behind it is that this disease does not come with prominent symptoms. Sometimes this disease is not detected until the condition gets severe. However, there are certain symptoms of Type-1 and Type-2 Diabetes, which one should know, in order to identify this ailment at an earlier stage. Let us gain an elaborative insight about this chronic disease and the warning signs that it comes with. 1. Intensive Thirst One of the earliest symptoms of diabetes is peeing more than regular. A normal individual usually pees for 4-7 times in a day but a diabetic might do much more than that. This in turn also leads to intensive thirst as the person starts feeling much thirstier than usual. Why does this happen? This happen because the glucose, which your body reabsorbs in normal conditions, is not able to do so when your blood sugar level goes up. 2. Dry Mouth Lack of necessary amount of fluids...

DEPRESSION – HOW TO FIGHT IT

‘Depression’ is a condition which is quite prevalent these days courtesy a hectic lifestyle. One might think that this condition is an imaginary one where it is just all in the head, however, on the contrary, depression is a ‘real’ disease, which has an extremely adverse effect on the brain. There are several reasons, apart from changes in the brain chemistry, that lead to depression which include difficult life situations, stress or any other medical condition. There are many symptoms that are associated with this disease that are commonly found in people who are suffering from depression. These symptoms include: a) Angry Outbursts b) Mood Swings  c) Withdrawal from people d) Losing interest in any activity e) Immense Sadness f) Hallucination  g) Lack Of Sleep h) Concentration Problems  Depression is a common problem. Several studies suggest that one in every 5 women and one in every 10 men suffer from the problem of depression. How to fight this disease? Is ther...