Skip to Content

Highly available EC2 instance

Three ways to keep a single EC2 instance and its Elastic IP available - a standby instance with a CloudWatch alarm and Lambda, an Auto Scaling group of one across AZs, and the same with EBS data carried over by snapshots and lifecycle hooks.

Some apps can only run as one instance (a license server, a legacy app) and are reached at one public IP. How does it stay available?

A standby instance and Lambda

A highly available EC2 instance with a standby: users reach the active instance through an Elastic IP; a CloudWatch alarm or EventBridge rule watches its health and triggers a Lambda function, which starts the standby instance and moves the Elastic IP to it A highly available EC2 instance with a standby: users reach the active instance through an Elastic IP; a CloudWatch alarm or EventBridge rule watches its health and triggers a Lambda function, which starts the standby instance and moves the Elastic IP to it

Watch the active instance, move the address on failure.
  • Users reach the public instance through an Elastic IP.
  • A CloudWatch alarm (on a metric or status check) or an EventBridge rule watches it.
  • On failure it invokes Lambda, which starts the standby instance and attaches the Elastic IP to it.

An Auto Scaling group of one

  • An ASG with min 1, max 1, desired 1, across at least 2 AZs.
  • If the instance fails, the ASG launches a replacement, possibly in the other AZ.
  • The instance's user data attaches the Elastic IP, found by tag, when it boots.
  • The instance needs an IAM role that allows the API calls to attach the Elastic IP.

Keeping the EBS data

A highly available EC2 instance with an Auto Scaling group of minimum, maximum and desired 1 across two AZs: the instance's user data attaches the Elastic IP; when the instance in AZ A is terminated, a lifecycle hook snapshots its EBS volume with tags; the replacement in AZ B gets a new volume from that snapshot through the launch lifecycle hook and takes the Elastic IP A highly available EC2 instance with an Auto Scaling group of minimum, maximum and desired 1 across two AZs: the instance's user data attaches the Elastic IP; when the instance in AZ A is terminated, a lifecycle hook snapshots its EBS volume with tags; the replacement in AZ B gets a new volume from that snapshot through the launch lifecycle hook and takes the Elastic IP

Carry the disk across AZs with a snapshot.

An EBS volume is tied to its AZ, so the replacement in another AZ can't just reattach it:

  • An ASG terminate lifecycle hook takes an EBS snapshot (with tags) before the instance goes away.
  • An ASG launch lifecycle hook creates a new volume from that snapshot in the new AZ and attaches it to the replacement.

Not in the course slides

For hardware failures within one AZ, EC2 automatic instance recovery (simplified automatic recovery is on by default for supported instances, or a CloudWatch alarm action) moves the instance to new hardware and keeps its instance ID, private IPs, Elastic IP and EBS volumes. It doesn't help if the AZ fails. See EC2 instance recovery.

Real-world examples

  • A license server that clients reach at a fixed IP runs in an ASG of one across two AZs; its user data reattaches the Elastic IP tagged license-server, and its config lives on EFS so no snapshot dance is needed.

Exam tips

  • "Single instance, must survive an AZ failure, fixed public IP" → ASG min/max/desired 1 across 2+ AZs + user data that attaches the Elastic IP (with an instance role).
  • "Automatically recover an instance after a hardware failure, same IP" → EC2 automatic recovery (CloudWatch StatusCheckFailed_System).
  • "Run custom actions when an ASG launches or terminates an instance" → lifecycle hooks.

Common traps

  • "Attach the same EBS volume in the other AZ." EBS volumes are AZ-bound; use a snapshot (or keep the data on EFS).
  • "An ASG of one gives zero downtime." A replacement takes minutes to launch; it's high availability, not fault tolerance.

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt