Quick Answer
Amazon EC2 (Elastic Compute Cloud) is a core web service provided by Amazon Web Services (AWS) that delivers secure, resizable compute capacity in the cloud. Designed to make web-scale cloud computing easier for developers and system administrators, EC2 allows you to spin up virtual servers—known as instances—within minutes. Rather than investing in physical hardware, provisioning an EC2 instance gives you complete control over your virtual computing environment, enabling you to run anything from lightweight web applications to heavy distributed container clusters.
At its core, Amazon EC2 acts as the foundational compute layer for modern cloud architectures. When paired with modern developer tooling like Docker, Terraform, and CI/CD pipelines, EC2 transforms from a simple virtual machine into a flexible, scalable infrastructure building block. Whether you are hosting a production database, running microservices, or building automated testing environments, understanding how to launch, configure, secure, and connect to your virtual servers is a vital skill for any cloud engineer.
Quick Answer
Amazon EC2 is a web service providing secure, resizable virtual servers (instances) in the AWS cloud. It enables developers and DevOps engineers to deploy compute infrastructure on-demand without managing physical hardware. You interact with EC2 through the AWS Management Console, CLI, or API by selecting an operating system template (AMI), choosing an instance size, configuring networking and firewall rules (security groups), attaching storage (EBS), and connecting securely via SSH.
What Is EC2?
Amazon Elastic Compute Cloud (EC2) is fundamentally a virtual machine rental service, but its deep integration with the wider AWS ecosystem makes it much more powerful than traditional on-premises virtualization. In a modern DevOps workflow, EC2 instances serve as the underlying worker nodes for container orchestrators like Amazon ECS and Kubernetes, or as standalone deployment targets managed via Infrastructure as Code (IaC) tools such as Terraform.
By decoupling compute capacity from physical infrastructure, EC2 empowers teams to scale up or down dynamically based on user traffic. If your application experiences a surge in requests, you can scale your compute tier seamlessly. When demand subsides, you can terminate or stop instances to optimize cost. Bridging traditional Linux administration with cloud-native automation requires a solid grasp of how EC2 components interact under the hood.
Key EC2 Concepts
To effectively operate Amazon EC2, you must understand several interconnected components that define how virtual servers are built, secured, stored, and accessed.
AMI
An Amazon Machine Image (AMI) is a pre-configured template that contains the software configuration—including the operating system, application server, and applications—required to launch your instance. Think of an AMI as a golden image snapshot of a server. When launching an ec2 instance, you select an AMI such as an official Ubuntu Server, Amazon Linux 2023, or Red Hat Enterprise Linux. In advanced DevOps pipelines, teams build custom AMIs using automated tools like Packer, incorporating pre-installed Docker runtimes and security hardening so that newly spawned servers are instantly ready for production traffic without manual configuration.
Instance type
AWS categorizes its virtual servers into various instance families tailored to specific workload performance profiles. The instance type determines the hardware of the host computer used for your instance, including CPU, memory, storage, and networking capacity. For general-purpose applications, families like t3 or m6i offer a balanced ratio of compute, memory, and network resources. For database-heavy workloads, memory-optimized r-series instances provide high RAM-to-vCPU ratios. Compute-optimized c-series instances suit batch processing, high-performance computing, and gaming servers, ensuring your architecture matches your exact performance and budget requirements.
Key pair
Security is paramount when managing cloud infrastructure, and EC2 relies on public-key cryptography rather than traditional username-and-password authentication for remote terminal access. A key pair consists of a public key that AWS stores on your instance and a private key file that you securely download to your local machine. When you attempt an ec2 ssh connection, your client uses the private key to cryptographically prove your identity. Keeping your private key file safe with strict file permissions is critical, as losing it or exposing it compromises the security of your virtual server.
Security group
Security groups act as virtual firewalls that control inbound and outbound traffic for your ec2 instance. Unlike network access control lists (NACLs) that operate at the subnet level, security groups operate at the instance level. By default, you should follow the principle of least privilege: deny all inbound traffic and explicitly open only necessary ports, such as port 22 for SSH or port 80/443 for web traffic. Avoid overly broad rules like opening 0.0.0.0/0 on SSH ports, which invites brute-force attacks. Proper configuration of an ec2 security group is your first line of defense against unauthorized network access.
EBS
Amazon Elastic Block Store (EBS) provides persistent block-level storage volumes for use with EC2 instances. While instance store volumes offer temporary storage tied to the physical host lifecycle, EBS volumes are highly reliable, network-attached storage devices that persist independently of the running life of your virtual server. You can provision different volume types—such as general-purpose SSD (gp3) or provisioned IOPS (io2)—and attach them to your instances. EBS also supports point-in-time snapshots, enabling automated backup routines and disaster recovery strategies for your critical application data.
Elastic IP/public IP
By default, when you launch a standard instance in a default VPC, AWS assigns it a dynamic public IPv4 address that allows communication over the internet. However, this public IP address changes if you stop and restart the instance. For production systems requiring a fixed public endpoint—such as DNS records or webhook integrations—you should allocate an Elastic IP address. An Elastic IP is a static, public IPv4 address designed for dynamic cloud computing that you can associate and disassociate from any instance in your account at will.
Launch an Instance
Image Pending
Executing an EC2 instance launch command via the AWS CLI.
Launching an instance can be accomplished through the AWS Management Console or via the AWS Command Line Interface (CLI). Using the CLI provides repeatability and integrates smoothly into automated deployment scripts.
To launch an Amazon Linux 2023 instance using the AWS CLI, execute a command similar to the following, ensuring you substitute your actual subnet ID, security group ID, and key pair name:
aws ec2 run-instances \
--image-id ami-0c55b159cbfafe1f0 \
--count 1 \
--instance-type t3.micro \
--key-name my-dev-keypair \
--security-group-ids sg-0123456789abcdef0 \
--subnet-id subnet-0123456789abcdef0 \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=WebDevServer}]'
After executing the command, you can verify that your instance is running by checking its state via the CLI:
aws ec2 describe-instances \
--filters "Name=tag:Name,Values=WebDevServer" \
--query "Reservations[*].Instances[*].[InstanceId,State.Name,PublicIpAddress]"
Expected outcomes include an instance state of running and a valid public IPv4 address assigned to the resource.
Connect With SSH
Once your instance is running and accessible via a public IP, you can establish an ec2 ssh connection to manage the operating system. Because SSH is sensitive to private key permissions, you must first secure your key file on your local machine to prevent unauthorized access errors.
Set the correct permissions on your private key file by running:
chmod 400 my-dev-keypair.pem
Next, connect to the instance using the appropriate default username for your AMI (e.g., ec2-user for Amazon Linux 2 or Amazon Linux 2023, or ubuntu for Ubuntu AMIs):
ssh -i "my-dev-keypair.pem" ec2-user@<your-instance-public-ip>
Common connection troubleshooting steps include verifying that your ec2 security group allows inbound traffic on port 22 from your current IP address, ensuring your instance has a public IP or Elastic IP assigned, and confirming that your private key matches the public key registered when the instance was launched.
Storage and Networking
Amazon EC2 is tightly integrated with Amazon Virtual Private Cloud (VPC) networking. Every instance resides within a VPC and a specific subnet, determining its private IP addressing and routing behavior. Internet-facing instances communicate via an Internet Gateway attached to the VPC, while private subnets route outbound traffic through a NAT Gateway.
On the storage front, ec2 storage scales dynamically. Beyond the root EBS volume configured during launch, you can attach multiple additional EBS volumes, resize existing volumes without detaching them, and snapshot data periodically. This flexibility ensures that your database files, application logs, and static assets have ample persistent storage space regardless of instance resizing operations.
Stop vs Reboot vs Terminate
Understanding the operational lifecycle states of an EC2 instance prevents accidental data loss and helps control cloud expenditure:
- Stop: Shuts down the instance. The virtual server is powered off, stopping compute charges, but data on attached EBS root and data volumes persists. You can restart a stopped instance at any time.
- Reboot: Performs a graceful soft restart of the operating system. The underlying host hardware may change, but your instance IP addresses and attached storage remain intact.
- Terminate: Deletes the instance permanently. Any data stored on local instance store volumes is lost immediately, and unless explicitly configured otherwise, root and non-root EBS volumes are deleted. Termination cannot be undone.
Troubleshooting
When deploying and managing virtual servers, developers frequently encounter common hurdles. If you receive a Permission denied (publickey) error during connection, verify that you are using the correct username for your AMI and that your private key file permissions are set strictly to 400. If your SSH client hangs or times out, check your ec2 security group rules to ensure port 22 is open to your specific IP address rather than blocked or misconfigured. Lastly, if your application cannot reach external APIs, check your VPC route tables and ensure your subnet has a valid path to an Internet Gateway or NAT Gateway.
✓ Best Practices
- Use least-privilege security group rules
- Store private keys securely with 400 permissions
- Leverage EBS snapshots for regular backups
- Use Elastic IPs for fixed production endpoints
✕ Common Pitfalls
- Exposing SSH port 22 to 0.0.0.0/0
- Hardcoding access keys or secrets in code
- Forgetting that stopped instances incur EBS storage costs
- Assuming termination preserves instance store data