Introduction to Amazon EC2

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides resizable compute capacity in the cloud. It is designed to make web-scale cloud computing easier for developers. With Amazon EC2, you can launch virtual servers, configure security and networking, and manage storage.

Key Concepts

  1. Instances: Virtual servers that run applications.
  2. AMI (Amazon Machine Image): A template that contains the software configuration (OS, application server, and applications) required to launch an instance.
  3. Instance Types: Various configurations of CPU, memory, storage, and networking capacity for your instances.
  4. Regions and Availability Zones: Physical locations around the world where AWS data centers are located.
  5. Security Groups: Virtual firewalls that control the traffic for one or more instances.
  6. Elastic IP Addresses: Static IP addresses designed for dynamic cloud computing.
  7. Key Pairs: Secure login information for your instances.

Setting Up an EC2 Instance

Step-by-Step Guide

  1. Log in to the AWS Management Console:

    • Navigate to the EC2 Dashboard.
  2. Launch an Instance:

    • Click on the "Launch Instance" button.
  3. Choose an Amazon Machine Image (AMI):

    • Select an AMI from the list. For beginners, the Amazon Linux 2 AMI is a good starting point.
  4. Choose an Instance Type:

    • Select an instance type based on your needs. For general purposes, the t2.micro instance type is a good choice and is eligible for the free tier.
  5. Configure Instance Details:

    • Configure the number of instances, network settings, and other options. For beginners, the default settings are usually sufficient.
  6. Add Storage:

    • Specify the storage volume for your instance. The default settings are typically adequate for a basic setup.
  7. Add Tags:

    • Tags are key-value pairs that help you manage and organize your instances. This step is optional.
  8. Configure Security Group:

    • Create a new security group or select an existing one. Ensure that you allow SSH access (port 22) for Linux instances or RDP access (port 3389) for Windows instances.
  9. Review and Launch:

    • Review your settings and click "Launch". You will be prompted to create a new key pair or select an existing one. Download the key pair file (.pem) and keep it secure.
  10. Connect to Your Instance:

    • Once the instance is running, select it from the EC2 Dashboard and click "Connect". Follow the instructions to connect to your instance using SSH (for Linux) or RDP (for Windows).

Practical Example

Launching a Simple Web Server on EC2

  1. Launch an EC2 Instance:

    • Follow the steps above to launch a t2.micro instance with the Amazon Linux 2 AMI.
  2. Connect to Your Instance:

    • Use SSH to connect to your instance:
      ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-dns
      
  3. Install Apache Web Server:

    • Update the package index and install Apache:
      sudo yum update -y
      sudo yum install -y httpd
      
  4. Start the Web Server:

    • Start the Apache service and enable it to start on boot:
      sudo systemctl start httpd
      sudo systemctl enable httpd
      
  5. Verify the Web Server:

    • Open a web browser and navigate to your instance's public DNS. You should see the Apache test page.

Exercise

Task: Launch an EC2 instance and set up a simple web server.

  1. Launch a t2.micro instance using the Amazon Linux 2 AMI.
  2. Connect to the instance using SSH.
  3. Install and start the Apache web server.
  4. Verify that the web server is running by accessing the instance's public DNS in a web browser.

Solution:

  1. Launch the instance as described in the step-by-step guide.
  2. Connect to the instance:
    ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-dns
    
  3. Install Apache:
    sudo yum update -y
    sudo yum install -y httpd
    
  4. Start Apache:
    sudo systemctl start httpd
    sudo systemctl enable httpd
    
  5. Verify by navigating to the instance's public DNS in a web browser.

Common Mistakes and Tips

  • Security Group Configuration: Ensure that your security group allows inbound traffic on the necessary ports (e.g., port 22 for SSH, port 80 for HTTP).
  • Key Pair Security: Keep your key pair file secure and do not share it. If you lose it, you cannot connect to your instance.
  • Instance Type Selection: Choose an instance type that matches your workload requirements. The t2.micro instance is suitable for low-traffic applications and is free-tier eligible.

Conclusion

In this section, you learned about Amazon EC2, its key concepts, and how to launch and configure an EC2 instance. You also practiced setting up a simple web server on an EC2 instance. Understanding EC2 is fundamental for leveraging AWS's compute capabilities, and it sets the stage for more advanced topics in cloud computing.

© Copyright 2024. All rights reserved