Skip to content

AWS EC2 Basics

What is EC2?

Amazon Elastic Compute Cloud (EC2) is the building block of AWS compute offering, it provides scalable computing capacity in the cloud, allowing users to run virtual servers on-demand. It removes the need for upfront hardware investment. With EC2, we can:

  • Launch virtual servers, known as instances, in minutes.
  • Scale compute capacity up or down based on your needs.
  • Choose from a variety of instance types optimized for different workloads.

Key Concepts

To understand and use EC2, familiarize yourself with the following key concepts:

  1. Instances: Virtual servers running your applications.
  2. AMIs (Amazon Machine Images): Pre-configured templates for your instances.
  3. Instance Types: Define the compute, memory, and storage capacity of your instances.
  4. Security Groups: Virtual firewalls controlling inbound and outbound traffic.
  5. Elastic IPs: Static IP addresses for dynamic cloud computing.
  6. Key Pairs: Secure login to your instances using SSH.

Networking in EC2

Networking capabilities include creating isolated networks using Virtual Private Clouds (VPCs), securing access with Security Groups, and assigning Elastic IPs for consistent public IPs. These features ensure flexibility and security for your cloud environment.

Storage Options

EC2 supports multiple storage solutions:

  • EBS (Elastic Block Store): Persistent block storage that can be attached to instances.
  • Instance Store: Temporary, high-performance storage tied to the lifecycle of an instance.
  • EFS (Elastic File System): Scalable, shared file storage accessible by multiple instances.

Working with EC2

Step 1: Install and Configure AWS CLI

Before proceeding, ensure you have AWS CLI installed and configured on your local machine.

  1. Download and install the (Install AWS CLI).
  2. Configure the CLI with your credentials:
Terminal window
aws configure

Enter your access key, secret key, default region, and preferred output format (e.g., JSON).

Step 2: Create a Key Pair

Generate a key pair for secure SSH access:

Terminal window
aws ec2 create-key-pair --key-name nginx-server-key --query 'nginx-server-key' --output text > nginx-server-key.pem
chmod 400 nginx-server-key.pem

Step 3: Launch an EC2 Instance

Run the following command to create an EC2 instance:

Terminal window
aws ec2 run-instances \
--image-id ami-0abcdef1234567890 \
--instance-type t2.micro \
--key-name nginx-server-key \
--security-group-ids sg-0abc12345def67890 \
--subnet-id subnet-0abcd1234 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=nginx-server}]'

Replace the placeholders with appropriate values for your setup. Use aws ec2 describe-images to find suitable AMIs.

Step 2: Describe Instances and Get AMI ID

  1. List available AMIs in your region:

    Terminal window
    aws ec2 describe-images --owners amazon --filters "Name=architecture,Values=x86_64" "Name=virtualization-type,Values=hvm" --query 'Images[*].[ImageId,Description]' --output table

    Select an appropriate AMI ID from the output.

  2. Confirm your existing EC2 instances (if any):

    Terminal window
    aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]' --output table

Step 3: Setup Network Components

  1. Create a security group:
Terminal window
aws ec2 create-security-group --group-name NginxSecGroup --description "Nginx Server Sec group"

Note the Security Group ID from the output.

  1. Add a rule to allow SSH and HTTP access:
Terminal window
aws ec2 authorize-security-group-ingress --group-id <SecurityGroupId> --protocol tcp --port 22 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id <SecurityGroupId> --protocol tcp --port 80 --cidr 0.0.0.0/0
  1. Create a subnet (if needed) and note its ID:
Terminal window
aws ec2 describe-subnets --query 'Subnets[*].[SubnetId,AvailabilityZone]' --output table

Step 4: Create a Key Pair

Generate a key pair for secure SSH access:

Terminal window
aws ec2 create-key-pair --key-name MyKeyPair --query 'KeyMaterial' --output text > MyKeyPair.pem
chmod 400 MyKeyPair.pem

Step 5: Launch an EC2 Instance

Run the following command to create an EC2 instance:

Terminal window
aws ec2 run-instances \
--image-id <AMI_ID> \
--instance-type t2.micro \
--key-name MyKeyPair \
--security-group-ids <SecurityGroupId> \
--subnet-id <SubnetId> \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyInstance}]'

Replace the placeholders with appropriate values for your setup.

Step 6: Connect to Your Instance

Retrieve the public IP address of your instance:

Terminal window
aws ec2 describe-instances --instance-ids <InstanceId> --query 'Reservations[0].Instances[0].PublicIpAddress' --output text

Connect using SSH:

Terminal window
ssh -i MyKeyPair.pem ec2-user@<PublicIP>

Step 7: Install Nginx

  1. Update the package manager:

    Terminal window
    sudo yum update -y
  2. Install Nginx:

    Terminal window
    sudo amazon-linux-extras install nginx1 -y
    sudo systemctl start nginx
    sudo systemctl enable nginx
  3. Verify Nginx is running by visiting http://<PublicIP> in your browser.

Step 8: Terminate Your Instance

To avoid incurring charges, terminate your instance when done:

Terminal window
aws ec2 terminate-instances --instance-ids <InstanceId>

Conclusion

In this tutorial, we covered the essentials of AWS EC2, including key concepts, creating an instance via the AWS CLI, and deploying a basic web server using Nginx. This foundational knowledge equips you to explore different use cases for various workloads.