This post will help to write bootstrap script that will run Commands on Linux Ec2 Instance at Launch. This comes very handy in terms of pre-configuring your EC2 installation with common automated commands.
How to run Commands on Linux Ec2 Instance at Launch?
To take an example lets say you want to install an http server on AWS. There are two options to do it.
Option 1: Executing commands post installation of EC2 instance Like in this Post.
In this case we will be executing set of commands after installing EC2 instance which are required for setting up an web server on AWS. What if you want to do it prior to EC2 installation then follow Option 2.
Option 2: In this method we will use Bootstrap script that will run Commands on Linux Ec2 Instance at Launch.
In this case we will using bootstrap script or code that’s get executed automatically during EC2 instance lauch. Follow below steps for configuring the same.
Step 1: Launch an EC2 instance in EC2 dashboard .
Step 2: During 3rd step of Ec2 launch “Configure Instance” Click on Advanced Details link as below:
Step 3: In the User data box write down below set of commands for httpd server installation and follow as usual sequence of EC2 launch by clicking Add storage button.
#!/bin/bash yum update -y yum install httpd24 -y service httpd start chkconfig httpd on echo "<html><body><h1>Hello UxTechno, This is Automated</h1></body></html>" > /var/www/html/index.html
Note: Please make sure you open port 80 in security group for this EC2 instance without which we can’t connect to http URL.
Steps 4: After few seconds of Launching as EC2 instance your httpd server will be ready.In case you do not get expected result or want to check errors for bootstrap script kindly check “/var/log/cloud-init.log” file within EC2 instance.