Module 2 - Compute in the Cloud
Module 2 - Compute in the Cloud
Introduction to Amazon EC2
- Compute refers to the processing power needed to run applications.
- Cloud compute is available on demand — you provision and manage it over the internet.
- EC2 instances can be quickly launched, scaled, and terminated compared to on-premises servers.
- You only pay whilst the resource is running; you are not charged when an instance is stopped.
- You can customise the CPU, memory, and storage of each instance.
- You can choose between Windows and Linux operating systems.
- Instances are straightforward to scale vertically by changing their size.
- Multi-tenancy allows multiple virtual machines to share the same physical host, with isolation between them.
Instance Types
EC2 instances are available in a variety of configurations, each optimised for a different workload. You can choose the combination of CPU, memory, storage, and networking that best fits your needs.
| Type | Description | Ideal For |
|---|---|---|
| General | Balanced CPU, memory, and networking | Web servers, development |
| Compute | High CPU resources | Batch processing, analytics |
| Memory | High memory resources | Databases, in-memory caches |
| Storage | High-speed local storage | Data warehousing, big data |
| Accelerated | Hardware accelerators (GPU, FPGA) | Machine learning, graphics |
Provisioning AWS Resources
You can provision an EC2 instance using either the AWS Management Console or the AWS API.
- The Console is well suited to test and development environments where you need to interact manually.
- The API is the preferred method for production environments, as it allows for automation and repeatability.
- You can run API calls via the AWS CLI from your terminal.
- You can also interact with AWS programmatically using an AWS SDK in your chosen language.
Launching an Amazon EC2 Instance
- AMI stands for Amazon Machine Image.
- AMIs are pre-built virtual machine images that contain the base components needed to start an instance.
- You can purchase AMIs from the AWS Marketplace, or create your own.
- Each AMI bundles a preconfigured instance type and software configuration.
EC2 Pricing
AWS offers several pricing models for EC2, allowing you to choose the option that matches your usage pattern:
- On-Demand — Pay for compute time with no upfront commitment.
- Savings Plans — Commit to a consistent level of usage for one or three years and receive up to 72% discount compared to On-Demand.
- Reserved Instances — Commit to a specific instance type for one or three years and receive up to 75% discount. Best suited to predictable workloads.
- Spot Instances — Purchase unused EC2 capacity at up to 90% discount. AWS may reclaim the instance with short notice, so this model suits fault-tolerant or flexible workloads.
- Dedicated Hosts — Rent an entire physical server for your exclusive use, giving you full control over how the resources are allocated.
Scaling Amazon EC2
- Scalability is the ability of a system to handle increased load by adding or upgrading resources.
- Vertical scaling means increasing the resources on an existing instance (for example, moving to a larger instance type).
- Horizontal scaling means adding more instances to spread the workload across multiple machines.
- Elasticity is the ability to scale automatically in real time based on demand — scaling up when traffic increases and scaling down when it decreases.
- AWS uses key metrics to decide when to trigger scaling actions.
- Amazon CloudWatch monitors these metrics and can trigger scaling events automatically.
- Scaling can be configured dynamically in response to live metrics, or via predictive scaling based on scheduled patterns.
Directing Traffic with Elastic Load Balancing
- Elastic Load Balancing (ELB) distributes incoming traffic across multiple EC2 instances.
- It routes each incoming request to an instance that is ready to accept traffic.
- ELB operates at the network level, managing traffic before it reaches your backend instances.
- All requests arrive at the load balancer first; the load balancer then forwards them to the appropriate instance.
Messaging and Queuing
- Amazon SQS (Simple Queue Service) provides message queuing, allowing components to communicate asynchronously.
- Amazon SNS (Simple Notification Service) uses the publish/subscribe pattern. You publish a single message, and multiple applications that have subscribed to that topic each receive a copy.
- Amazon EventBridge routes events from multiple sources to the correct downstream services based on rules you define.