Problem:
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:
-
Create an index.html file (refer to previous labs).
-
Login to AWS console and go to S3.
-
Create a bucket: Give any name (e.g. buddywebsitebucket), select a region same as the region you will be launching EC2 instance.
-
Upload index.html file into the bucket.
-
Login to AWS console and go to IAM:
-
Go to Roles (side menu)
-
Create a role (if not already created in previous lab): Give any name, select role type as Amazon EC2, Attach following policies: AmazonS3FullAccess.
-
-
Go to EC2 console.
-
Launch an EC2 instance:
-
Select Amazon Linux AMI, t2 micro,
-
In configure instance details,
-
select the role we created against IAM roles
-
Click on Advanced Details, Select User Data option as text, and enter below details:
-
#!/bin/bash
-
yum install httpd -y
-
yum update -y
-
aws s3 cp s3:// buddywebsitebucket/index.html var/www/html/
-
service httpd start
-
chkconfig httpd on
-
-
-
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).
-
-
Copy public IP and run in browser to see the web page.
-
Remove the EC2 instance.
-
Remove the s3 buckets created.
Exam Tips
-
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.
-
To copy entire contents of the bucket, you can use command as:
-
aws s3 cp s3:// buddywebsitebucket var/www/html/ --recursive
-
Recent comments