Monday, September 17, 2018

AWS EC2 Instance (advance section script) with webserver and availability zone info

The use of this script requires basic knowledge of AWS and shell scripting. This is the script I normally use when I want to test something in my personal account and let the instance be powered off after 2 hours.

This script will bring up EC2 instance with following :

* updated packages,
* HTTPD service installed and started,
* Also run a job at startup to Poweroff instance after 2 hours (refer 7200)

(Note: Poweroff is kept only for those who need instance only for practicing and/or limited time and to save costs. In case of production use, comment out poweroff statement)

#!/bin/bash
yum update -y
yum install httpd24 -y
service httpd start
chkconfig httpd on
echo "Hello, this is a test instance from `curl http://169.254.169.254/latest/meta-data/placement/availability-zone`" > /var/www/html/index.html

echo "# This will bring up instance only for below mentioned seconds" >> /etc/rc.local
echo "(sleep 7200; poweroff) &" >> /etc/rc.local

No comments: