AWS Elastic Cloud Compute, also known as EC2, is one of AWS Core Services. This service allows you to provision on demand compute resources in the AWS cloud. The experience is similar to configuring a new machine in your local data center, once the machine is already physically present and ready to run. In fact, you get to choose the specs of the AWS machine and install the operation system, the application stack and everything else that you need. EC2 is an IaaS (Infrastructure as as service), and is probably the only AWS offering that falls into this cloud category. Amazon takes care of providing the virtual machine; you have to take care of everything running on that machine. How does EC2 work? What is a virtual machine ? How is it configurable? We will try to answer all these questions and much more in this article. Enjoy the ride.
EC2 Instance Types : The hardware
EC2 allows you to provision and launch virtual servers known as instances. A virtual server refers to a server that is located in the Amazon cloud. Its resources are shared by multiple users who each have control over the underlying server. This is achieved by dividing a physical server into multiple virtual/logical servers with the help of a hypervisor. With AWS, you will only interact with virtual servers. These virtual servers, also known as instances, come in different types grouped in families, as outlined below:
|General purpose||Ideal for webservers and code repository||A1, T3, T2, M5, M5a, M4, T3a|
|Compute optimized||Compute heavy applications like HPC (High performance computing, scientific modeling, gaming servers, machine learning)||C5, C5n, C4|
|Memory optimized||Memory intensive applications like OLAP cubes||R5, R5a, R4, X1e, X1, High Memory z1d|
|Accelerated computing||GPU based for floating point number calculations, graphic processing and data pattern modeling||P3, P2, G3, F1|
|Storage optimized||Basically databases||H1, I3, D2|
The instance type allows you to choose the hardware that will be used for the virtual machines you are provisioning. The t2.micro instance type comes with 1GB of memory, low-to-moderate data transfer rates to network connections, and one virtual CPU (vCPU) running on a 2.5 GHz Intel Xeon Processor. A VCPU (virtual cpu) is a unit of measurement used by AWS to describe the instance computing power. It is not very clear how it is defined and it is not to be confused with an actual CPU. For a more detailed understanding of VCPUs, here’s an interesting article I came across: https://www.credera.com/insights/whats-in-a-vcpu-state-of-amazon-ec2-in-2018/. Generally speaking the more VCPU the instance has, the more compute power it has. For more information on AWS instance type you can always refer to the official documentation: https://aws.amazon.com/ec2/instance-types/.
EC2 AMI : The software
Now that we have chosen the needed hardware on the virtual server, we need to decide what operating system and applications are needed in order to run our application. AMIs come into play. Rather than installing an operating system and a software stack from scratch, you will choose an AMI that will contain exactly what you need. You can also take a simple AMI only for the OS, manually install all other softwares that you need (JRE11, CassandraDB, tomcat for example) bundle it, save it and reuse it on another instance you want to provision. This allows you to define what environments you need for you machine, create the environments once and use them as many time as you need. It is a very powerful principle that allows you to automate a lot of grunt work. You can find AMIs in Quick Start AMIs, AWS Marketplace and Community AMIs. You can also store your custom AMIs in my AMIs.
EC2 Storage Volume
On a physical instance run on premise, every server has its own storage (hard drive) or volume store. For an EC2 instance you get to choose between 2 different storage systems:
- Instance Store Volume:
- Ephemeral (data isn’t persisted after shutdown)
- Directly connected to the server
- Cannot be encrypted
- Data access is very fast
- Amazon Elastic Block Store (EBS)
- Persisted (will survive shutdown and system crashes)
- Can be encrypted if you are manipulating sensitive data
- Can be moved around, mounted on other instances and saved into AMI
- An EC2 instance can be plugged to multiple EBS volume
- Lives far from the instance and connected via low latency network connection
In order to create a virtual server, you have to choose an instance type (the hardware), an AMI (the software) and the store volume (the storage). Once this is done your EC2 instance is ready to be used. You can access the EC2 instance via the console management, the AWS CLI or the AWS SDK. How to do so is beyond the scope of this article and will be explored thoroughly in a later article.
EC2 Elasticity, Scalability and Failover
We still have to explore two very interesting services in AWS that come in handy when working with EC2. As mentioned, EC2 stands for Elastic Cloud Compute. Until now we have not encountered any feature that would explain the Elasticity of this service. As far as we know, we are creating virtual servers to use for computing. Let us explore together why EC2 is elastic.
Before we start, let us first define what elasticity means. Elasticity is not to be confused with scalability (AWS is both elastic and scalable, but we will get to that soon ;)). Scalability is the ability of a system to grow in order to respond to more demand without impacting users. A simple example, if your app is able to serve 100 requests per second in a low demand period and 100 Million requests per second in high demand season it means that your app is scalable. It is able to serve a growing number of requests without downtime. This can be achieved in 2 ways. The first easy way is to provision as many servers as the app would need for the high demand season and a couple more just in case. In this scenario, you are also paying for the servers in the low season when these are barely used, what we also call overprovision. This will allow your app to scale in the high demand season and serve all users without interruption as it is routing requests to different instances (this is called horizontal scaling). The other way to do this is to use as few servers as possible in the low season and as many servers as possible in the high season in order to serve users without downtime. This is the definition of Elastic. Computing becomes elastic when the number of instances grow when needed and shrink when not needed. How does AWS achieve this? Well… through a dedicated service called Amazon EC2 AutoScaling. This service allows the definition of a maximum/minimum number of instances and will automatically turn off/on instances in order to match EC2 computing demands. Under the hood, this service uses either dynamic scaling and/or predictive scaling in order to achieve this elasticity.
Another interesting service that goes well with EC2 is the ELB (Elastic load balancing). This managed service on a regional level offers high availability and is automatically scalable. Through this service you can load balance calls into as many EC2 instances as you need. On top of it, you can base your routing on health check to make sure that you never route to an unhealthy EC2 instance. This allows your application to be both highly available and fault tolerant because you know what they say : Everything fails all the time, so plan for failure and nothing fails.
EC2 Pricing Models
Last but not least, let us quickly examine EC2 pricing models:
- On demand instances: the most expensive option, a true pay as you go model, where you pay for what you consume … down to the second. If you consume an instance for 12h:03:02 you will pay for exactly this amount of time.
- Reserved instances: if you know that your application will run uninterruptedly for at least a month, this is your go-to option. Practically, you will find reservations for at least one year and up to three years. This will cost you less than on demand instances. You can pay All Upfront, Partial Upfront, No Upfront.
- Spot instances: 90/100 discount compared to on demand instances. The caveat is that the instance/capacity can, on 2 mins notice, be reclaimed by AWS by shutting down your instances. These instances can be used for continuous integration, batch non critical workloads …
In this article we saw what an EC2 instance is, how to configure it correctly, choose the hardware we need (instance type), the software we need (AMI) and the volume storage we need (EBS vs instance store). We also saw what makes EC2 elastic and the pricing models that are supported for this service.
I hope that you enjoyed this article and see you next week with a new article about a different AWS core service.