AWS Builder Center

Amazon EC2 Explained: Instances, Pricing, and Security Groups

Amazon EC2 (Elastic Compute Cloud) gives you virtual servers in minutes. You pick the size, the operating system, and the network settings, and you pay for what you use. It’s one of the oldest AWS services and the foundation for many others.

The building blocks

  • Instance: the virtual server itself.
  • AMI (Amazon Machine Image): a template with the operating system and software your instance starts from.
  • Instance type: the hardware size, such as CPU, memory, and network capacity.
  • Key pair: used to log in securely over SSH, instead of a password.
  • Security group: a virtual firewall around your instance.

Choosing an instance type

Instance types are grouped into families by purpose:
  • General purpose: a balanced mix, good for most workloads.
  • Compute optimized: high CPU, good for batch processing and web servers under load.
  • Memory optimized: lots of RAM, good for in-memory databases.
  • Storage optimized: fast local disk access.
  • Accelerated computing: GPUs and similar hardware for ML and graphics.
Start small, measure, and resize. Changing the type later is easy.

Pricing options

Option
Best for
On-Demand
Short or unpredictable workloads, no commitment
Savings Plans / Reserved
Steady usage, lower price in exchange for a commitment
Spot
Flexible, fault-tolerant jobs at a deep discount
Spot capacity can be taken back by AWS with short notice (about two minutes), so only use it for work that can be interrupted and retried.

Storage: EBS vs. instance store

  • EBS volumes are network-attached disks. Data persists even when the instance stops, and you can snapshot it.
  • Instance store is physically attached storage. It’s fast, but the data is lost when the instance stops or terminates.
Use EBS for anything you can’t afford to lose.

Security groups

A security group controls traffic in and out of your instance:
  • Rules only allow traffic. There are no deny rules.
  • Security groups are stateful. If a request is allowed in, the response is allowed out automatically.
  • Everything is blocked by default until you add a rule.
Don’t open SSH (port 22) to  0.0.0.0/0 . Restrict it to your own IP, or skip SSH entirely and use AWS Systems Manager Session Manager.

Give your instance a role, not keys

If your app needs to call other AWS services, attach an IAM role to the instance. It provides temporary credentials automatically, so you never store access keys on the server.

Stop vs. terminate

  • Stop: the instance shuts down, and you stop paying for compute, but you still pay for attached EBS storage.
  • Terminate: the instance is deleted for good.
A stopped and restarted instance usually gets a new public IP. If you need a fixed one, use an Elastic IP.

Common mistakes

  • Leaving unused instances and volumes running and paying for them.
  • Opening SSH to the whole internet.
  • Storing important data on instance store.
  • Putting access keys on the instance instead of using a role.

Conclusion

EC2 is flexible, but the defaults won’t optimize cost or security for you. Pick a sensible instance type, use EBS for durable data, lock down your security group, and attach a role. Terminate your test instances when you’re done.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article