Anmazon EC2
Amazon EC2 (Elastic Compute Cloud) is a web service that provides resizable and secure virtual servers in the cloud. It allows you to run various types of workloads, including applications, databases, websites, and more. Here's what you need to know about EC2:
1. Instance Types
- General Purpose: Balanced compute, memory, and networking resources. Ideal for web servers, development environments, and small-to-medium databases. Examples: t4g, t3, m5, m6g.
- Compute Optimized: High performance for compute-intensive applications like batch processing, scientific modeling, and high-performance web servers. Examples: c5, c6g.
- Memory Optimized: Designed for memory-intensive tasks, such as in-memory databases, real-time big data analytics, and high-performance computing. Examples: r5, r6g, x1.
- Storage Optimized: Optimized for high, sequential read and write access to large data sets on local storage. Ideal for databases, data warehousing, and file processing. Examples: i3, d2.
- Accelerated Computing: Feature GPUs or FPGAs for graphics-intensive applications, machine learning, or scientific simulations. Examples: p4, g4dn, f1.
2. Instance Purchasing Options
- On-Demand Instances: Pay by the second with no long-term commitments. Ideal for short-term, unpredictable workloads.
- Reserved Instances: Commit to a one- or three-year term for a significant discount compared to On-Demand pricing. Useful for predictable workloads.
- Spot Instances: Take advantage of unused EC2 capacity at a discounted rate. Suitable for fault-tolerant and flexible applications, such as big data processing, containerized workloads, and batch processing.
- Savings Plans: Flexible pricing model that provides discounts in exchange for a commitment to a consistent amount of usage (measured in $/hour) for a one- or three-year term.
- Dedicated Hosts and Instances: Run instances on physical servers dedicated for your use. Ideal for compliance requirements or specific licensing terms that require dedicated hardware.
3. Instance Lifecycle
- Launch: When an EC2 instance is created and started.
- Running: The instance is up and operational. You are billed while in this state.
- Stop and Start: Stopping an instance halts its compute capacity, but the associated EBS volume (if present) is retained. Starting the instance again resumes the compute capacity.
- Reboot: Restarts the instance, similar to a system reboot. The instance ID remains the same.
- Terminate: Permanently deletes the instance and its associated storage (unless the root volume's delete-on-termination attribute is set to
false
).
4. Amazon Machine Images (AMIs)
- An AMI provides the information required to launch an EC2 instance, including:
- The operating system (Linux, Windows, etc.).
- Application software.
- Pre-installed applications or software packages.
- Public AMIs: Provided by AWS, third-party vendors, or the community, including common operating systems and software stacks.
- Custom AMIs: Create custom AMIs with specific configurations or software for consistency across multiple instances.
- Marketplace AMIs: Offered by third-party vendors with pre-configured software, available through the AWS Marketplace.
5. Storage Options
- Instance Store: Ephemeral storage physically attached to the host where your EC2 instance runs. Data is lost when the instance is stopped, terminated, or fails.
- EBS (Elastic Block Store): Persistent block storage that remains even after the instance stops or terminates. EBS volumes can be:
- General Purpose SSD (gp3, gp2): Balanced performance for most workloads.
- Provisioned IOPS SSD (io1, io2): High-performance storage for critical databases.
- Throughput Optimized HDD (st1): Low-cost storage for frequently accessed, throughput-intensive workloads.
- Cold HDD (sc1): Lowest cost for infrequently accessed data.
- EFS (Elastic File System): A fully managed NFS file system that can be mounted to multiple instances for shared storage.
- S3 (Simple Storage Service): Object storage for storing and retrieving any amount of data, commonly used for storing backups, application data, and media.
6. Networking
- VPC (Virtual Private Cloud): EC2 instances run within a VPC, which provides network isolation. Within a VPC, instances can be placed in subnets (public or private).
- Elastic IP Addresses: Static, public IP addresses that can be associated with an EC2 instance for consistent IP addressing, even if the instance is stopped and started.
- Security Groups: Virtual firewalls attached to instances, controlling inbound and outbound traffic. Rules specify which IP ranges, protocols, and ports are allowed.
- Elastic Load Balancer (ELB): Distributes incoming traffic across multiple EC2 instances for high availability and fault tolerance.
- Auto Scaling: Automatically scales your fleet of EC2 instances based on demand. Scaling policies can be set based on metrics such as CPU utilization, request rate, etc.
7. Instance Metadata and User Data
- Instance Metadata: Information about your instance that you can query from within the instance itself (e.g., instance ID, public IP, security groups). Available through the metadata endpoint (
http://169.254.169.254/latest/meta-data/
). - User Data: Scripts or instructions provided when an instance is launched. User data is executed when the instance boots for the first time, often used for installing software or configuring services.
8. Security and IAM
- IAM Roles: Assign IAM roles to instances to grant them temporary, secure access to AWS services (e.g., S3, DynamoDB) without embedding credentials in applications.
- Key Pairs: Use SSH key pairs to securely access Linux/Unix instances or Windows passwords for Windows instances. The private key is required for SSH access, so it must be kept secure.
- Security Best Practices:
- Restrict SSH access using security groups.
- Use IAM roles instead of hard-coding AWS credentials in applications.
- Regularly update instances with security patches.
9. Monitoring and Management
- CloudWatch: Monitor EC2 instance metrics (e.g., CPU, disk, network) in near real-time. Set up alarms to trigger actions or notifications.
- CloudTrail: Logs API calls made to EC2, providing visibility into instance launch, stop, terminate, and other activities for auditing and security analysis.
- Elastic IP Addresses: Static IP addresses that can be quickly reassigned to another instance in case of failure, improving high availability and resilience.
10. Pricing Models and Cost Management
- Pay-as-You-Go: On-Demand instances are billed per second or hour, depending on the instance type.
- Cost Management: Use features like AWS Cost Explorer, budgets, and reports to monitor and control EC2 costs. Rightsize instances and leverage Savings Plans and Reserved Instances for predictable workloads to reduce costs.
11. Fault Tolerance and High Availability
- Multi-AZ Deployments: Distribute instances across multiple Availability Zones (AZs) to improve fault tolerance and resilience to failures.
- Elastic Load Balancing (ELB): Automatically distributes incoming application traffic across multiple instances for high availability.
- Auto Scaling: Dynamically adjusts the number of EC2 instances in response to demand, ensuring application availability and cost efficiency.
12. Instance Lifecycle Management
- Auto Scaling Groups: Automatically manage instance groups, scaling them up or down based on metrics such as CPU utilization or network traffic.
- EC2 Instance Schedules: Use AWS Instance Scheduler or AWS Lambda to start and stop instances based on defined schedules, reducing costs by turning off resources when not in use.
- Termination Protection: Enable termination protection to prevent accidental termination of critical instances.
13. Advanced Features
- Elastic GPU: Attach Elastic GPUs to EC2 instances to accelerate graphics performance for applications needing GPU power without using dedicated GPU instances.
- Enhanced Networking: Leverage Elastic Network Adapter (ENA) or Elastic Fabric Adapter (EFA) for high throughput and low latency network performance, suitable for high-performance computing applications.
- Spot Fleet: Request a combination of Spot Instances and On-Demand Instances to optimize costs and availability.
- Placement Groups:
- Cluster: Groups instances in a single AZ for low-latency, high-bandwidth network throughput.
- Spread: Distributes instances across different hardware to reduce simultaneous failure risk.
- Partition: Divides instances into logical partitions, isolating them across different racks.
14. Data Transfer and Connectivity
- Data Transfer Costs: Understand data transfer costs, especially for data transferred out of AWS or between regions, to optimize network usage and manage expenses.
- Direct Connect: Establish a private, dedicated network connection from your on-premises environment to AWS to enhance performance and security.