Lesson 5 of 9 · 23m
EC2 Without Pet Servers
Launch templates, Auto Scaling groups, user data, patching, and access patterns that keep instances replaceable.
EC2 is simple right up until an instance becomes somebody's pet. Then configuration drifts, the disk fills with untracked logs, nobody knows which package version matters, and replacing the host feels like surgery on a awake patient. The production pattern is the opposite: instances are replaceable machines built from known images and configuration.
The launch path is the real server definition
aws ec2 create-launch-template --launch-template-name web-v3 --version-description 'AL2023, nginx, IMDSv2' --launch-template-data '{
"ImageId": "ami-0123456789abcdef0",
"InstanceType": "t3.small",
"IamInstanceProfile": {"Name": "web-instance-profile"},
"MetadataOptions": {"HttpTokens": "required"},
"UserData": "IyEvYmluL2Jhc2gK..."
}'User data is base64-encoded in API calls. It should bootstrap, not define the entire system by hand: install the agent, join configuration management, pull the artifact, start the service. If rebuilding the host requires a wiki page from 2019, that is the actual problem.
Auto Scaling is an availability tool first
An Auto Scaling group maintains desired capacity, replaces unhealthy instances, and spreads them across subnets and Availability Zones. Scaling based on CPU or request count is useful, but do not forget the boring part: automatic replacement when one host dies.
min: 2
desired: 2
max: 6
health check: ELB + EC2
termination policy: oldest launch template firstMinimum two across two AZs is not waste; it is the cost of surviving a single instance or zone failure without a 2 a.m. hero moment.
SSH should not be the operating model
Use AWS Systems Manager Session Manager for shell access where possible. It avoids open inbound port 22, writes audit logs, and uses IAM rather than shared SSH keys.
aws ssm start-session --target i-0123456789abcdef0If you must use SSH, restrict source addresses and manage keys deliberately. A bastion can be correct, but a bastion with port 22 open to the world is just an attack surface with a friendly name.
Field notes
- Never store application state only on the root volume. Use EBS volumes with snapshots, EFS, S3, or a database depending on durability needs.
- Patch by replacing instances with new images when you can; mutating long-lived hosts in place recreates configuration drift.
- Instance metadata exposes temporary credentials, so every local process is a potential credential reader. Require IMDSv2 and keep the instance role narrow.
- Spot instances are excellent for interruptible workers and terrible as the only capacity for stateful or latency-sensitive services.
Was this lesson helpful?