Get some experience with EC2 User Data, Instance Metadata and S3 buckets
In this lab we will:
Log into RosettaHUB using the user name and password provided by RosettaHUB.
Click the "Go to AWS Console" button.
The AWS management console should open in a new browser tab. You may need to enable pop-ups from rosettahub.com. Click on Services near the top left and select EC2 from the Compute category.
This will bring you to the EC2 Dashboard. From here, click on Launch Instance.
Select the first Amazon Machine Image (AMI) listed, which runs Amazon Linux 2.
At Step 2: Choose an Instance Type, leave everything as it is (t2.micro) and click on Next: Configure Instance Details (not Review and Launch just yet).
At Step 3: Configure Instance Details, scroll to the bottom of the page and expand Advanced Details..
Paste the following into the User data text box:
#!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
Click on Next: Add Storage.
At Step 4: Add Storage, again leave everything as it is and click on Next: Add Tags.
At Step 5: Add Tags, click Add Tag and enter "Name" as the Key and "User data demo" as the Value. Then click on Next: Configure Security Group.
At Step 6: Configure Security Group page, you can use the rule you created in Week 0 to allow HTTP traffic
Click Review and Launch. At Step 7: Review Instance Launch page, click Launch.
You will now see a dialogue: "Select an existing key pair or create a new key pair". You can choose the key pair you created in Week 0. Click on Launch Instances.
Click the View Instances button at the bottom right of the Launch Status page. Take note of the public IP address of your newly-created instance.
After a few minutes, test that the web server is working by pointing a web browser to http://public_ip_addr (replacing public_ip_addr with the address of your instance). It will take a few minutes for the web server to be installed and started up.
Terminate the instance when you are finished.
Let's display some Instance metadata on our web page instead of the default Apache test page.
Repeat the previous step, replacing the User data with the following
#!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
echo "<h2>Test page</h2>Instance ID: " > /var/www/html/index.html
curl --silent http://169.254.169.254/latest/meta-data/instance-id/ >> /var/www/html/index.html
echo "<br>Availability zone: " >> /var/www/html/index.html
curl --silent http://169.254.169.254/latest/meta-data/placement/availability-zone/ >> /var/www/html/index.html
echo "<br>IP address: " >> /var/www/html/index.html
curl --silent http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
After a few minutes, test that the web server is working by pointing a web browser to http://public_ip_addr (replacing public_ip_addr with the address of your instance). It will take a few minutes for the web server to be installed and started up.
You should see some basic instance metadata displayed.
Terminate the instance when you are finished.
At the AWS management console, click on Services near the top left and select S3 from the Storage category.
From the Amazon S3 console dashboard, choose Create Bucket.
Type a bucket name in Bucket Name. The bucket name you choose must be globally unique across all existing bucket names in Amazon S3 (that is, across all AWS customers). For more information, see Bucket Restrictions and Limitations.
Leave the other fields unchanged and click Next
Click Next again at the Configure Options step
At the Permissions step, uncheck the first two checkboxes "Block new public ACLs and uploading public objects" and "Remove public access granted through public ACLs". This will allow us to upload an object that will be visible to anyone on the Internet.
Select our newly-created bucket and click Upload
Add an image of your choice (not something sensitive as this will be on the public Internet) and click "Next"
Under "Manage public permissions", choose "Grant public read access to this object(s)" and click "Next"
Click "Upload"
Select your newly-uploaded file and copy the "Object URL" to the clipboard. It will be of the form: https://s3-eu-west-1.amazonaws.com/bucketname/filename
You can paste this Object URL into a browser to verify that your image is public.
Repeat the steps 1/2, replacing the User data with the following.
(Don't copy and paste blindly! Insert your object URL on the last line.)
#!/bin/bash
yum update -y
yum install httpd -y
systemctl enable httpd
systemctl start httpd
echo "<h2>Test page</h2>Instance ID: " > /var/www/html/index.html
curl --silent http://169.254.169.254/latest/meta-data/instance-id/ >> /var/www/html/index.html
echo "<br>Availability zone: " >> /var/www/html/index.html
curl --silent http://169.254.169.254/latest/meta-data/placement/availability-zone/ >> /var/www/html/index.html
echo "<br>IP address: " >> /var/www/html/index.html
curl --silent http://169.254.169.254/latest/meta-data/public-ipv4 >> /var/www/html/index.html
echo "<hr>Here is an image that I have stored on S3: <br>" >> /var/www/html/index.html
echo "<img src=INSERT YOUR OBJECT URL HERE>" >> /var/www/html/index.html
After a few minutes, test that the web server is working by pointing a web browser to http://public_ip_addr (replacing public_ip_addr with the address of your instance). It will take a few minutes for the web server to be installed and started up.
You should see your image displayed.
Do not terminate this instance for now - we will ask you to show it to us to verify that you have successfully completed this.