AWS – Create an Amazon EC2 Linux Instance

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

  1. Go to
  2. On the Sign In screen, enter your E-mail or mobile number, and password. Then click Sign in using our secure server.
  3. When your login completes, you should see the AWS Console.
    AWS Console

Select an AWS Region

  1. Select the AWS Region where you want to create your VM instance.
    Select Region

Launch a Linux Instance

  1. Select Services→EC2.
    Open the EC2 Dashboard
  2. On the EC2 Dashboard, click Launch Instance.
    Launch Instance
  3. On Step 1: Choose an Amazon Machine Image, click Select next to the Amazon Linux AMI.
    Amazon Linux AMI
  4. On Step 2: Choose an Instance Type set, select t2.micro (Free tier eligible) and click Next: Configure Instance Details.
  5. On Step 3: Configure Instance Details, expand the Advanced details section.
  6. In User data, verify that As text is selected.
  7. 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.
  8. #!/bin/sh
    sudo yum -y install httpd
    sudo chkconfig httpd on
    sudo service httpd start

    Configure Instance Details

  9. Click Next: Add Storage.
  10. 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.
  11. 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.
  12. 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.
  13. Confirm that Assign a security group is selected.
  14. Optionally, set a custom Security group name.
  15. Optionally, set a custom Description.
  16. Click Add Rule and set the Type to HTTP.
    Review and Launch
  17. Click Review and Launch.
  18. 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.
    Review Instance Launch
  19. In the Key Pair Dialog Box, select Create a new key pair and enter a Key pair name. Then click Download Key Pair.
    Key Pair
  20. Save the key pair to your disk.
  21. Click Launch Instances.
  22. 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.

Retrieve Public DNS Name

  1. From the EC2:Instances:Instances screen, select your instance. Copy the Public DNS name.

OSX/Linux: Connect using SSH

  1. Launch the Terminal application.
  2. Execute the following commands. Substitute the PEM_filename and Public_DNS_Name as appropriate.
  3. chmod 400 PEM_filename
    ssh -i ./PEM_filename ec2-user@Public_DNS_Name
    $ chmod 400 ./MyKeyPair.pem
    $ ssh -i ./MyKeyPair.pem The authenticity of host ' (' 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 ',' (ECDSA) to the list of known hosts. Last login: Tue Oct  4 00:07:30 2016 from        __|  __|_  )        _|  (     /   Amazon Linux AMI       ___|\___|___| No packages needed for security; 3 packages available Run "sudo yum update" to apply all updates. $

Manage Apache

  1. Confirm that the httpd service settings were automatically configured via the commands that you provided in User data.
  2. [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 ~]$
  3. 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.log file. 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.
  4. [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 ~]$
  5. Enter the Public-DNS-Name of your instance in your browser. Confirm that Apache test page loads.
    Apache Test Page

Manage Linux

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.

  1. Run the following commands to install PHP.
  2. [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
      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    
    [ec2-user@ip-172-31-17-95 ~]$

Access EC2 Metadata

You can access information about a running instance from inside the instance. For more information on this topic, refer to

  1. Run the following commands to retrieve the instance ID, local hostname, local IP, public hostname, and public IP.
  2. curl ; echo
    curl ; echo
    curl ; echo
    curl ; echo
    curl ; echo
    [ec2-user@ip-172-31-17-95 ~]$ curl ; echo
    [ec2-user@ip-172-31-17-95 ~]$ curl ; echo
    [ec2-user@ip-172-31-17-95 ~]$ curl ; echo
    [ec2-user@ip-172-31-17-95 ~]$ curl ; echo
    [ec2-user@ip-172-31-17-95 ~]$ curl ; echo

    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.

  1. Use your favorite text editor to create a new web page at /var/www/html/index.php that contains the following. This new PHP page will override the Apache Test Page.
  2. <?php
      $url = "";
      $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/>";
  3. Reload the Public-DNS-Name of your instance in your browser.
    Instance Details Page


Congratulations! You have successfully created an Amazon EC2 Linux instance. You may find the following additional resources helpful:

Continue to the next lab guide: AWS – Create an Amazon EC2 Windows Instance

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.