[Recipes] EC2 Bootstrap Scripting


While starting an EC2 instance, run script to install apache server, copy the index.html from s3 to the html folder of apache server, and finally start apache httpd server.

Solution Summary: 

 You can run any script while starting an EC2 instance through bootstrap scripting.

Solution Steps: 

  1. Create an index.html file (refer to previous labs).

  2. Login to AWS console and go to S3.

  3. Create a bucket: Give any name (e.g. buddywebsitebucket), select a region same as the region you will be launching EC2 instance.

  4. Upload index.html file into the bucket.

  5. Login to AWS console and go to IAM:

    1. Go to Roles (side menu)

    2. Create a role (if not already created in previous lab):  Give any name, select role type as Amazon EC2, Attach following policies: AmazonS3FullAccess.

  6. Go to EC2 console.

  7. Launch an EC2 instance:

    1. Select Amazon Linux AMI, t2 micro,

    2. In configure instance details,

      1. select the role we created against IAM roles

      2. Click on Advanced Details, Select User Data option as text, and enter below details:

        1. #!/bin/bash

        2. yum install httpd -y

        3. yum update -y

        4. aws s3 cp s3:// buddywebsitebucket/index.html var/www/html/

        5. service httpd start

        6. chkconfig httpd on

    3. tag as (Key: Name, Value:DemoEC2-WithBash), use existing web dmz security group (or create one if needed), use existing EC2 key-pair(or create one if needed).

  8. Copy public IP and run in browser to see the web page.

  9. Remove the EC2 instance.

  10. Remove the s3 buckets created.


Exam Tips

  1. It is possible to have an S3 bucket that is in different region than that of EC2, but the script may be bit different from above. 

  2. To copy entire contents of the bucket, you can use command as:

    1. aws s3 cp s3:// buddywebsitebucket var/www/html/ --recursive

Recipe Tags: 

Learn Serverless from Serverless Programming Cookbook


Please first use the contact form or facebook page messaging to connect.

Offline Contact
We currently connect locally for discussions and sessions at Bangalore, India. Please follow us on our facebook page for details.
WhatsApp (Primary): (+91) 7411174113
Phone (Escalations): (+91) 7411174114

Business newsletter

Complete the form below, and we'll send you an e-mail every now and again with all the latest news.


Cloudericks.com is my blog to share notes and learning materials on Cloud and Data Analytics. My current focus is on Amazon Web Services.

I like to write. I try to document what I learn and share with others. I believe that knowledge is useless unless you share it; the more you share, the more you learn.

Recent comments

Photo Stream