DEV Community

Harny Lawrence
Harny Lawrence

Posted on

🚀Diving into AWS EC2 – My First Hands-On Experience

Earlier, I laid the foundation for my DevOps journey by exploring AWS IAM and understanding the basics of cloud computing. This week, I took the next step: diving into AWS EC2 (Elastic Compute Cloud) and deploying my first application. Here’s a detailed recap of what I learned, the challenges I faced, and how I overcame them.

🔹 What is AWS EC2?
EC2 is one of AWS’s core services, allowing you to launch and manage virtual servers (instances) in the cloud. Here’s what makes EC2 powerful:

Elastic: Instances can be scaled up or down based on demand.

Compute: Provides CPU, RAM, and storage resources.

Cloud: Hosted on AWS’s infrastructure, eliminating the need for physical servers.

Types of EC2 Instances
AWS offers a variety of EC2 instance types optimized for different workloads. Here are the five major types:

General Purpose: Balanced compute, memory, and networking (e.g., t2.micro).

Compute Optimized: Ideal for compute-intensive tasks like batch processing.

Memory Optimized: Designed for memory-intensive applications like databases.

Storage Optimized: Optimized for high-speed storage and low-latency workloads.

Accelerated Computing: Uses hardware accelerators like GPUs for machine learning and graphics processing.

For my first instance, I chose the t2.micro (free tier eligible) because it’s cost-effective and perfect for learning.

Here’s a detailed step-by-step breakdown of how I launched my first EC2 instance and deployed Jenkins:

Step 1: IAM Best Practices
Before launching an EC2 instance, I ensured I followed AWS security best practices:

Created an IAM User: Instead of using the root user, I created a dedicated IAM user with limited permissions.

Enabled Multi-Factor Authentication (MFA): Added an extra layer of security to my account.

Assigned Policies: Granted the IAM user permissions to launch and manage EC2 instances.

Step 2: Launching the EC2 Instance

a. Navigated to the EC2 Dashboard:

Logged into the AWS Management Console and selected EC2 under 
Services.
Enter fullscreen mode Exit fullscreen mode

b. Clicked Launch Instance:

Chose Ubuntu as the operating system (a popular choice for DevOps
workflows).

Selected the t2.micro instance type (free tier eligible).

c. Configured Key Pair:

Created a new key pair (e.g., aws_login.pem) for SSH access.

Downloaded the .pem file and stored it securely.

d. Configured Security Groups:

Created a new security group or used the default one.

Added rules to allow SSH access (port 22) from my IP address.

e. Launched the Instance:

Clicked Launch Instance and waited for it to initialize.

Step 3: Connecting to the Instance via SSH

a. Opened Terminal:

Navigated to the directory where the .pem file was stored.

b. onnected to the Instance:

Used the following command:
ssh -i aws_login.pem ubuntu@

c. Fixed SSH Key Permissions:
Initially, I encountered an error: “Permissions for 'aws_login.pem'
are too open.”

Fixed it by restricting permissions using:

chmod 600 aws_login.pem

What is chmod 600?
This command sets the file permissions so that only the owner can read
and write the file. This is necessary for SSH to work securely.

d. Reconnected Successfully:

Ran the SSH command again and accessed the instance.

Step 4: Installing Jenkins

What is Jenkins?

Jenkins is an open-source automation server used for Continuous
Integration and Continuous Delivery (CI/CD). It helps automate the
building, testing, and deployment of applications.

<u>Why Do We Need Java?</u>
Jenkins is a Java-based application, so it requires Java to run. This 
is why we install Java before Jenkins.
Enter fullscreen mode Exit fullscreen mode

Installation Steps:

  1. Updated the Package List:
    sudo apt update
    Installed Java:

  2. sudo apt install openjdk-11-jdk
    Verified Java Installation:

  3. java -version

  4. Installed Jenkins:
    Added the Jenkins repository:

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo
tee /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc]
https://pkg.jenkins.io/debian-stable binary/ | sudo tee
/etc/apt/sources.list.d/jenkins.list > /dev/null
Updated the package list and installed Jenkins:

sudo apt update
sudo apt install jenkins
Started Jenkins:

sudo systemctl start jenkins
Enabled Jenkins to Start on Boot:

sudo systemctl enable jenkins

Step 5: Accessing Jenkins

a. Tried Accessing Jenkins:

Opened a browser and entered :8000.

The page didn’t load because the security group blocked port 8000.

b. Configured Security Group:

Went back to the EC2 dashboard and selected the instance’s security
group.

Added a new inbound rule:

Type: Custom TCP

Port Range: 8000

Source: 0.0.0.0/0 (for testing purposes; restrict this in production).

c. Accessed Jenkins:

Refreshed the browser, and the Jenkins setup page appeared.

Challenges Faced & Fixes

SSH Key Permissions:

Issue: The key pair was too open, causing SSH to reject it.

Fix: Used chmod 600 to restrict permissions.

Jenkins Not Running:

Issue: Jenkins didn’t start after installation.

Fix: Installed the correct version of Java and restarted the Jenkins service.

Port 8000 Blocked:

Issue: Couldn’t access Jenkins via the browser.

Fix: Added an inbound rule to the security group to allow traffic on port 8000.

Top comments (0)