Create an EC2 instance and connect via SSH

Introduction

In this project, you will launch an Amazon EC2 (Elastic Compute Cloud) instance and securely connect to it using SSH. EC2 allows you to run virtual machines (called instances) in the AWS Cloud. This is useful in the real world for:

  • Scalability: Instantly scale your compute capacity up or down based on demand.
  • Cost-effectiveness: The AWS Free Tier allows you to experiment without incurring charges if you remain within free usage limits.
  • Flexibility: You can choose various instance types and configurations for different workloads.

Prerequisites

  • AWS Account (Free Tier eligible)
    You must have an AWS account that can use the Free Tier. (Even though AWS typically requires a credit card on file, you will not be billed if you stay within the free usage limits.)
  • AWS CLI Installed
    The AWS Command Line Interface must be installed on your local machine. (This is used for automating or scripting AWS tasks.)
  • IAM User with Sufficient Permissions
    The IAM user you use for this project must have the required privileges to create and manage EC2 instances. (At minimum, the user needs the AmazonEC2FullAccess policy or equivalent custom permissions.)
  • Cloud Provider Services Enabled
    Make sure the EC2 service is available in your chosen region (e.g., us-east-1).

Step-by-Step Implementation

Below are the manual steps (through the AWS Console) and the CLI commands side by side for each phase. You can choose whichever method you prefer or do both to reinforce learning.

Step A: Configure AWS CLI (if using CLI)

  • AWS Console
    This step is not needed directly in the console, as the console uses your browser session.
  • CLI
    aws configure

Explanation:

This command sets up your AWS credentials and default region.

You will be prompted for:


AWS Access Key ID
AWS Secret Access Key
Default region name (e.g., us-east-1)
Default output format (e.g., json)

Step B: Create a Key Pair AWS Console (GUI)
Go to EC2 service in your AWS Console.
In the left navigation pane, under Network & Security, click on Key Pairs.
Click Create key pair.
Give it a name (e.g., my-ec2-keypair), choose RSA key type, .pem format.
Save the downloaded .pem file in a secure location on your local machine.

CLI :

aws ec2 create-key-pair \

--key-name my-ec2-keypair \

--query "KeyMaterial" \

--output text > my-ec2-keypair.pem

Explanation:

  • create-key-pair is the command to create a new key pair in AWS.
  • --key-name specifies the name for the key pair.
  • --query and --output store the private key portion directly into a .pem file.
  • The result is saved locally as my-ec2-keypair.pem.


Set Permissions (CLI only)

Explanation:
This ensures the private key file is not publicly viewable.
Required by SSH for security.



Step C: Launch an EC2 Instance

AWS Console (GUI)
Go to the EC2 dashboard.
Click Launch instances.
Name and tags: Enter a name for your instance (e.g., MyFreeTierInstance).
Application and OS Images (Amazon Machine Image): Select Amazon Linux 2023 or similar free tier eligible AMI.
Instance type: Choose t2.micro (Free Tier eligible).
Key pair (login): Select the key pair you just created (my-ec2-keypair).
Network settings:
Make sure you create (or select) a security group that allows SSH (port 22) inbound from your IP or from anywhere if you are just testing.
Leave storage to the default 8GB (within Free Tier limits).
Click Launch instance.


CLI :

aws ec2 run-instances \

--image-id ami-0c02fb55956c7d316 \

--instance-type t2.micro \

--key-name my-ec2-keypair \

--security-group-ids sg-1234567890abcdef0 \

--subnet-id subnet-12345678 \

--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=MyFreeTierInstance}]' \

--count 1

Explanation:

  • aws ec2 run-instances starts one or more EC2 instances.
  • --image-id selects the Amazon Machine Image (here, a sample Amazon Linux 2023 AMI for us-east-1; use an AMI that is Free Tier eligible in your region).
  • --instance-type t2.micro ensures free-tier eligibility.
  • --key-name references the key pair you created.
  • --security-group-ids sets the security group that allows inbound SSH. Replace with your actual Security Group ID.
  • --subnet-id indicates the subnet where your instance will launch. Replace with your subnet ID.
  • --tag-specifications sets the Name tag for your instance.



Step D: Check Instance Status AWS Console (GUI)

Return to your EC2 Instances page.
You should see your new instance in the list, with a status of Running once it is ready.
Wait for the Status Checks to turn 2/2 checks passed.


CLI :

aws ec2 describe-instances --filters "Name=tag:Name,Values=MyFreeTierInstance"

Explanation: describe-instances displays information about your instances. --filters helps narrow the results by matching the “Name” tag you assigned.

Step E: Connect to EC2 via SSH

Obtain Public IP (AWS Console or CLI) Console: On the Instances page, under Description, note the Public IPv4 address.

CLI: From the previous describe-instances output, find the field "PublicIpAddress": "YOUR_IP_HERE".

Connect using SSH : ssh -i /path/to/my-ec2-keypair.pem ec2-user@PUBLIC_IP_ADDRESS

Explanation: ssh -i specifies your private key. ec2-user is the default username for Amazon Linux. (Other AMIs may have different usernames like ubuntu or centos.) Replace PUBLIC_IP_ADDRESS with the actual IP you noted.

Conclusion

By doing this project , we have successfully:

  • Created an EC2 instance using both the AWS Console and the AWS CLI.
  • Generated a key pair and learned how to manage private keys securely.
  • Configured a Security Group to allow SSH access.
  • Connected to your instance via SSH, validating that it runs properly.


Skills Learned:

  • Navigating the AWS management console for EC2.
  • Using the AWS CLI to automate infrastructure tasks.
  • Handling key pairs and secure SSH authentication.
  • Basic understanding of security groups, instance states, and cloud resource management.

With these skills, you can further explore deploying software on your EC2 instance, scaling your environment, or automating more complex tasks.

What is Cloud Computing ?

Cloud computing delivers computing resources (servers, storage, databases, networking, and software) over the internet, allowing businesses to scale and pay only for what they use, eliminating the need for physical infrastructure.


  • AWS: The most popular cloud platform, offering scalable compute, storage, AI/ML, and networking services.
  • Azure: A strong enterprise cloud with hybrid capabilities and deep Microsoft product integration.
  • Google Cloud (GCP): Known for data analytics, machine learning, and open-source support.