AWS Learning Journey

Stop SSHing Into Servers: Amazon EC2 Done Right

Instance types, AMIs, user data, EBS, pricing models, and Session Manager — how to run virtual machines on AWS like a professional, not a hobbyist.

9 min read · 1656 words
AWSAmazon EC2Cloud ComputingDevOpsInfrastructure
Stop SSHing Into Servers: Amazon EC2 Done Right cover image

AWS from Zero to Production — Phase 4, Part 1 of 6 · See the full series roadmap


Here’s how a lot of people learn EC2:

Launch an instance. Download a .pem key. Open port 22 to the world. SSH in. Install things by hand. Forget what you installed. Lose the key. Panic.

Six months later, that server is a pet: unique, fragile, impossible to rebuild, and nobody dares to touch it.

Professionals do it differently. Servers are built from images, configured by code, accessed without SSH keys, and replaced instead of repaired. This article shows you how — and covers every EC2 building block along the way.

What you’ll walk away with


What is EC2?

Amazon Elastic Compute Cloud (EC2) gives you virtual machines, called instances, in minutes. You choose the CPU, memory, storage, operating system, and network — and pay only while they run.

EC2 is the foundation under many other services. Even when you use containers or managed databases, there’s often EC2 capacity underneath.


Instance types: reading the name

An instance type defines the hardware. The name tells you what you’re getting:

m7g.large
| ||  |
| ||  +-- size: nano, micro, small, medium, large, xlarge, 2xlarge...
| |+----- attributes: g = AWS Graviton (Arm), i = Intel, a = AMD,
| |                   d = local NVMe storage, n = enhanced networking
| +------ generation: higher is newer (and usually better value)
+-------- family: m = general purpose

Instance families

Engineer’s note: Graviton instances (the g suffix) use AWS-designed Arm processors and often give better price-performance. Most Linux workloads, languages, and containers run on Arm without code changes — but build your container images for arm64.

Picking a size

Start small, measure, then rightsize. CloudWatch metrics and AWS Compute Optimizer (Phase 10) tell you whether you’re over- or under-provisioned. Changing the type is a stop, modify, and start.


Amazon Machine Images (AMIs)

An AMI is the template an instance boots from: operating system, software, and configuration.

AMI IDs differ by Region. Instead of hard-coding them, use the public Systems Manager parameters that always point to the latest version:

aws ssm get-parameter \
  --name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
  --query Parameter.Value --output text

EC2 Image Builder

EC2 Image Builder automates building, testing, and distributing golden images: a base AMI plus your hardening, agents, and patches. Run it on a schedule, and every new instance starts from a fresh, patched image.


User data: configure at first boot

User data is a script that runs when an instance first starts. It’s the simplest way to make instances configure themselves:

#!/bin/bash
dnf update -y
dnf install -y nginx
systemctl enable --now nginx

Watch out: Never put secrets in user data. Anyone who can describe the instance attributes can read it. Fetch secrets at runtime from Secrets Manager instead.


Storage: EBS vs. instance store

Amazon Elastic Block Store (EBS)

EBS volumes are network-attached disks that persist independently of the instance.

Phase 5 goes deep on EBS volume types and snapshots.

Instance store

Some instance types include instance store: physical disks on the host.


Networking: security groups and Elastic IPs

Engineer’s note: If you think you need an Elastic IP for a web server, you probably need a load balancer instead. Part 2 explains why.


The instance metadata service (IMDS)

Every instance can query http://169.254.169.254 for information about itself — its ID, AZ, and, importantly, the temporary credentials of its IAM role.

IMDSv2 requires a session token, which protects against attacks that trick an app into fetching that URL, such as server-side request forgery. Always require it:

aws ec2 run-instances ... --metadata-options HttpTokens=required

And give instances permissions through an IAM role (an instance profile) — never by copying access keys onto the server.


Key pairs — and why you may not need them

A key pair lets you SSH into Linux instances: AWS stores the public key, and you keep the private key.

The problems:

AWS Systems Manager Session Manager

Session Manager gives you a shell on an instance through the AWS console or CLI:

Requirements:

  1. The SSM Agent, preinstalled on Amazon Linux and many other AWS AMIs.
  2. An instance role with the AmazonSSMManagedInstanceCore managed policy.
  3. A network path to Systems Manager: a NAT gateway, or interface endpoints for ssm, ssmmessages, and ec2messages.
aws ssm start-session --target i-0123456789abcdef0

The CLI command needs the Session Manager plugin installed locally.

Recommendation: Launch instances without a key pair and without port 22 open. Use Session Manager when you truly need a shell — and treat needing one as a sign that something should be automated.


Pricing models

You can run the same instance under very different prices:

Which to use?

Engineer’s note: A common pattern is a Savings Plan for the baseline, On-Demand for peaks, and Spot for anything that can retry. Phase 10 covers cost optimization in depth.


Placement groups

Placement groups influence how instances are placed on hardware:

Most web applications never need one.


Common mistakes


Key takeaways


Quick self-check

Test yourself before moving on:

  1. What does the g mean in c7g.xlarge?
  2. Where would you look if a user data script failed?
  3. What three things does Session Manager need?
  4. Which pricing model fits a nightly batch job that can safely retry?

Answers

  1. AWS Graviton — an Arm-based processor.
  2. /var/log/cloud-init-output.log on the instance, or the system log in the console.
  3. The SSM Agent, an instance role with AmazonSSMManagedInstanceCore, and a network path to Systems Manager.
  4. Spot Instances.

What’s next

In Part 2, we put instances behind load balancers and let Auto Scaling add and remove them automatically: ALB, NLB, target groups, health checks, launch templates, and scaling policies.

Do you still SSH into servers at work? Be honest in the comments.


Share this article

LinkedIn post draft