In the previous guide in this series, we covered the how to sign up for an Amazon AWS. In this guide, we will start putting your new AWS account to use. The learning in this guide corresponds to the AWS Associate Exam Blueprint section 2.1.
Access the AWS Console
- Go to https://aws.amazon.com
- On the Sign In screen, enter your E-mail or mobile number, and password. Then click Sign in using our secure server.
- When your login completes, you should see the AWS Console.
Select an AWS Region
- Select the AWS Region where you want to create your VM instance.
Launch a Linux Instance
- Select Services→EC2.
- On the EC2 Dashboard, click Launch Instance.
- On Step 1: Choose an Amazon Machine Image, click Select next to the Amazon Linux AMI.
- On Step 2: Choose an Instance Type set, select t2.micro (Free tier eligible) and click Next: Configure Instance Details.
- On Step 3: Configure Instance Details, expand the Advanced details section.
- In User data, verify that As text is selected.
- Copy and paste the following text into the User data text box. The User data will execute as a script on the instance the first time that it starts. In this case, we will be installing a web server (httpd), configuring it to start at boot, and starting it.
- Click Next: Add Storage.
- By default, Step 4: Add Storage shows the volume required to boot the instance. You can optionally add addition volumes on this screen. Click Next: Tag Instance.
- On Step 5: Tag Instance, you can optionally add metadata to the Instance. Enter a Name for your instance and click Next: Configure Security Group.
- On Step 6: Configure Security Group, you can configure the firewall that filters network access to the instance. Since we want to run the HTTP service, we need to add the port that this service uses (port 80) to the security group.
- Confirm that Assign a security group is selected.
- Optionally, set a custom Security group name.
- Optionally, set a custom Description.
- Click Add Rule and set the Type to HTTP.
- Click Review and Launch.
- On Step 7: Review Instance Launch, you may see a security warning that your instance is open to the world. This is because we didn’t restrict the source IP range on the SSH rule. If this were a production instance instead of a training instance, the source range for SSH should be restricted. Review the details and click Launch.
- In the Key Pair Dialog Box, select Create a new key pair and enter a Key pair name. Then click Download Key Pair.
- Save the key pair to your disk.
- Click Launch Instances.
- From the Launch Status page, you View the launch log, Create Billing alerts and access other helpful resources. Billing alerts are useful to warn that you have exceeded the free usage tier. Click View Instances.
#!/bin/sh sudo yum -y install httpd sudo chkconfig httpd on sudo service httpd start
Retrieve Public DNS Name
- From the EC2:Instances:Instances screen, select your instance. Copy the Public DNS name.
OSX/Linux: Connect using SSH
- Launch the Terminal application.
- Execute the following commands. Substitute the PEM_filename and Public_DNS_Name as appropriate.
chmod 400 PEM_filename ssh -i ./PEM_filename ec2-user@Public_DNS_Name
EXAMPLE: $ chmod 400 ./MyKeyPair.pem $ ssh -i ./MyKeyPair.pem firstname.lastname@example.org The authenticity of host 'ec2-52-41-19-6.us-west-2.compute.amazonaws.com (18.104.22.168)' can't be established. ECDSA key fingerprint is SHA256:eGSKzh2+W8bpKAAW3aGFhBwBKm7JItosdfj4eOii8bY. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'ec2-52-41-19-6.us-west-2.compute.amazonaws.com,22.214.171.124' (ECDSA) to the list of known hosts. Last login: Tue Oct 4 00:07:30 2016 from 126.96.36.199 __| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2016.09-release-notes/ No packages needed for security; 3 packages available Run "sudo yum update" to apply all updates. $
- Confirm that the httpd service settings were automatically configured via the commands that you provided in User data.
- If you don’t see the expected results, you can troubleshoot the commands that you provided in User data by inspecting the
/var/log/cloud-init-output.logfile. If you made mistakes in the User data, you can either launch a new instance with corrected user data, or just repair the current instance by running the commands again.
- Enter the Public-DNS-Name of your instance in your browser. Confirm that Apache test page loads.
[ec2-user@ip-172-31-17-95 ~]$ yum list installed httpd Loaded plugins: priorities, update-motd, upgrade-helper Installed Packages httpd.x86_64 2.2.31-1.8.amzn1 @amzn-main [ec2-user@ip-172-31-17-95 ~]$ chkconfig --list httpd httpd 0:off 1:off 2:on 3:on 4:on 5:on 6:off [ec2-user@ip-172-31-17-95 ~]$ sudo service httpd status httpd (pid 2619) is running... [ec2-user@ip-172-31-17-95 ~]$
[ec2-user@ip-172-31-17-95 ~]$ cat /var/log/cloud-init-output.log ... Command line error: no such option: -i Usage: yum [options] COMMAND List of Commands: ... [ec2-user@ip-172-31-25-241 ~]$
For the most part, you can manage an EC2 instance as you would any other Linux host. For example, you can install additional applications with yum.
- Run the following commands to install PHP.
[ec2-user@ip-172-31-17-95 ~]$ sudo yum -y install php Loaded plugins: priorities, update-motd, upgrade-helper amzn-main/latest | 2.1 kB 00:00 amzn-updates/latest | 2.3 kB 00:00 Resolving Dependencies ... Installed: php.x86_64 0:5.3.29-1.8.amzn1 Dependency Installed: compat-gmp4.x86_64 0:4.3.2-1.14.amzn1 php-cli.x86_64 0:5.3.29-1.8.amzn1 php-common.x86_64 0:5.3.29-1.8.amzn1 Complete!
Access EC2 Metadata
You can access information about a running instance from inside the instance. For more information on this topic, refer to http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
- Run the following commands to retrieve the instance ID, local hostname, local IP, public hostname, and public IP.
curl http://169.254.169.254/latest/meta-data/instance-id ; echo curl http://169.254.169.254/latest/meta-data/local-hostname ; echo curl http://169.254.169.254/latest/meta-data/local-ipv4 ; echo curl http://169.254.169.254/latest/meta-data/public-hostname ; echo curl http://169.254.169.254/latest/meta-data/public-ipv4 ; echo EXAMPLE: [ec2-user@ip-172-31-17-95 ~]$ curl http://169.254.169.254/latest/meta-data/instance-id ; echo i-05bfae4f7cabd4471 [ec2-user@ip-172-31-17-95 ~]$ curl http://169.254.169.254/latest/meta-data/local-hostname ; echo ip-172-31-17-95.us-west-2.compute.internal [ec2-user@ip-172-31-17-95 ~]$ curl http://169.254.169.254/latest/meta-data/local-ipv4 ; echo 172.31.17.95 [ec2-user@ip-172-31-17-95 ~]$ curl http://169.254.169.254/latest/meta-data/public-hostname ; echo ec2-52-32-180-220.us-west-2.compute.amazonaws.com [ec2-user@ip-172-31-17-95 ~]$ curl http://169.254.169.254/latest/meta-data/public-ipv4 ; echo 188.8.131.52
Note that Amazon instances have both internal and external IP addresses. Instances also have both internal and external hostnames. When connecting to instances from the Internet, be sure you are using the external hostname and IP address.
Access EC2 Metadata via the Web Page
You may find it more useful to access the EC2 Metadata via a web page running on the instance. I add a page like this to all of my web servers. It’s invaluable in setting up and diagnosing load balanced web servers — we will do this in a later guide.
- Use your favorite text editor to create a new web page at
/var/www/html/index.phpthat contains the following. This new PHP page will override the Apache Test Page.
- Reload the Public-DNS-Name of your instance in your browser.
<?php $url = "http://169.254.169.254/latest/meta-data/"; $instance_id = file_get_contents($url . "instance-id"); $local_hostname = file_get_contents($url . "local-hostname"); $local_ipv4 = file_get_contents($url . "local-ipv4"); $public_hostname = file_get_contents($url . "public-hostname"); $public_ipv4 = file_get_contents($url . "public-ipv4"); echo "<b>instance-id:</b> " . $instance_id . "<br/>"; echo "<b>local-hostname:</b> " . $local_hostname . "<br/>"; echo "<b>local-ipv4:</b> " . $local_ipv4 . "<br/>"; echo "<b>public-hostname:</b> " . $public_hostname . "<br/>"; echo "<b>public-ipv4:</b> " . $public_ipv4 . "<br/>"; ?>
Congratulations! You have successfully created an Amazon EC2 Linux instance. You may find the following additional resources helpful:
- Launch a Linux Virtual Machine
- Getting Started with Amazon EC2 Linux Instances
- Running Commands on Your Linux Instance at Launch
- Installing a LAMP Web Server on Amazon Linux
- Instance Metadata and User Data
Continue to the next lab guide: AWS – Create an Amazon EC2 Windows Instance