Amazon Elastic Compute Cloud (Amazon EC2) is a service that provides virtual servers in the cloud.
The virtual servers you create on EC2 are called EC2 instances.
EC2 instances give you a lot of flexibility and control in the cloud.
You can create or shut down instances in minutes.
At the end of the billing cycle, you only pay for what you use.
You're in for a treat! We're learning about the world's leading compute service, how itworks and how we can use its power for our projects.
This service is used by some of the biggest companies in the world - including Netflix and Airbnb - and has saved millions of dollars for all kinds of users.
Let's unravel the magic of Amazon EC2.
What does EC2 mean?
The name EC2 sounds technical and unfamiliar - and that's okay! While it may seem intimidating, the important part is that we're here to learn about it. One step at a time.
EC2 = Elastic Compute Cloud. Here's what the three words mean:
Elastic = flexible. It can easily adapt and change in size and power to fit your needs. It's
Compute = computing power. EC2 provides virtual computers that can do various tasks, just like your personal computer.
Cloud = available over the internet.
If we put it all together, Amazon EC2 is a service that lets you to rent and use virtual computers in the cloud. They're like your personal computers, but they live on the internet. You can create, customise, and use these computers for all different reasons, from running applications to hosting websites.
Just in case you were wondering... yup, "C2" just means there are two words starting with C in the name. AWS loves to abbreviate names in this way, you've seen this in Amazon S3 - Simple Storage Service.
Why we love Amazon EC2
As we've learnt previously, compute services like Amazon EC2 take away the hassle of managing physical servers. AWS has already bought the servers, stacked them in data centres, and they are already online ready to be used.
With EC2, you get a virtual server that operates in the cloud, and it's a game-changer:
You can created and launch an Amazon EC2 instance within minutes.
You can create thousands of EC2 instances on demand.
When you're done, you can stop the instance. No more being stuck with unused servers.
You pay by the hour or second for each instance type (minimum of 60 seconds). No long-term commitments necessary!
You only pay for the compute time your instance uses. You won't pay for anything if your instance is already stopped.
You can make instances bigger or smaller whenever you need to.
You only pay for the computing power you use.
EC2 instances = virtual servers
We've described EC2 in a few different ways now. We've said:
EC2 lets us rent virtual computers
EC2 gives us virtual servers
EC2 provides virtual computers
EC2 instances
It might look a bit confusing, so let's clear it up now. All of them mean the exact same thing.
AWS owns and operates physical servers in their data centres.
Each physical server can create and manage multiple virtual computers (some people call them virtual machines).
These virtual computers are sometimes also called virtual servers, since they're just servers that run on the cloud.
In AWS, the service that lets us access a virtual server is called Amazon EC2. The virtual servers we create in EC2 are called EC2 instances.
How EC2 works
Launch - To start, you create your instance. Begin by picking a template with basic settings for your computer, like the instance type (more on that below) and security settings. We'll go through the set up process in the hands-on exercise!
Connect Next, connect to the instance. You can connect to the instance in lots of different ways, the simplest way is to log in to your AWS Management Console. You'll learn about the other ways in future topics! AWS services and apps can also connect directly with your instance to exchange data.
Use - Once you connect with your instance, you can start using it. You can run commands to install software, add storage, organise files, and more.
Amazon Machine Images (AMIs)
When you launch an EC2 instance, the first step is choosing an operating system (OS)*. AWS supports a range of operating systems including Linux, MacOS, Ubuntu, Windows, and more.
*An operating system, or OS, is a computer's manager that controls everything the computer does. Every phone and computer has an OS - for example, Windows, macOS, Android, OS, Linux and more! Just like a conductor leads an orchestra, the operating system directs the computer's hardware to work together and run smoothly. When we say that VMs can only run one operating system at a time, it means that each VM runs as if it were a standalone physical computer.
To select the operating system for your server, you must choose an Amazon Machine Image (AMI).
AMI is like a blueprint for your virtual computer. It includes details about the operating system, any software you want to have installed, and how the computer's storage and other settings should be arranged. This way, when you create a new virtual computer, it's all set up just the way you want it.
You can launch one or many identical instances from a single AMI. This means AMIs are reusable!
You can think of AMIs as cake recipes, and the EC2 instances that get created are the finished cakes!
AMIs come in several flavours:
Quick Start AMIs by AWS.
AWS Marketplace AMIs, made by other AMIs creators that are not AWS.
My AMIs based on your instances.
Community AMIs made by AWS users.
Custom images you create yourself with EC2 Image Builder.
Here's a sneak peek of AMIs:
Picking the right instance type
Choosing the right instance type is like selecting the perfect tool in a big toolbox. Instance types offer different combinations of computing power, storage, memory (i.e. how much data it can temporarily store for quick access) or other resources.
You pick the right instance type to suit your application's requirements. No more rigid, one-size-fits-all hardware constraints.
The instance types you can choose from are grouped for use cases like compute-optimised, memory-optimised, storage-optimised instances, and more.
We'll dive into the different instance types in the next topic!
Amazon EC2 lifecycle
Your EC2 instance goes through different states, from getting created (launch state) to getting deleted (terminated state). Think of your EC2 instance's lifecycle like managing a car.
Pending: The instance is launched and preparing for use. Pending is where AWS does everything it needs to do to set up an instance, like copying the AMI content into this virtual server. No charges apply at this stage.
When you decide to start a road trip, your car is in a 'Pending' state while you're loading your luggage and preparing for the journey. No costs at this point - you haven't used any fuel yet.
Running: This is the fully operational state where your instance is ready to process tasks. Charges kick in once it's running. As soon as an instance is running, you can take other actions on the instance, such as reboot, terminate, stop, and stop-hibernate.
Once you're on the road, your car is 'Running,' and that's when the fuel and maintenance costs start adding up.
Reboot, Stop, Stop-Hibernate: You can reboot an instance to restart the operating system. Stop an instance = shutting it down, with the option to restart later. Stop-Hibernate writes your instance's state into its memory, so it draws out the memory and gets up to speed really quickly when you start it again.
If something in your car feels a little clunky during the trip and you need a quick restart, you switch off your car for a second but quickly switch it back on. That's 'Rebooting' your car's engine.
'Stop' is like completely switching off your car and parking it on the side of the road overnight.
'Stop-Hibernate' is like putting your car in Park mode, but it's ready to continue as soon as you're back.
Terminate: When you terminate an instance, it's gone for good. No more charges apply, but be careful; all data on the instance is lost forever. Ideally you have important data stored in that instance backed up.
If you decide to get rid of your car permanently, it's like 'terminating' your EC2 instance – no more costs, but you lose the car. You'll also lose the luggage back inside if it's still stuck in the boot!
Now, let's discuss the cost aspect of the EC2 lifecycle.
You only get charged for an EC2 instance if you're in the running state or if you are in the stopping state when preparing to hibernate.
This means that you can stop instances when they aren't in use, such as applications that are only used from Monday-Friday.
AWS does not charge usage or data transfer fees for your instance after you stop it. But storage for any Amazon EBS volumes is still charged.
An extra for the curious
Curious about how AWS' physical servers to spin up EC2 instances? Here's what's happening behind the scenes:
EC2 runs on top of the physical servers using virtualisation technology.
When you spin up an EC2 instance (i.e. a virtual machine), you aren't necessarily taking an entire host to yourself.
Instead, you are sharing the host servers with multiple other instances.
Hypervisors are the tools that make this magic happen.
- A hypervisor is a special software that divides the server's resources, such as computing power, memory, and storage, among multiple virtual computers called EC2 instances.
- It's responsible for making sure each EC2 instance gets its fair share of resources.
- The hypervisor is also responsible for isolating the virtual machines from each other as they share resources from the host. Even though they may be sharing resources, one EC2 instance is not aware of any other EC2 instances also on that physical server. They are secure and separate from each other.
This concept of sharing a single server's resources among multiple users is called multi-tenancy. It's like having several apartments in a building, with each apartment being a separate living space for different people, even though they share the same building. The hypervisor is responsible for coordinating this multi-tenancy and it is managed by AWS.