Introduction
In my previous IAM article we learnt basics of IAM and how to create Users, Groups and attach Policies. You can refer here: https://dev.to/kadhamvj23/aws-identity-and-access-management-explained-for-beginners-cn7
After setting up secure access to our AWS account using IAM, the next question we mostly have is where do we actually run our application?
The answer is Amazon EC2 - Elastic Cloud Compute.
EC2 is one of the most widely used AWS services and understanding it well is essential for anyone starting their cloud journey. In this article we will cover what EC2 is, why it exists, the different types of instances, pricing models, Regions and availability Zones and finally hands-on walk through of creating your first EC2 instance.
Breaking Down the Name -EC2
Let us understand what each word in the name actually means:
Elastic --> In AWS you will notice many services have this prefix "Elastic". The reason is simple. Whenever AWS provides a service that can be scaled up or scaled down based on our needs, that service is called Elastic. With EC2 you can increase resources when traffic is high and decrease them when the traffic is low.
So in simple terms EC2 = A virtual server on the cloud that you can resize anytime.
Cloud: EC2 runs on AWS's public cloud infrastructure, meaning the servers are owned and managed by Amazon across the world.
Compute: The word compute means you are asking AWS to provide you CPU, RAM and Disk - basically a virtual machine or server that can run your applications.
How does EC2 actually work?
When you request a Virtual server from AWS, here is what happens behind the scenes:
You request a virtual machine on AWS
⬇️
request goes to a Hypervisor(a software layer
sitting on top of physical servers that creates and manages VMs)
⬇️
Hypervisor creates your VM
⬇️
You get the access to your EC2 instance
You never touch any physical hardware. AWS manages all of that for you.
Why use EC2?
Imagine your company wants to host an application. The traditional approach would be:
- Buy physical servers
- Install a hypervisor
- Create VMs
- Provide access to employees
- manage timely upgrades
- Handle security patches
- Deal with hardware failures
This sounds manageable for 10 servers. But imagine doing this for 1000s of servers. Your entire day as a system admin would be consumed just keeping things running -not building anything new.
AWS EC2 solves this entirely
Instead of managing physical hardware, you simply:
- Go to AWS Console
- Launch an EC2 instance in minutes
- Pay only for what you use - PAYG
- Let AWS handle all hardware maintenance, upgrades and security
AWS takes care of the physical infrastructure so you can focus on your actual work.
Key Concepts You must Know Before Creating EC2 Instance
1. AMI - Amazon Machine Image
Before launching an EC2 instance, you will need to choose an AMI. Think of it as the OS template for your server.
Example: Amazon Linux, Ubuntu, Windows, Red hat
2. Key-Pair
When you create an EC2 instance, AWS gives you a key-pair - a set of two keys(public and private) used to securely connect to your instance.
AWS keeps the public key.
You can download and keep the private key(.pem file)
Without this key, you can't SSH into your instance.
Think of this as a digital lock and key - AWS puts the lock on the server and gives you the only key.
3. Security groups:
A Security Group acts as a virtual firewall for your EC2 instance. it controls which traffic is allowed in and out.
We will understand more about this in future articles when we deep dive more into Security Groups and Networking.
4. EBS: Elastic Block Storage
Every EC2 instance needs storage. EBS is the hard disk attached to your EC2 instance where your data, OS files & application files are stored.
Just like EC2, EBS is also elastic in nature, that can increase and decrease the disk size anytime without stopping your instance.
Types of EC2 instances:
AWS offers 5 types of EC2 instances. You can choose based on what your application needs. The following are the types:
1. General Purpose: Best for balanced CPU, RAM and storage like web servers, small apps
2. Compute Optimized: best for High CPU performance like gaming servers
3. Memory Optimized: for large amounts of RAM like Big databases, data analytics
4. Storage Optimized: For High disk rea/write speed like data warehouses, log processing
5. Accelerated Computing: For GPU based tasks like machine learning.
Depending on your application requirement you pick the right instance type. For the learning purpose in this complete blog I will be using the general Purpose EC2 instance.
EC2 Pricing Models
This is very important and often comes up in AWS certifications:
1. On-Demand:
- Pay per hour or per second
- No commitment
- best for: short term or unpredictable workloads
- Most expensive per hour but most flexible.
2. Reserved Instances
- You commit to using EC2 for 1 or 3 years
- Get up to 75% discount compared to On-Demand
- Best for steady, predictable workloads
3. Spot Instances
- You bid for unused AWS capacity
- Can be up to 90% cheaper than On-Demand
- But AWS can terminate your instance with 2 mins notice if they need the capacity back.
- Best for batch jobs, testing jobs, non critical tasks
4. Savings Plan
- flexible pricing model
- Commit to a specific amount of usage per hour
- Applies across EC2, lambda and other services
Regions and Availability Zones
AWS has data centers spread across the world organized as:
Regions -- A geographical area like Mumbai, US East, Singapore. AWS has multiple regions across the world. You can choose a region closest to your users for low latency.
Availability Zones(AZs) -- Within each region there are multiple Availability zones. Each AZ is essentially a separate data center with its own power, cooling and networking. This means if one AZ goes down your application can still run from another AZ.
EC2 Best practices
- Always attach a Security Group - never leave all ports open
- Never loose your key pair - you cannot recover it.
- Choose region closest to your uses.
- Stop instances when not in use - you are charged for running time.
Hands-On: Launching an EC2 Instance and Deploying Jenkins on AWS
Now, let’s move towards the practical implementation by launching an EC2 **instance and **deploying Jenkins on it.
By the end of this lab, you will successfully deploy your first application on AWS EC2.
Step 1: Login to AWS Console
- Open your AWS account and log in to the AWS Management Console.
- In the search bar at the top, type EC2 and click on the EC2 service.
- You will now be redirected to the EC2 Dashboard.
Step 2: Launch a New EC2 Instance
On the left-hand side panel:
- Click on Instances
- Then click on Launch Instance (top-right corner)
- You will now enter the Launch Instance configuration page.
Step 3: Configure the EC2 Instance
1. Give Your Instance a Name: Under the Name and Tags section, provide a name for your instance. Example: My-First-Instance
2. Choose an Operating System: Under Application and OS Images (Amazon Machine Image) select the operating system.
For this practice, I selected: Ubuntu
3. Create a Key Pair: The Key Pair is extremely important because it is used to securely log in to your EC2 instance.
- Click on Create new key pair
- Give the key pair a name
- Download the .pem file
Note: Keep this .pem file safe because it cannot be downloaded again.
And for now, we will not touch the security part and other parts, just keep it as it is and then click on "Launch Instance".
Step 4: Verify the Instance is Running
Once the instance is launched:
- Open the instance
- Ensure the Instance State is Running
- Click on the Instance ID to view complete details.
- We will use the Public IPv4 Address to access the server and application. Example: 32.197.45.191
Step 5: Connect to EC2 Instance Using SSH
Open your terminal.
Depending on your operating system, you can use:
- Git Bash (Windows)
- PuTTY
- MobaXterm
- Default Terminal (Mac/Linux)
For this practice, I will use Git Bash.
Navigate to the Download Folder: Move to the folder where the .pem file was downloaded like below image.
Connect to the EC2 Instance
Run the following command:
ssh -i test-user1.pem ubuntu@32.197.45.191
Step 6: Fix Permission Error for .pem File
While connecting, you may get an error like: " Permissions for 'test-user1.pem' are too open "
This happens because the .pem file contains sensitive credentials and requires restricted permissions.
Use the following command to change file permissions:
chmod 600
"chmod" command is used to change the permissions of the file.
Now reconnect:
ssh -i test-user1.pem ubuntu@32.197.45.191
You should now successfully log in to the EC2 instance.
Step 7: Verify Current User
To check the current logged-in user, type "whoami in the terminal.
The output will be "ubuntu"
Step 8: Switch to Root User (Optional)
To become the root user type:
sudo su -
Now you will have root access. As shown in below image.
Step 9: Update Packages
Before installing any software, always update the server packages.
If Logged in as Ubuntu User
sudo apt update
If Logged in as Root User, type "apt update"
Once this is done, lets now try to deploy an Jenkins application from here.
Step 10: Install Jenkins
Now let’s deploy an application on the EC2 instance.
We will install Jenkins.
Visit the official Jenkins website
Copy the Ubuntu installation commands from the website and execute them in the terminal.
Step 11: Verify Jenkins Service Status
After installation, check Jenkins status:
systemctl status jenkins
If Jenkins is not active, also if you are getting any error for inactive you can also check the java version, it works best with java 21 version. So if the java isn't in this version try updating the version.
Also restart the service:
systemctl restart jenkins
Then check the status again.
Step 12: Access Jenkins from Browser
Copy the Public IP Address from your EC2 instance.
Open a browser and access Jenkins using:
http://<Public-IP>:8080
Initially, the application may not open because port 8080 is blocked in the Security Group
Step 13: Configure Security Group for Jenkins
To allow access to Jenkins:
- Open the EC2 Instance
- Scroll down to the Security section
- Click on the attached Security Group
Under Inbound Rules:
- Click Edit Inbound Rules
Add a new rule with:
Type Port Range Source
Custom TCP 8080 Anywhere IPv4
Then click: Save Rules
Step 14: Open Jenkins Again
Now refresh the browser:
http://<Public-IP>:8080
Step 15: Retrieve Jenkins Initial Password
Run the following command inside the EC2 terminal:
cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the password and paste it into the Jenkins browser page.
You will now enter the Jenkins dashboard.
Congratulations! 🎉🥳
You have successfully:
- Launched your first EC2 instance
- Connected to it using SSH
- Managed secure access using a .pem key
- Installed Jenkins
- Configured Security Groups
- Deployed and accessed your first application on AWS
I hope this article helped you in knowing the basics of EC2 and and also how to deploy an Application in EC2.
Let's meet in next article with another service of AWS!!!






Top comments (0)