Amazon EC2 is an abbreviation for Elastic Compute Cloud. It’s elastic in nature which means you can scale it at any point in time. It’s very hard to predict the compute power for application deployment. Sometimes, you need more compute power and sometimes less. In both cases, you have to decide the compute power well in advance. Because it takes a lot of time in the procurement of hardware resources and set them up.
AWS EC2 solves on-demand scalability issue of the On-premises deployment. In this tutorial, we will launch an AWS EC2 Linux instance to understand the concept.
What is Amazon EC2?
Amazon Elastic Compute Cloud (Amazon EC2) provides elastic computing capacity in the Amazon Cloud. When we say elastic that means it’s scalable at any point in time. With the Amazon EC2, you can launch as many virtual machines as you want. Which reduces the upfront cost of hardware purchase. You can grow or replicate your infrastructure services as your business grow.
AWS provides you with the web interface to launch the AWS EC2 instance. It provides ready to instantiate AMIs (Amazon Machine Images) to quickly launch an EC2 instance.
Service vs Instance
It may be confusing for beginners that sometimes we say service and sometimes instance. So, EC2 is web service provided by AWS but we use it through an instance like t2.large instance.
In general, a machine instance is a virtual machine running in the cloud. All applications that you deploy run on this virtual machine itself. These virtual computing environments are knowns as instances.
One of the key reason for using AWS EC2 is the opportunity to replace upfront infrastructure expenses with low variable costs that can scale with your business.
Let’s talk about the more exciting features that AWS EC2 offers.
- It provides the virtual computing environment known as instances.
- EC2 service is elastic in nature – Scale when your business grow.
- Pay-as-you-go pricing model.
- Preconfigured templates available for your instances, known as AMI (Amazon Machine Images).
- Web interface available to do the various configurations of CPU, memory, storage, and networking capacity.
- Elastic Ip addresses are available – Static Ip address for your dynamic cloud computing.
- Secure login for your instances with key pair (Public Key and Private Key).
- Persistent storage volumes for your data known as Amazon Elastic Block Storage (Amazon EBS).
- Highly Reliable – multiple physical locations for your data.
- Integrated Environment – EC2 is integrated with almost every service available on AWS. You do not need to do POCs (Proof of Concept) and research.
- No additional IT team is required to manage your deployment.
- You are not buying hardwares – You can release services whenever you want without worrying about the investment cost.
You will find many reasons why to use AWS EC2 when you will actually start using it. Tell us your favorite reason in the comment section.
EC2 Instance Types
AWS offers several EC2 instance types and each of them is for particular use cases. Each instance type offers a different memory, computer, and storage capabilities.
AWS grouped EC2 instances based on these capabilities and provides the below-listed instance types:
General Purpose Instance
These instances provide a good balance of memory, computer, storage, and networking resources. And can be used in a variety of use cases and workloads.
T2 and T3 Instances
T2 and T3 are the Burstable Performance Instances. These instances provide you the baseline CPU performance but have the ability to burst to a higher level based on the workload.
These are only instances which use the credits for CPU usage. Just for your information “One CPU credit is equal to one vCPU running at 100% utilization for one minute.”
These instance types are the perfect candidates for :
- Web Applications
- Code Repositories etc.
M4, M5, M5a, and M5d Instances
These instances are well suited for below type of applications:
- Web Servers
- Application Servers
- Small and Medium databases
- Gaming Servers
These instances are well suited for below type of applications:
- Distributed data stores
- Containerized Microservices
- Web Servers etc.
You can check the summary of hardware specification like default vCPUs, Memory (GiB) on AWS. Based on you hardware requirement you can choose the machine type.
Compute Optimized Instance
Compute-optimized instances focus on providing you the high-performance processors. These types of instances are ideal for compute-bound applications.
C series (C4, C5, C5d, and C5n) Instances
These are the good candidates for below types of applications:
- High-performance computing
- Batch Processing
- Compute Intensive Applications
- Machine Learning etc.
Memory Optimized Instance
As per the name suggests these are designed to provide you fast performance on the large dataset in memory. Memory Optimized instances are being used mainly for the database services.
R Series(R4, R5, R5a, and R5d) Instances
These types of instances are the good candidates for the below types of applications:
- High-performance relational and NoSQL databases like MySQL and MongoDB
- In memory caching – Redis and Memcache
- Big Data like Hadoop/Spark clusters
Accelerated Computing Instance
These instances provide very high processing capability for the GPUs (Graphics Processing Units). There are several instance types available under the Accelerated Computing Instance like F1, P2, P3, G2 and G3.
These are well suited for the below types of applications:
- Video Creation Services
- 3D Visualization
- Video Encoding
- Virtual Reality
- Deep Learning
- Molecular Modeling etc.
Storage Optimized Instance
Storage Optimized Instances are highly optimized to provide low latency, IOPS (I/O operations per second) to your application.
These instances are the good candidates for the below type of applications:
- MapReduce and distributed file system
- Require high-throughput access to the big chunk of data
D2 instances are the well suited for the below types of applications:
- Parallel Processing
- Log processing applications
- Data processing applications
I3 instances are the well suited for the below types of applications:
- Relational Databases
- NoSQL Databases
- In-Memory Databases (like Redis)
- Online Transaction processing (OLTP) systems
Instances from AMIs
AMI is an abbreviation for the Amazon Machine Image which provides the essential information to launch a virtual machine in cloud. You can launch multiple identical instances from an AMI.
An AMI contains three things:
- Template – For the root volume for the instances (An application server, an OS, and applications)
- Permissions to Launch – Which account can use this AMI to launch instances
- Block device mapping
We will discuss the Amazon Machine Images in detail in a separate post. There we will learn how to create, register, use and deregister the AMIs.
Setup AWS EC2
Most of the services provided by AWS are linked together. So, let’s launch a Linux EC2 instance and explore the services for how they work.
To get started, make sure that you have an account on AWS and that has been activated.
- Login to AWS Console
- Explore the Services Tab
- Search for EC2
- Click on EC2 service under Compute – Note: If you have already used it then it will appear in history section as well.
If you are able to perform all the above actions then you are good to go. Let’s launch a instance now.
You will redirected to the EC2 dashboard. You will see something like the below screen. In the middle section, you see two things:
- Resources – Currently we have zero instances running.
- Create Instance – To launch the new virtual instances.
Click on the “Launch Instance” button.
Step 1: Choose AMI
I will choose an existing AMI to launch the virtual machine. If you see there is a section in “My AMIs” for the images that you created.
To give you a quick start, AWS provides you with a number of Amazon Machine Images (AMI) by default. We are going to use Linux instance from AMI “Amazon Linux 2 AMI [HVM], SSD Volume Type”.
Click on the “Select” AMI and continue.
Step 2: Choose Instance Type
Now, you will see the list of instance types that we were just talking about. You can filter and find the instance type that you want based on your use case.
I’m choosing t2.micro type, since it’s free tier and my account, is eligible. So I will get by default: t2.micro (Variable ECUs, 1 vCPUs, 2.5 GHz, Intel Xeon Family, 1 GiB memory, EBS only)
Clicking on “Review and Launch” will launch the instance with the default settings. Click on the “Next: Configure Instance Details”.
Step 3: Configure Instance
You will see the below screen with several configuration options:
- Number of Instances – You can choose to launch more than one instance at a time.
Note : Please ignore the “Launch into Auto-scaling Group” for now. We will talk about this in scaling up your application.
- Purchasing Option – Check this if you want to buy Spot Instances. This lets you specify the maximum price you are willing to pay per instance hour.
- Network – AWS create a default VPC for you. You can create a VPC and select your own IP address range, create subnets, configure route tables, and configure network gateways.
- Subnet – is a range of IP addresses in your VPC that can be used to isolate different EC2 resources from each other or from the Internet.
- Capacity Reservation – reserve capacity for your EC2 instances in a specific Availability Zone
You can leave all the options with their default values for now. I just wanted to let you know that what are these options and their usage.
Now, click on “Next: Add Storage” button.
Step 4: Add Storage
Let’s add storage to your instance. One thing to notice here is that default Size (GiB) is 8 GiB. You can add the storage that you want.
Note : Free tier eligible customers can get up to 30 GB of EBS General Purpose (SSD) or Magnetic storage.
Delete on Termination – EBS volumes persist independently from the running life of an EC2 instance. After 12 months if you don’t pay then AWS will delete the storage on termination.
Now, click on the “Next: Add Tags” button.
Step 5: Add Tag
Tags are used to label an AWS resource. Tag consist of a key and pair(optional). See the below screen:
Now, click on the “Next: Configure Security Group” button.
Step 6: Configure Security Group
Security groups control the traffic to reach your instance. You allow certain ports and protocols to get access.
Note: Rules with source of 0.0.0.0/0 allow all IP addresses to access your instance. It highly recommends setting security group rules to allow access from known IP addresses only.
Now, It’s time to review the instance that we configured and launch it by clicking “Review and Launch”.
Step 7: Review Instance
Now, It’s time to see your instance before launch. A review screen will all the options and details that you have selected in the above steps.
Step 8: Create Key Pair
Now, please create a key pair. A key pair consists of two keys 1) Public Key 2) Private key. Key will allow you to connect your instance securely.
Note: Please make sure that you store the .pem file somewhere securely on your Desktop system for further use.
Finally, launch instance by clicking the “Launch Instances” button.
Launch Instance Status
AWS takes a few minutes to launch your instance. You will see the below screens while AWS is processing in the back for your instance.
Once the instantiation has been completed you will be shown another intermediate status screen. This is in case you want to see the logs of instance launch.
On this screen, scroll down and you will see a button “View Instance”.
View EC2 Instance
Now, your instance has been launched and you can see the instance details on the same Dashboard from where we started in Step -1.
See in the left toolbar there are many options available. We will talk about each of them in detail in other posts to give you end-to-end understanding.
Connect to EC2 Instance
You can directly click on the “Connect” button. OR Click on the button “Actions” on the top and you will see the below options.
On clicking “Connect” button, you will see the below options to connect to your EC2 instance.
Connecting through the SSH is straight forward, you can use console or putty to connect through the SSH client.
I will show you how to connect through a Java SSH Client from your browser. Make sure that you have Java installed on your machine. Otherwise, this option will not work for you.
Give the path of the key file “journaldev.pem” from your local machine. That’s it. Click on the “Launch SSH Client” and you are done.
Terminate EC2 Instance
You can terminate the EC2 instance at any point in time. Click on the “Actions > Instance State”, you will see the last option in the dropdown “Terminate”.