AWS EC2 ๐: The Primary Compute Service of AWS
Day 2 : Understanding the Basics of EC2 Instances, Configuration, and Deployment for Cloud Enthusiasts
In this article, Iโll be talking about the first name that comes to mind for anyone interested in AWS compute services: EC2 ๐โthe most popular service on AWS. Iโll dive into some fundamentals of EC2 ๐งโ๐ซ, explore key configuration terms โ๏ธ, and walk you through a practical demo of deploying a Jenkins application on an EC2 instance ๐ฏ. Along the way, Iโll include screenshots ๐ผ๏ธ and detailed explanations ๐.
Letโs begin our journey into the world of AWS compute ๐.
WHY AWS EC2 ??
Imagine youโre starting a small online store to sell handmade crafts. Traditionally, youโd need to:
Buy physical servers ๐ฅ๏ธ.
Find space to store them ๐ข.
Maintain and manage the hardware ๐ง.
Pay for electricity and cooling โกโ๏ธ.
This setup is not only expensive but also time-consuming, especially if your business is just starting out.
Why AWS EC2 Instead?
No Upfront Costs ๐ธ:
With EC2, you donโt need to buy servers. You can rent a virtual server for as long as you need and only pay for the time you use it.Quick Setup โ๏ธ:
You can launch an EC2 instance within minutes and have your website up and running. No waiting for hardware delivery or setup.Flexibility ๐:
If your business grows and you need more power, you can easily upgrade your EC2 instance. If demand drops, you can downgrade or stop using it altogether.Accessibility ๐:
Your online store runs in the cloud, so customers from anywhere in the world can access it anytime, without needing a local server.
With EC2, you can focus on growing your business instead of worrying about managing servers!
What is Amazon EC2?
An EC2 instance is a virtual server in the AWS Cloud
Amazon Elastic Compute Cloud (Amazon EC2) provides on-demand, scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 reduces hardware costs so you can develop and deploy applications faster. You can use Amazon EC2 to launch as many or as few virtual servers as you need, configure security and networking, and manage storage. You can add capacity (scale up) to handle compute-heavy tasks, such as monthly or yearly processes, or spikes in website traffic. When usage decreases, you can reduce capacity (scale down) again.
We create an instance (virtual server) in AWS which comes with a balance of compute, memory, network and storage depending upon the instance types in AWS.
Image Source : AWS EC2 Official Documentation
See, there are so many terms related to AWS EC2 ๐. If youโre preparing for the Certification Exam ๐ or just learning the theory, you can always refer to the official documentation ๐. However, here Iโll share the terms and information that I found most useful during my EC2 project walkthrough ๐.
Without much delay lets begin the demo project of Deploying Jenkins on EC2.
Step 1) Create an EC2 Instance ๐
Follow the screenshots below and try it yourself. Youโll quickly see that creating an EC2 instance is super simpleโjust a few clicks, and boom, your server is ready! ๐ปโจ
Each instance contain a instance name/tag; we can also attach additional tags
AMI (Amazon Machine Image): Preconfigured templates for your instances that package the components you need for your server (including the operating system and additional software).
You can use custom or predefined instances. For example, you can create an AMI (Amazon Machine Image) from snapshots of a previously configured machine ๐ฅ๏ธ๐พ. This way, instead of manually installing and configuring the instance again ๐ง, you can create a new instance directly using that snapshot AMI โก. In this demo, I am going with ubuntu
Make sure that what all you are using is FREE TIER ELIGIBLE
Instance Type: Various configurations of CPU, memory, storage, networking capacity, and graphics hardware for your instances.
There are basically five types of instances: General Purpose, Storage Optimized, Compute Optimized, Memory Optimized and Accelerated Computing. There are family types of instances like t2,t3,โฆ. which all comes under the above five key types
Then comes the most important part: the key pair ๐, which is essential for accessing your EC2 machine via SSH. Without this key pair, you cannot log into your EC2 instance from your local machine ๐ฅ๏ธ. Secure login information for your instances. AWS stores the public key and you store the private key in a secure place.
Iโve written a detailed article about the challenges I faced while trying to SSH into my EC2 machine. It covers valuable insights and offers guidance on how you can debug this issue if you encounter it too. Definitely worth a read! ๐โจ
"Remote Host Identification Has Changed"
Leave this network settings as default , only just enable the public IP option if not rest you can leave default like VPC (we will cover in future article) and security groups settings.
Security groups: A virtual firewall that allows you to specify the protocols, ports, and source IP ranges that can reach your instances, and the destination IP ranges to which your instances can connect. These are very important like sometimes you have deployed your website well on the EC2 instance but you cannot access it on some port assigned on the web then the reason is this security group settings only. I have a demo later how to change the security rules
Storage (EBS) :Persistent storage volumes for your data using Amazon Elastic Block Store (Amazon EBS). Leave it as default 8GB and click on Launch Instance
Boom! ๐ You have your EC2 machine up and running! Now, all that's left is to connect to the machine and install Jenkins ๐ ๏ธ.
Step 2) Log in to your EC2 Machine
Now follow these steps to log in to your freshly created machine
1. Find Your EC2 Instanceโs Public IP
Go to EC2 Dashboard > Instances.
Select the instance you want to SSH into and note the Public IP or Public DNS (IPv4).
2. Set Permissions for the Key File
Open a terminal or command prompt on your local machine.
Make sure your private key (.pem) file has proper permissions:
chmod 400 /path/to/your-key.pem
3. SSH into Your EC2 Instance
Use the SSH command to connect to your EC2 instance.
Open your terminal and run:
ssh -i /path/to/your-key.pem ec2-user@<your-ec2-public-ip>
Replace
/path/to/your-key.pem
with the actual path to your.pem
file.Replace
<your-ec2-public-ip>
with the Public IP or Public DNS of your EC2 instance.
For Ubuntu-based instances, the username might be
ubuntu
instead ofec2-user
, so you would use:ssh -i /path/to/your-key.pem ubuntu@<your-ec2-public-ip>
4. Accept the Fingerprint
- The first time you connect, you'll be asked if you want to continue connecting. Type
yes
and press Enter.
5. Youโre Logged In!
- Once connected, you should see the command line of your EC2 instance, and you can now execute commands, install software, etc.
Step 3) Set up Jenkins on your EC2 Machine
Now since your logged in, just go to the offical jenkins page of installing jenkins in a ubuntu machine.
#Install Java
sudo apt update
sudo apt install fontconfig openjdk-17-jre
java -version
#Install Jenkins
sudo wget -O /usr/share/keyrings/jenkins-keyring.asc \
https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key
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
sudo apt-get update
sudo apt-get install jenkins
#Verify Jenkins with the my output provided below
sudo systemctl start jenkins
sudo systemctl status jenkins
This output shows that Jenkins is active and running! ๐
Now, go to your web browser and enter the URL using your public IP along with port 8080
:http://your-public-IP:8080
Oh no, the site is inaccessible! ๐ซ
Remember the point about security groups! To make Jenkins accessible at port 8080, you need to update the security group.
Steps to update the security group:
Go to EC2 Dashboard > Security Groups in the AWS Management Console.
Select the security group associated with your EC2 instance.
Click on Inbound Rules and then Edit inbound rules.
Add a new rule:
Type: Custom TCP Rule
Port Range:
8080
Source: Anywhere (0.0.0.0/0) or restrict to your IP for more security.
Save the changes.
Once this is done, you should be able to access Jenkins on the public IP at http://your-public-IP:8080
. Happy Jenkins-ing! ๐
Go to the desired file location, as shown in the photo above, to retrieve your admin password. Once you have it, type the password into the box, and you'll be able to access Jenkins on your EC2 machine! ๐
I hope you enjoyed this demo, where I used screenshots to explain EC2 through a practical approach. In the future, I plan to create some video tutorials for the practical aspects. Until then, enjoy this screenshot-based practical learning of AWS!
Credits:
AWS Official Documentation
Jenkins Official Documentation