AWS EC2 ๐ŸŒ: The Primary Compute Service of AWS

AWS EC2 ๐ŸŒ: The Primary Compute Service of AWS

Day 2 : Understanding the Basics of EC2 Instances, Configuration, and Deployment for Cloud Enthusiasts

ยท

7 min read

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:

  1. Buy physical servers ๐Ÿ–ฅ๏ธ.

  2. Find space to store them ๐Ÿข.

  3. Maintain and manage the hardware ๐Ÿ”ง.

  4. 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?

  1. 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.

  2. Quick Setup โš™๏ธ:
    You can launch an EC2 instance within minutes and have your website up and running. No waiting for hardware delivery or setup.

  3. 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.

  4. 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.

Each EC2 instance type provides a balance of compute, memory, network, and storage resources.

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

๐Ÿ’ก
Tags play a very important role in organisations. I will definitely talk about this in my upcoming articles.

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 of ec2-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.

Official Installation Guide

#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:

  1. Go to EC2 Dashboard > Security Groups in the AWS Management Console.

  2. Select the security group associated with your EC2 instance.

  3. Click on Inbound Rules and then Edit inbound rules.

  4. 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.

  5. 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

ย