AWS Certified Developer - Associate
AWS Fundamentals
EC2 Basics
In this article, we explore AWS EC2, a service that enables you to deploy and run applications on virtual servers in the cloud. With EC2, you can bypass the traditional challenges of managing physical hardware, including purchasing, setup, maintenance, and hardware failures. Instead, you focus on deploying your application onto a virtual server, letting AWS manage the underlying infrastructure.
Client-Server Architecture
In a client-server architecture, a server supplies resources to one or more clients. For example, a web server provides HTML files to your browser. Whether your application runs on physical or virtual servers, end users access it via web browsers or mobile applications by sending requests and receiving responses.
Traditionally, setting up a physical server required:
- Renting space in a data center
- Purchasing a physical server (e.g., Dell or HP)
- Installing and securing an operating system (e.g., Ubuntu)
- Installing required dependencies and deploying your application
- Managing hardware issues like disk failures
With AWS EC2, you rent a virtual server in the cloud, deploy your application, and let AWS handle the heavy lifting.
What is an EC2 Instance?
An EC2 instance is a virtual server that provides the computing power of a physical host. You can select an instance type that matches your application's needs in terms of CPU, memory, storage, and networking. AWS offers various instance types, including:
- General Purpose: Balanced compute, memory, and networking for diverse applications.
- Compute-Optimized: High-performance processors for compute-bound applications.
- Memory-Optimized: Ideal for workloads that require processing large datasets in memory.
- Storage-Optimized: Designed for applications that need high I/O operations.
- GPU Instances: Perfect for machine learning, deep learning, and other GPU-intensive tasks.
Amazon Machine Images (AMIs)
When you deploy an EC2 instance, you begin by selecting an AMI (Amazon Machine Image). An AMI is essentially a blueprint that includes the operating system and any pre-installed software. This eliminates the need to manually install and configure the operating system and software requirements.
Think of an AMI as a recipe that can be used to create multiple identical EC2 instances, ensuring consistent deployments. You can create public AMIs shared with the AWS community, private AMIs for your organization, or shared AMIs for specific AWS accounts.
Modifications made to an instance—such as adding users or configuring firewall settings—can be preserved by creating a new custom AMI, which then serves as an updated blueprint for future deployments.
Secure Access with SSH Keys
To ensure secure access to an EC2 instance, AWS uses SSH along with a key pair (public and private keys). At launch, you specify a key pair. The public key is embedded in the instance while you keep the private key safe, enabling secure authentication for remote logins.
EC2 Instance Lifecycle
An EC2 instance passes through several states during its lifecycle:
- Pending: The instance is launching.
- Running: The instance is operational.
- Stopping: The instance is preparing to shut down.
- Stopped: The instance is shut down but can be restarted.
- Shutting-down: The instance is about to be terminated.
- Terminated: The instance is permanently deleted and can no longer be used.
Bootstrapping with User Data
When launching an EC2 instance, you can provide user data (such as shell scripts or cloud-init directives) that executes at startup. This bootstrap script can automatically install software, configure settings, or run initial setup tasks. Note that the user data size is limited to 16 kilobytes.
Security Groups
Security groups in AWS EC2 act as virtual firewalls that control inbound and outbound traffic to your instance. For example, if you deploy a web server, you may allow inbound traffic on ports 80 (HTTP) and 443 (HTTPS).
Elastic Block Store (EBS)
While EC2 handles compute tasks, persistent data storage is managed by Amazon Elastic Block Store (EBS). EBS volumes attach to your EC2 instances, storing critical data such as operating systems, applications, and databases. Additionally, you can take incremental snapshots of these volumes, which are stored in Amazon S3, to facilitate efficient backups and cost savings.
Integration with Elastic Load Balancing and Auto Scaling
EC2 seamlessly integrates with other AWS services to enhance performance and reliability:
- Elastic Load Balancer (ELB): Automates the distribution of incoming application traffic across multiple EC2 instances.
- Auto Scaling Groups: Dynamically adjusts the number of running instances to accommodate changes in traffic, ensuring consistent performance during peaks.
Elastic IP Addresses
EC2 instances often receive public IP addresses that might change if the instance is stopped and restarted. To maintain a consistent public IP, you can use Elastic IP addresses. These are static and can be remapped between instances as needed, ensuring consistent access to your application.
Launch Templates
Launch templates allow you to define a standard configuration for your EC2 instances. These templates capture parameters like AMIs, security groups, subnets, and instance sizes. They are particularly useful when combined with auto scaling groups to ensure that new instances automatically meet your configuration requirements.
EC2 Instance Placement
AWS provides multiple placement options to influence the physical location of your EC2 instances within a data center, optimizing performance and fault tolerance:
- Cluster Placement Group: Places instances close together to reduce latency and increase throughput, ideal for high-performance computing.
- Partition Placement Group: Distributes instances across logical partitions to minimize the risk of simultaneous hardware failures, perfect for distributed workloads like Hadoop.
- Spread Placement Group: Ensures instances are distributed across distinct hardware to limit the impact of hardware failures.
EC2 Pricing Options
AWS EC2 offers a range of pricing models to suit diverse application needs:
- On-Demand: Pay for compute capacity by the hour or second with no long-term commitments.
- Spot Instances: Bid on unused capacity at discounts of up to 90%; however, these instances can be interrupted if capacity is required.
- Savings Plans: Commit to a consistent hourly spend over a one- or three-year term for lower rates.
- Reserved Instances: Commit to a specific amount of compute power for a one- to three-year term for additional savings.
- Dedicated Hosts: Rent an entire physical server for compliant, exclusive use and license reusability.
- Dedicated Instances: Receive dedicated hardware exclusively for your use, ensuring isolation from other tenants.
Using these flexible pricing options, you can fine-tune your spending to best match your application's usage patterns and budget requirements.
Summary
AWS EC2 provides a flexible, scalable, and cost-effective solution for running applications on virtual servers. By leveraging AMIs, security groups, EBS, launch templates, and a variety of placement and pricing options, you can tailor your cloud infrastructure to meet your specific needs while reducing operational overhead.
Note
With these fundamental concepts in mind, you are now equipped to deploy robust and scalable applications on AWS EC2. For further reading, visit the AWS Documentation.
Watch Video
Watch video content