Amazon EC2 — in brief
What is EC2?
EC2 stands for Elastic Compute Cloud. By “elastic” we mean resizable, by “cloud” we all know means it’s virtual, and the service delivered over internet and “computing” refers to computing power.
EC2 is a web service that provides secure, reliable, resizable compute capacity in the cloud.
EC2 Features
- Scalable — Has a resizable compute capacity, i.e. scale up and scale down.
- Hibernation — Enable hibernation as an additional stop behaviour, you would also need to enable the root volume encryption. But it can’t hibernated for more than 60 days.
Types of EC2 instances
Spot Instance
- Available at upto 90% discount (as compared to on-demand prices).
- Suitable for hosting the apps that are:
* stateless
* Fault-tolerant
* Flexible
** Big Data
** Containerised workloads
** CI/CD
** web servers
** HPC (High Performance Computing) - Very suitable for applications, which do not need to stay online always/continuously. Going down of those applications should not affect that application.
- If the spot instance is terminated by amazon, you’ll not be charged for the partial running hour, however if you terminate the instance yourself, you’ll be charged for any hour in which the instance ran.
On-Demand Instance
- With On-Demand Instances, you pay for compute capacity by the second with no long-term commitments. You have full control over its lifecycle — you decide when to launch, stop, hibernate, start, reboot, or terminate it.
- There is no long-term commitment required when you purchase On-Demand Instances. You pay only for the seconds that your On-Demand Instances are in the running state. The price per second for a running On-Demand Instance is fixed
Reserved Instance
- Reserved for 1 yr or 3 yrs.
- Reserved instances provide you with a significant discount (up to 72%) compared to On-Demand instance pricing.
- It provides a discounted hourly rate and an optional capacity reservation for EC2 instances.
Sub Types: With reserve instances, you can choose the type that best fits your applications needs.
- Standard: These provide the most significant discount (up to 72% off On-Demand) and are best suited for steady-state usage.
- Convertible: These provide a discount (up to 54% off On-Demand) and the capability to change the attributes of the RI as long as the exchange results in the creation of Reserved Instances of equal or greater value. Like Standard, Convertible reserved instances are best suited for steady-state usage.
- Scheduled: These are available to launch within the time windows you reserve. This option allows you to match your capacity reservation to a predictable recurring schedule that only requires a fraction of a day, a week, or a month.
Dedicated Instance
- Dedicated Instances are EC2 instances that run in a VPC on hardware that’s dedicated to a single customer. Your Dedicated instances are physically isolated at the host hardware level from instances that belong to other AWS accounts. Dedicated instances may share hardware with other instances from the same AWS account that are not Dedicated instances. Pay for Dedicated Instances On-Demand, save up to 70% by purchasing Reserved Instances, or save up to 90% by purchasing Spot Instances.
- Allowing you to use your existing server — bound software licenses.
Root Volume types (EBS)
- General Purpose SSD (gp2)
- Provisional IOPS SSD (io1)
- Cold HDD (st1)
- Throughput Optimised HDD (sc1)
- Magnetic (standard)
io1 gives the best performance w.r.t IOPS, and magnetic gives lower performance.
AZ for EC2 and its volumes will be same, as HDD/SSD should be as close to the machine as possible.
Terminating EC2 instance, you might loose the root volume as well, but you can choose while setting while configuring the EC2 not to delete on Termination. You can even choose to encrypt the volume, even the root volume.
Backend Volumes used by EC2
- Instance Store Volumes (Ephemeral storage)
- EBS
- EFS
EC2 and Auto Scaling
EC2 Auto Scaling can then scale out your group (i.e. add more instances) to deal with high demand at peak times and scale in your group (i.e. run fewer instances) to reduce costs during low utilisation.
Dynamic Scaling — You can define how to scale the capacity of your Auto Scaling Group (ASG) in response to changing demand.
Scheduled Scaling — Scheduled scaling helps you to set up your own scaling schedule according to predictable load changes. For example, let’s say that every week the traffic to your web application starts to increase on Wednesday, remains high on Thursday, and starts to decrease on Friday. You can configure a schedule for Amazon EC2 Auto Scaling to increase capacity on Wednesday and decrease capacity on Friday.
To use scheduled scaling, you create scheduled actions. Scheduled actions are performed automatically as a function of date and time.
Amazon Machine Image (AMI)
An Amazon Machine Image (AMI) provides the information required to launch an instance. Basically AMI is required to launch new instance. You must specify an AMI when you launch an instance. You can launch multiple instances from a single AMI when you need multiple instances with the same configuration. You can use different AMIs to launch instances when you need instances with different configurations.
The root volume of an instance is either an Amazon Elastic Block Store (Amazon EBS) volume or an instance store volume.
Interesting part is You can purchase AMIs from a third party, including AMIs that come with service contracts from organisations such as Red Hat. You can also create an AMI and sell it to other Amazon EC2 users.
EC2 Placement Groups
You can use placement groups to influence the placement of a group of interdependent instances to meet the needs of your workload. Depending on the type of workload, you can create a placement group using one of the following placement strategies:
- Cluster — instances close together (closely installed) inside an Availability Zone. Takes low network latency. Gives high network throughput. Happens in a single AZ.
- Spread — It has distinct underlying hardware. It’s suitable for apps with small number of critical instances, which are separate from each other. They can be in same or different AZs.
- Partitioned — spreads your instances across logical partitions such that groups of instances in one partition do not share the underlying hardware with groups of instances in different partitions, i.e. multiple instances in single instances. Each partition has its own set of racks, and no two partitions in same placement groups share same racks. This strategy is typically used by large distributed and replicated workloads, such as Hadoop, Cassandra, and Kafka.
Termination Policy
Default Termination Policy
- Before Amazon EC2 Auto scaling selects an instance to terminate, it first determines which AZ has the most instances, and at least one instance that is not protected from scale in.
- Zeroing that particular instance, using Default Termination Policy.
Oldest Launch Template or Configuration
- Determine whether any of the instances use the oldest launch template or configuration.
- Amazon EC2 Auto Scaling terminates instances that use a launch configuration before instances that uses a launch template. Launch template stay, if launch configuration exists.
Random Termination
- If there are multiple instances to terminate, determine which instances are closest to the next billing hour.
- If after the above scenario, still you have to choose, in that case, terminate one of the instances at random.
Concerns w.r.t EC2
- AZ for EC2 and its volumes have to be the same, as volume has to be as close as possible.
- Terminating EC2 instance, you might lose the root volume as well. You can chose “Not to delete on Termination”, during the set-up while configuring.
- You can choose to encrypt the volume while setting up.
Some Tips
- Install httpd on your EC2 to make it a web server.
- While setting up your EC2 instance (in step 4), you can choose to delete on termination for your root volume (but be cautious while doing so, as you’ll lose all your data)
- Enabling the accidental termination prevention, you cannot terminate the running the running instance.
* In case you want to terminate the running instance, first disable the running instance or stop it, and then termination option will be enabled. - You can have any number of EC2 instances within a security group. You can have multiple security groups attached to EC2 instances (max 5).
- Single EC2 instances won’t scale and it’ll be a single point of failure in the architecture.
- To build a resilient system have EC2 instances in an auto scaling group, across 2 different AZs.
- Available Hypervisors for EC2 are Nitro and Xen.
References
- https://aws.amazon.com/ec2
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-on-demand-instances.html
- https://aws.amazon.com/ec2/pricing/reserved-instances/
- https://aws.amazon.com/ec2/pricing/dedicated-instances/
- https://docs.aws.amazon.com/autoscaling/ec2/userguide/schedule_time.html