EC2 Spot Instance Interruption Operator
What this skill does
Executes EC2 Spot Instance interruption-handling operations correctly and
safely. Runs deterministic pre-checks before any state-changing CLI
(EventBridge rule is ENABLED, SQS queue is healthy with acceptable depth,
Lambda graceful-shutdown function is Active with sufficient concurrency,
ALB/NLB target group deregistration delay is configured, ASG lifecycle
hook timeout accommodates the 2-minute window, Spot Fleet or ASG has a
valid replacement strategy with sufficient diversification), executes
the graceful-shutdown pipeline behind a CONFIRM gate, and verifies the
result by confirming the interrupted instance was drained from the load
balancer, state was checkpointed, replacement launched, and the
application's error rate stayed within the tolerance band. Every
interruption-warning handler produces the drain-and-checkpoint sequence;
every fleet tuning surfaces the capacity-optimized recommendation and
the 3-family diversification minimum.
Quick navigation
| Section |
What it covers |
When to read it |
| § Quick reference |
Verdict thresholds (REVIEW_REQUIRED/OPERATION_COMPLETED) + pre-check priority |
Before any operation |
| § Mindset |
Why the 2-minute warning is the safety window, capacity-optimized as the default, diversification math |
Understanding the safety model |
| § Pre-flight |
Spot/Fleet/ASG metadata gate — state, capacity, allocation strategy, diversification |
Before executing any CLI |
| § Process |
Per-operation planning: handle-interruption, configure-pipeline, tune-replacement, audit-diversification, validate-placement, configure-checkpointing, audit-health |
When choosing which operation to run |
| § Output format |
Structured OPERATION/VERDICT/PRE_CHECKS/STEPS/POST_VERIFY template |
Formatting the response |
| § Anti-Patterns |
NEVER list — common mistakes that lose in-flight work or strand the fleet below capacity |
Review before risky operations |
| § Pre-flight safety |
Capture pre-state, verify pipeline health, validate checkpoint target |
Defense-in-depth |
Quick reference — verdict thresholds
| Verdict |
Trigger condition |
Action |
REVIEW_REQUIRED |
Operation plan is ready but requires human review before execution (replacing lowest-price with capacity-optimized changes billing behavior, reducing a Spot Fleet's target capacity below current running count, changing the interruption behavior from terminate to hibernate or stop, modifying an ASG lifecycle hook that affects in-flight deployments, enabling capacity rebalance on a production ASG) |
Emit plan with the specific items to review, wait for operator approval |
OPERATION_COMPLETED |
Interruption handling finished and post-verification passed (instance drained from ELB, state checkpointed to S3 or DynamoDB, replacement launched, ASG/Fleet capacity restored, application error rate within tolerance) |
Emit verification results, monitoring plan |
Priority order for pre-checks (apply in this sequence, any failure routes
to REVIEW_REQUIRED with the failure surfaced):
- Spot request reachability — the Spot Instance Request exists and
State: active (not cancelled, closed, failed). The instance
itself is running.
- EventBridge rule health — the
EC2 Spot Instance Interruption Warning rule is ENABLED, targets
the SQS queue or Lambda function, and the rule's event pattern
matches "detail-type": ["EC2 Spot Instance Interruption Warning"].
- SQS queue health — the interruption queue is healthy:
ApproximateNumberOfMessagesVisible is not climbing (no consumer
lag), ApproximateAgeOfOldestMessage is under the 2-minute window
threshold, the dead-letter queue is empty.
- Lambda graceful-shutdown function —
State: Active,
ReservedConcurrentExecutions >= 1 (not the stealth-throttle
condition), timeout >= 60 seconds (enough for drain + checkpoint
within the 2-minute window), runtime is a supported version.
- ALB/NLB target group — the instance is a registered target,
deregistration_delay.timeout_seconds is set to a value that allows
in-flight requests to complete (typically 30-60 seconds).
- ASG lifecycle hook — if the instance is in an ASG, the
InstanceLaunching and InstanceTerminating lifecycle hooks have
timeouts that accommodate the 2-minute window (default 60 minutes
for launching, but the termination hook should be at least 120
seconds to give the graceful-shutdown pipeline room).
- Replacement strategy — the Spot Fleet or ASG has a valid
allocation strategy (
capacity-optimized recommended,
capacity-optimized-prioritized, diversified,
lowest-price with multiple pools).
- Diversification — the Spot Fleet or ASG specifies at least 3
instance families across at least 3 AZs (the minimum for 99.9%
availability per the AWS Spot best-practices guidance).
- Capacity rebalance —
AllocationStrategy on the ASG or Spot
Fleet supports capacity rebalance (or is explicitly enabled via
CapacityRebalance on the ASG).
Cost/time baselines (2026):
- EventBridge Instance Interruption Warning: delivered 2 minutes (120
seconds) before the actual interruption. This is the maximum
graceful-shutdown window. Some interruption types (capacity-reclaim)
may provide less notice in practice — design for 90 seconds to be
safe.
- ELB target deregistration: enters
draining state, waits
deregistration_delay.timeout_seconds (default 300 seconds for ALB,
configurable 0-3600). Within the 2-minute window, set this to 30-60
seconds to leave room for checkpointing.
- Lambda graceful-shutdown execution: typically 10-45 seconds (drain
from ELB, write checkpoint to S3, signal the application to flush).
- Spot Fleet auto-replacement: the Fleet launches a replacement within
seconds of the interruption. Provisioning time depends on the AMI
and instance type (30 seconds to 3 minutes).
- Spot placement score: a single API call
(
get-spot-placement-scores) returns in under 5 seconds for up to
50,000 capacity combinations.
Mindset
One-line takeaway: the 2-minute Instance Interruption Warning is
the safety window — it is enough time to drain from the load balancer
and checkpoint state, but only if the pipeline is pre-wired and tested.
Spot is NOT unreliable; it is a different reliability model that
rewards diversification and graceful shutdown. Driven by three EC2
Spot realities:
The 2-minute warning allows graceful shutdown (drain + checkpoint).
When EC2 reclaims a Spot Instance, EventBridge emits an
EC2 Spot Instance Interruption Warning event 2 minutes before the
actual interruption. A Lambda function (or any event consumer) can
use this window to deregister the instance from the load balancer,
flush in-flight transactions, write a checkpoint to S3 or DynamoDB,
and signal the application to shut down cleanly. Without this
pipeline, the instance is terminated with no warning and all in-
flight work is lost.
Capacity-optimized allocation strategy minimizes interruptions.
The capacity-optimized strategy (and its
capacity-optimized-prioritized variant) launches Spot Instances
from the pools with the lowest interruption rates, rather than the
lowest price. This reduces the frequency of interruptions at a
marginally higher cost. For production workloads, capacity-optimized
is the default recommendation — the cost saving from lowest-price
is eaten by the disruption of frequent interruptions.
Diversify across at least 3 instance families for 99.9%
availability. Spot interruption rates vary by instance family, AZ,
and time of day. A fleet that runs on a single instance type in a
single AZ is at high risk of a simultaneous reclaim. Diversifying
across at least 3 instance families (e.g., m5, m6a, c6g) and
at least 3 AZs gives a 99.9% availability profile for Spot — the
mathematical basis is that the probability of all pools being
interrupted simultaneously drops exponentially with the number of
independent pools.
Pre-flight: Spot/Fleet/ASG metadata gate
Run before classification. Misclassifying these produces wrong plans.
Pagination: describe-spot-instance-requests paginates at 1,000/page
— drain --next-token to completion. Filter by --state active.
Live-account pre-flight (skip if offline plan audit): the 7 read-only commands
(describe-spot-instance-requests ... autoscaling describe-auto-scaling-groups) are listed in Diagnostic commands.
Malformed-input protocol (VERDICT: ERROR + REMEDIATION): see Error handling.
| Spot/Fleet/ASG attribute |
Effect on operation |
State: cancelled or closed |
Spot request no longer active. Instance may still be running but is not managed by the Spot service. |
InstanceInterruptionBehavior: terminate |
Default. Instance is terminated on interruption. No state preserved. |
InstanceInterruptionBehavior: stop |
Instance is stopped (EBS-backed only). Can be restarted when capacity returns. |
InstanceInterruptionBehavior: hibernate |
Instance hibernates (memory state saved to EBS). Requires the instance to be hibernation-enabled at launch. |
EventBridge rule State: DISABLED |
Interruption warnings are NOT delivered. The graceful-shutdown pipeline is dark. |
SQS ApproximateAgeOfOldestMessage > 120 |
Messages are older than the 2-minute window — the consumer (Lambda) is lagging. Interruption warnings in the queue are already stale. |
Lambda ReservedConcurrentExecutions: 0 |
Stealth throttle. The graceful-shutdown Lambda will never execute. Every interruption is a hard termination. |
Lambda Timeout < 60 |
May not complete drain + checkpoint within the 2-minute window. |
Spot Fleet AllocationStrategy: lowestPrice with 1 pool |
High interruption risk. Recommend switching to capacity-optimized with >= 3 pools. |
Spot Fleet FulfilledCapacity < TargetCapacity |
Fleet is below target — instances are being interrupted faster than replacements launch. |
ASG CapacityRebalance: false |
Capacity rebalance is off. The ASG does not proactively replace at-risk instances. |
ASG MixedInstancesPolicy with 1 instance type |
No diversification. High risk of simultaneous reclaim. |
Process — operation planning (apply in order)
Step 0: Expert knowledge - non-obvious EC2 Spot interruption behaviors (condensed)
- The 2-minute warning is a maximum, not a guarantee - design the pipeline to finish within 90 seconds. EventBridge is the ONLY notification channel.
InstanceInterruptionBehavior is set at launch and immutable (terminate / stop / hibernate).
- Capacity rebalance is proactive (launches replacements BEFORE the warning). Prefer
capacity-optimized over lowest-price for production.
- Diversification math: 3 families x 3 AZs = 9 pools for 99.9% availability. Spot placement score is a forecast, not a guarantee.
- Spot Block is deprecated (2024+). ELB deregistration delay must fit within 2 minutes; lifecycle hooks fire on termination, not the warning.
- Full deep dives (Graviton rates, StatusCode early warning, Fleet auto-replacement): Advanced patterns.
Step 1: Pre-check gate — REVIEW_REQUIRED if any check needs human attention
Run ALL pre-checks for the chosen operation. If any check fails or
requires human review, the verdict is REVIEW_REQUIRED with the specific
items listed. Do NOT execute until the operator reviews.
For ALL operations:
- Spot Instance Request exists and
State: active.
- Instance is
running.
- EventBridge interruption-warning rule is
ENABLED.
- SQS queue (or Lambda target) is healthy — no consumer lag.
For handle-interruption (live EventBridge warning event):
5. Event detail.instance-action determines shutdown sequence
(terminate, stop, or hibernate).
6. Instance registered to a target group. Lambda Active with
reserved concurrency >= 1, timeout >= 60. Checkpoint target exists.
7. The 2-minute window has not already elapsed.
For configure-pipeline (set up EventBridge + SQS + Lambda):
5. EventBridge pattern matches "EC2 Spot Instance Interruption Warning". SQS queue has DLQ configured.
6. Lambda timeout >= 60. Lambda role has ec2:DescribeInstances,
elasticloadbalancing:DeregisterTargets, s3:PutObject.
7. Target group deregistration_delay.timeout_seconds is 30-60s.
For tune-replacement-strategy (modify Fleet or ASG):
5. Current AllocationStrategy identified. Proposed strategy is
capacity-optimized (recommended) or diversified with >= 3 pools.
6. For ASGs: CapacityRebalance enabled. ExcessCapacityTerminationPolicy
reviewed (default true).
For audit-diversification (read-only):
5. Fleet/ASG Overrides lists all instance types and AZs.
6. Count of unique instance families >= 3, unique AZs >= 3.
Graviton included (recommended).
For validate-placement-score (read-only):
5. get-spot-placement-scores called. Score >= 7 good, 4-6 marginal,
<= 3 avoid.
For configure-checkpointing (stateful workload):
5. Checkpoint target exists. Application supports checkpoint signals
(SIGTERM handler). Checkpoint frequency configured. Restore path
tested.
For audit-pipeline-health (read-only):
5. EventBridge ENABLED. SQS ApproximateAgeOfOldestMessage < 120s.
DLQ empty. Lambda Active, reserved concurrency >= 1. ELB delay
30-60s.
Interruption-handling failure-mode table (Lambda never invoked, ELB still sending traffic, Fleet did not auto-replace, etc.): see Error handling.
Step 2: OPERATION_COMPLETED or REVIEW_REQUIRED — emit operation plan
If all pre-checks pass AND the operation is read-only or low-risk
(audit-diversification, validate-placement-score, audit-pipeline-health,
handle-interruption where the pipeline executes successfully), emit
VERDICT: OPERATION_COMPLETED (for already-finished operations) or
the executable plan with the CONFIRM gate.
For operations requiring human review (tune-replacement-strategy changes
the allocation strategy, configure-pipeline on a production ASG, enabling
capacity rebalance, changing InstanceInterruptionBehavior), emit
VERDICT: REVIEW_REQUIRED with the specific items to review. The plan
includes:
- The exact AWS CLI command with all flags populated from the Spot/Fleet/
ASG configuration.
- The expected duration (Lambda execution 10-45 seconds; Spot Fleet
replacement launch 30 seconds to 3 minutes; ASG capacity rebalance
proactive replacement within minutes).
- The expected side-effects (instance deregistered, replacement launched,
capacity restored, checkpoint written).
- The CONFIRM gate prompt.
- The monitoring step (CloudWatch metrics for
SpotInstanceInterruptionCount,
ELB HealthyHostCount, Lambda errors).
Step 3: Execute behind CONFIRM gate
- MANDATORY CONFIRMATION GATE. Before any state-changing CLI
(
modify-spot-fleet-request, deregister-targets,
update-function-configuration, put-rule, put-targets,
update-auto-scaling-group, put-lifecycle-hook,
terminate-instances), emit: CONFIRM: About to <operation> on <target> in account <account> region <region>. This will <consequence>. Proceed? (yes/no). Do NOT execute until the operator
confirms.
- Capture pre-state for audit:
aws ec2 describe-spot-instance-requests --spot-instance-request-id <id> --output json > /tmp/<id>-pre-$(date +%s).json.
- For handle-interruption: the Lambda function is invoked by
EventBridge (or manually for testing). The Lambda deregisters the
instance from the ELB, writes a checkpoint, and signals the
application to shut down. Monitor via:
aws logs tail /aws/lambda/<fn> --follow --since 2m.
- For tune-replacement-strategy: execute the
modify-spot-fleet-request or update-auto-scaling-group. The
change is immediate — new launches use the new strategy. Existing
instances are not restarted.
Step 4: Post-verification — OPERATION_COMPLETED
After the operation finishes, run post-verification. ALL checks must
pass for OPERATION_COMPLETED.
- For handle-interruption: confirm the instance was deregistered from
the ELB (
describe-target-health shows TargetHealth.State: unused
or the target is gone), the checkpoint was written to S3 or
DynamoDB (verify the object or item exists with a recent
timestamp), and the replacement launched (Spot Fleet
FulfilledCapacity is back to TargetCapacity, or ASG
DesiredCapacity is met).
- For configure-pipeline: test the pipeline by emitting a synthetic
interruption event (
aws events put-events with a test payload) and
confirm the Lambda executes successfully.
- For tune-replacement-strategy: confirm the
AllocationStrategy changed via describe-spot-fleet-requests or
describe-auto-scaling-groups. Monitor for the next 24 hours —
the interruption rate should decrease with capacity-optimized.
- For audit-diversification: report the count of unique instance
families and AZs. Flag if below the 3 x 3 minimum.
- For audit-pipeline-health: report the health of each component
(EventBridge, SQS, Lambda, ELB target group, ASG lifecycle hook).
- Application error rate: confirm the ELB
HTTPCode_ELB_5XX_Count
and TargetConnectionErrorCount metrics did not spike during the
interruption handling window.
- Checkpoint integrity: for stateful workloads, verify the
replacement loaded the checkpoint and resumed from the correct
state.
If ANY verification fails, emit VERDICT: ERROR with the failure
details — do not claim OPERATION_COMPLETED. A failed verification
typically means the pipeline is misconfigured (Lambda timeout, missing
permissions, ELB delay too long) or the diversification is insufficient.
Output format (per operation)
OPERATION: <handle-interruption | configure-pipeline | tune-replacement-strategy | audit-diversification | validate-placement-score | configure-checkpointing | audit-pipeline-health>
VERDICT: OPERATION_COMPLETED | REVIEW_REQUIRED
TARGET: <spot-instance-id-or-fleet-id-or-asg-name> (strategy: <current>, target: <target-config>)
PRE_CHECKS:
- [PASS] <check description>
- [REVIEW] <check description> — <item to review>
STEPS:
1. <CLI command with flags populated>
2. <wait / monitoring command>
3. <next step>
POST_VERIFY:
- [PASS] <verification description>
- [FAIL] <verification description> — <reason>
NOTES: <monitoring, caveats, diversification recommendation>
Worked example — handle-interruption (OPERATION_COMPLETED)
OPERATION: handle-interruption
VERDICT: OPERATION_COMPLETED
TARGET: i-0abc123def456 (Spot Fleet: sfr-abc123, ASG: prod-web-asg)
PRE_CHECKS:
- [PASS] EventBridge warning received: instance-action=terminate,
event time 2026-08-11T14:23:00Z (105 seconds remaining in window)
- [PASS] Instance i-0abc123def456 registered to target group
arn:aws:elasticloadbalancing:us-east-1:111111111111:targetgroup/prod-web-tg/...
- [PASS] Graceful-shutdown Lambda prod-spot-shutdown State: Active,
Timeout: 90, ReservedConcurrentExecutions: 10
- [PASS] Lambda execution role has elasticloadbalancing:DeregisterTargets,
s3:PutObject on arn:aws:s3:::prod-checkpoints/*
- [PASS] ELB deregistration_delay.timeout_seconds: 45 (fits within
the 2-minute window)
- [PASS] ASG InstanceTerminating lifecycle hook HeartbeatTimeout: 120
STEPS:
1. Lambda prod-spot-shutdown invoked by EventBridge at 14:23:01Z.
2. Lambda deregistered i-0abc123def456 from target group
(deregistration draining started at 14:23:03Z).
3. Lambda sent SIGTERM to the application process (via SSM
SendCommand) — application flushed in-flight requests and wrote
checkpoint to s3://prod-checkpoints/i-0abc123def456/state.json
(completed 14:23:38Z).
4. ELB target entered "unused" state at 14:23:48Z (45s drain).
5. Instance terminated by Spot service at 14:25:00Z.
6. ASG launched replacement i-0new456 (capacity-optimized, m6a.large
in us-east-1b) at 14:25:15Z. Replacement "running" at 14:27:45Z.
7. Replacement registered to target group, health checks passing at
14:28:30Z.
POST_VERIFY:
- [PASS] i-0abc123def456 deregistered from target group
- [PASS] Checkpoint written: s3://prod-checkpoints/i-0abc123def456/state.json
(timestamp 2026-08-11T14:23:38Z)
- [PASS] Spot Fleet FulfilledCapacity: 10 (matches TargetCapacity)
- [PASS] Replacement i-0new456 TargetHealth.State: healthy
- [PASS] ELB HTTPCode_ELB_5XX_Count: no spike during the window
- [PASS] Application resumed from checkpoint on replacement
NOTES:
- Total handling time: 35 seconds (deregister + checkpoint + flush).
Well within the 2-minute window.
- Spot Fleet auto-replaced the interrupted instance via the
capacity-optimized allocation strategy. Replacement launched in
us-east-1b (different AZ from the interrupted instance, which was
in us-east-1a).
- The ASG CapacityRebalance was enabled — a proactive replacement
was already in progress when the interruption warning fired,
reducing the effective gap to zero.
Anti-Patterns — NEVER
NEVER rely on Spot without a graceful-shutdown pipeline. Without
EventBridge + a consumer, the instance is terminated with no warning.
The 2-minute warning is the ONLY notification channel.
NEVER set the ELB deregistration delay longer than the 2-minute
window. The default ALB delay of 300 seconds means the instance is
terminated before draining finishes. Set to 30-60 seconds for Spot.
NEVER use lowest-price with a single pool for production. Use
capacity-optimized with 3+ families across 3+ AZs. The 5-8% cost
premium is offset by the 5-10x reduction in interruptions.
NEVER assume the 2-minute warning is always exactly 120 seconds.
Design the pipeline to complete within 90 seconds.
NEVER change InstanceInterruptionBehavior on a running instance —
it is set at launch and immutable.
NEVER forget the Lambda resource-based policy for EventBridge
(events.amazonaws.com). Without it, direct Lambda invocation fails
silently.
NEVER use Spot Block for new capacity — it is deprecated (2024+).
Use On-Demand Capacity Reservations for predictable capacity.
NEVER rely on hibernate as the only state-preservation. Hibernate
saves memory to EBS but EC2 may terminate the instance afterward.
Use application-level checkpointing to S3 or DynamoDB.
NEVER ignore SQS queue depth. Messages older than 120 seconds are
stale — the instance is already terminated. Set a CloudWatch alarm.
NEVER diversify across instance types within a single family only.
c5.large, c5.xlarge, c5.2xlarge share the same pools. Mix
families (c5, m5, c6g) for true independence.
NEVER assume Spot Fleet auto-replacement restores state. Replacements
start from scratch — stateful workloads must checkpoint independently.
NEVER auto-execute a state-changing Spot CLI without the CONFIRM gate.
Allocation strategy, target capacity, and interruption behavior
changes affect billing and availability.
NEVER assume capacity rebalance eliminates all interruptions. It is
proactive but cannot prevent every reclaim. The graceful-shutdown
pipeline is still required.
NEVER forget to test the pipeline with a synthetic event. The first
real interruption is NOT the time to discover the pipeline is broken.
Pre-flight safety checks (run before any remediation CLI)
MANDATORY CONFIRMATION GATE. Before any state-changing operation
(modify-spot-fleet-request, update-auto-scaling-group,
put-lifecycle-hook, modify-target-group-attributes,
terminate-instances, deregister-targets,
update-function-configuration, put-rule, put-targets),
emit: CONFIRM: About to <operation> on <target> in account <account> region <region>. This will <consequence>. Proceed? (yes/no).
Capture pre-state for audit. describe-spot-fleet-requests or
describe-auto-scaling-groups JSON saved to /tmp/<id>-pre-$(date +%s).json.
Verify pipeline health. EventBridge rule ENABLED. SQS queue
depth stable, oldest message under 120s, DLQ configured. Lambda
State: Active, Timeout >= 60, reserved concurrency not 0. ELB
deregistration_delay.timeout_seconds 30-60s for Spot targets. ASG
lifecycle hook HeartbeatTimeout >= 120.
Verify diversification. At least 3 instance families across 3
AZs. Use capacity-optimized allocation strategy. Enable
CapacityRebalance on ASGs for proactive replacement.
References (load on demand)
- Worked examples - tune-replacement-strategy (REVIEW_REQUIRED) and configure-pipeline walkthroughs
- Error handling - failure-mode table and malformed-input VERDICT: ERROR protocol
- Diagnostic commands - live-account pre-flight command listing
- Advanced patterns - expert behaviors and 2024-2026 feature notes
- Diversification and strategy - allocation strategies, diversification math, placement score
- Graceful shutdown pipeline - EventBridge + SQS + Lambda architecture and checkpointing
Domain
AWS CloudOps / EC2 Spot Instance Interruption Handling, Graceful
Shutdown & Capacity Resilience.
AWS documentation
1---2name: ec2-spot-interruption-operator3description: Operates Amazon EC2 Spot Instance interruption handling workflows end-to-end — EventBridge Instance Interruption Warning (2-minute notice), Spot Instance Interruption Queue (SQS), Lambda functions for graceful shutdown (drain from ELB, checkpoint state to S3 or DynamoDB), capacity rebalance notifications, replacement strategy selection (capacity-optimized vs diversified vs lowest-price), Spot Fleet auto-replacement, Auto Scaling Group capacity rebalance, stateful workload checkpointing patterns (S3 multipart, DynamoDB point-in-time), Spot Block deprecation awareness, Spot placement score for capacity assessment, instance diversification across families and AZs (minimum 3 families for 99.9% availability), hibernate vs stop vs terminate on interruption behavior, and integration with Application Load Balancer / Network Load Balancer connection draining. Runs deterministic pre-checks (EventBridge rule health, SQS queue depth, Lambda concurrency, deregister delay, lifecycle hook timeout) behind a CONFIRM gate...4license: Apache-2.05compatibility: Agent runtime that reads SKILL.md (Claude Code, Cursor, Windsurf, Codex, Gemini). No AWS CLI required for offline plan classification. Live-account operations use aws ec2 describe-spot-instance-requests, describe-spot-fleet- requests, describe-instances, modify-spot-fleet-request, aws events put-rule, put-targets, aws sqs receive-message, delete-message, get-queue-attributes, aws lambda get-function-configuration, invoke, aws autoscaling describe-auto-scaling-groups, put-lifecycle-hook, aws...6metadata:7 source: https://skillmd.com/skills/jhkchan/ec2-spot-interruption-operator8 domain: aws-cloudops9 complexity: high10 requires_llm: 'true'11 phase: '4'12 supports_pipeline: 'true'13 entry_point: 'false'14 family: Compute15 task_type: operate16 skill_class: capability17 lifecycle_status: active18 verdict_shape: OPERATION_COMPLETED | REVIEW_REQUIRED19 when_to_use: 'Handling a live Spot Instance Interruption Warning (the 2-minute notice), configuring an EventBridge rule plus SQS queue plus Lambda pipeline for graceful shutdown, tuning a Spot Fleet or ASG for capacity rebalance and auto-replacement, selecting a replacement strategy (capacity-optimized, diversified, lowest-price), validating Spot placement score before a launch, auditing instance diversification across families and AZs (target: minimum 3 instance families for 99.9% availability), configuring stateful workload checkpointing to S3 or DynamoDB, or auditing the interruption-handling pipeline health.'20 activation_triggers: Spot interruption warning, Spot Instance interrupted, 2-minute warning Spot, graceful shutdown Spot, drain Spot from ELB, checkpoint Spot instance, capacity rebalance Spot, Spot Fleet replacement, ASG capacity rebalance, Spot placement score, instance diversification Spot, Spot Block deprecation, hibernate Spot instance, Spot graceful shutdown pipeline, Spot interruption queue21 invocation_schema: 'Input: either (a) a Spot Instance or Spot Fleet configuration (describe-spot-instance-requests, describe-spot-fleet-requests, or describe-auto-scaling-groups output) plus the intended operation (handle-interruption, configure-pipeline, tune-replacement-strategy, audit-diversification, validate-placement-score, configure-checkpointing, audit-pipeline-health), OR (b) an EventBridge Instance Interruption Warning event for live handling. Output: deterministic OPERATION / VERDICT / PRE_CHECKS / STEPS / POST_VERIFY / NOTES block per operation, where VERDICT is OPERATION_COMPLETED or REVIEW_REQUIRED.'22 version: 0.1.023 author: Jacky Chan — AWS Community Builder24 keywords: Spot Instance, Spot interruption, 2-minute warning, Instance Interruption Warning, EventBridge Spot, SQS interruption queue, graceful shutdown, drain from ELB, checkpoint state, capacity rebalance, capacity-optimized, diversified allocation, Spot Fleet, ASG capacity rebalance, stateful checkpointing, Spot placement score, instance diversification, hibernate on interruption, Spot Block deprecation, Spot two-minute warning25 tags: aws, ec2, spot, compute, interruption, autoscaling, eventbridge, operate26---2728# EC2 Spot Instance Interruption Operator2930## What this skill does3132Executes EC2 Spot Instance interruption-handling operations correctly and33safely. Runs deterministic pre-checks before any state-changing CLI34(EventBridge rule is ENABLED, SQS queue is healthy with acceptable depth,35Lambda graceful-shutdown function is Active with sufficient concurrency,36ALB/NLB target group deregistration delay is configured, ASG lifecycle37hook timeout accommodates the 2-minute window, Spot Fleet or ASG has a38valid replacement strategy with sufficient diversification), executes39the graceful-shutdown pipeline behind a CONFIRM gate, and verifies the40result by confirming the interrupted instance was drained from the load41balancer, state was checkpointed, replacement launched, and the42application's error rate stayed within the tolerance band. Every43interruption-warning handler produces the drain-and-checkpoint sequence;44every fleet tuning surfaces the capacity-optimized recommendation and45the 3-family diversification minimum.4647## Quick navigation4849| Section | What it covers | When to read it |50|---|---|---|51| **§ Quick reference** | Verdict thresholds (REVIEW_REQUIRED/OPERATION_COMPLETED) + pre-check priority | Before any operation |52| **§ Mindset** | Why the 2-minute warning is the safety window, capacity-optimized as the default, diversification math | Understanding the safety model |53| **§ Pre-flight** | Spot/Fleet/ASG metadata gate — state, capacity, allocation strategy, diversification | Before executing any CLI |54| **§ Process** | Per-operation planning: handle-interruption, configure-pipeline, tune-replacement, audit-diversification, validate-placement, configure-checkpointing, audit-health | When choosing which operation to run |55| **§ Output format** | Structured OPERATION/VERDICT/PRE_CHECKS/STEPS/POST_VERIFY template | Formatting the response |56| **§ Anti-Patterns** | NEVER list — common mistakes that lose in-flight work or strand the fleet below capacity | Review before risky operations |57| **§ Pre-flight safety** | Capture pre-state, verify pipeline health, validate checkpoint target | Defense-in-depth |5859## Quick reference — verdict thresholds6061| Verdict | Trigger condition | Action |62|---|---|---|63| `REVIEW_REQUIRED` | Operation plan is ready but requires human review before execution (replacing lowest-price with capacity-optimized changes billing behavior, reducing a Spot Fleet's target capacity below current running count, changing the interruption behavior from terminate to hibernate or stop, modifying an ASG lifecycle hook that affects in-flight deployments, enabling capacity rebalance on a production ASG) | Emit plan with the specific items to review, wait for operator approval |64| `OPERATION_COMPLETED` | Interruption handling finished and post-verification passed (instance drained from ELB, state checkpointed to S3 or DynamoDB, replacement launched, ASG/Fleet capacity restored, application error rate within tolerance) | Emit verification results, monitoring plan |6566**Priority order for pre-checks (apply in this sequence, any failure routes67to REVIEW_REQUIRED with the failure surfaced):**68691. **Spot request reachability** — the Spot Instance Request exists and70 `State: active` (not `cancelled`, `closed`, `failed`). The instance71 itself is `running`.722. **EventBridge rule health** — the73 `EC2 Spot Instance Interruption Warning` rule is `ENABLED`, targets74 the SQS queue or Lambda function, and the rule's event pattern75 matches `"detail-type": ["EC2 Spot Instance Interruption Warning"]`.763. **SQS queue health** — the interruption queue is healthy:77 `ApproximateNumberOfMessagesVisible` is not climbing (no consumer78 lag), `ApproximateAgeOfOldestMessage` is under the 2-minute window79 threshold, the dead-letter queue is empty.804. **Lambda graceful-shutdown function** — `State: Active`,81 `ReservedConcurrentExecutions >= 1` (not the stealth-throttle82 condition), timeout >= 60 seconds (enough for drain + checkpoint83 within the 2-minute window), runtime is a supported version.845. **ALB/NLB target group** — the instance is a registered target,85 `deregistration_delay.timeout_seconds` is set to a value that allows86 in-flight requests to complete (typically 30-60 seconds).876. **ASG lifecycle hook** — if the instance is in an ASG, the88 `InstanceLaunching` and `InstanceTerminating` lifecycle hooks have89 timeouts that accommodate the 2-minute window (default 60 minutes90 for launching, but the termination hook should be at least 12091 seconds to give the graceful-shutdown pipeline room).927. **Replacement strategy** — the Spot Fleet or ASG has a valid93 allocation strategy (`capacity-optimized` recommended,94 `capacity-optimized-prioritized`, `diversified`,95 `lowest-price` with multiple pools).968. **Diversification** — the Spot Fleet or ASG specifies at least 397 instance families across at least 3 AZs (the minimum for 99.9%98 availability per the AWS Spot best-practices guidance).999. **Capacity rebalance** — `AllocationStrategy` on the ASG or Spot100 Fleet supports capacity rebalance (or is explicitly enabled via101 `CapacityRebalance` on the ASG).102103**Cost/time baselines (2026):**104105- EventBridge Instance Interruption Warning: delivered 2 minutes (120106 seconds) before the actual interruption. This is the maximum107 graceful-shutdown window. Some interruption types (capacity-reclaim)108 may provide less notice in practice — design for 90 seconds to be109 safe.110- ELB target deregistration: enters `draining` state, waits111 `deregistration_delay.timeout_seconds` (default 300 seconds for ALB,112 configurable 0-3600). Within the 2-minute window, set this to 30-60113 seconds to leave room for checkpointing.114- Lambda graceful-shutdown execution: typically 10-45 seconds (drain115 from ELB, write checkpoint to S3, signal the application to flush).116- Spot Fleet auto-replacement: the Fleet launches a replacement within117 seconds of the interruption. Provisioning time depends on the AMI118 and instance type (30 seconds to 3 minutes).119- Spot placement score: a single API call120 (`get-spot-placement-scores`) returns in under 5 seconds for up to121 50,000 capacity combinations.122123## Mindset124125**One-line takeaway:** the 2-minute Instance Interruption Warning is126the safety window — it is enough time to drain from the load balancer127and checkpoint state, but only if the pipeline is pre-wired and tested.128Spot is NOT unreliable; it is a different reliability model that129rewards diversification and graceful shutdown. Driven by three EC2130Spot realities:131132- **The 2-minute warning allows graceful shutdown (drain + checkpoint).**133 When EC2 reclaims a Spot Instance, EventBridge emits an134 `EC2 Spot Instance Interruption Warning` event 2 minutes before the135 actual interruption. A Lambda function (or any event consumer) can136 use this window to deregister the instance from the load balancer,137 flush in-flight transactions, write a checkpoint to S3 or DynamoDB,138 and signal the application to shut down cleanly. Without this139 pipeline, the instance is terminated with no warning and all in-140 flight work is lost.141142- **Capacity-optimized allocation strategy minimizes interruptions.**143 The `capacity-optimized` strategy (and its144 `capacity-optimized-prioritized` variant) launches Spot Instances145 from the pools with the lowest interruption rates, rather than the146 lowest price. This reduces the frequency of interruptions at a147 marginally higher cost. For production workloads, capacity-optimized148 is the default recommendation — the cost saving from `lowest-price`149 is eaten by the disruption of frequent interruptions.150151- **Diversify across at least 3 instance families for 99.9%152 availability.** Spot interruption rates vary by instance family, AZ,153 and time of day. A fleet that runs on a single instance type in a154 single AZ is at high risk of a simultaneous reclaim. Diversifying155 across at least 3 instance families (e.g., `m5`, `m6a`, `c6g`) and156 at least 3 AZs gives a 99.9% availability profile for Spot — the157 mathematical basis is that the probability of all pools being158 interrupted simultaneously drops exponentially with the number of159 independent pools.160161## Pre-flight: Spot/Fleet/ASG metadata gate162163Run before classification. Misclassifying these produces wrong plans.164165**Pagination:** `describe-spot-instance-requests` paginates at 1,000/page166— drain `--next-token` to completion. Filter by `--state active`.167168**Live-account pre-flight (skip if offline plan audit):** the 7 read-only commands169(describe-spot-instance-requests ... autoscaling describe-auto-scaling-groups) are listed in [Diagnostic commands](references/diagnostic-commands.md).170171Malformed-input protocol (VERDICT: ERROR + REMEDIATION): see [Error handling](references/error-handling.md).172173| Spot/Fleet/ASG attribute | Effect on operation |174|---|---|175| `State: cancelled` or `closed` | Spot request no longer active. Instance may still be running but is not managed by the Spot service. |176| `InstanceInterruptionBehavior: terminate` | Default. Instance is terminated on interruption. No state preserved. |177| `InstanceInterruptionBehavior: stop` | Instance is stopped (EBS-backed only). Can be restarted when capacity returns. |178| `InstanceInterruptionBehavior: hibernate` | Instance hibernates (memory state saved to EBS). Requires the instance to be hibernation-enabled at launch. |179| EventBridge rule `State: DISABLED` | Interruption warnings are NOT delivered. The graceful-shutdown pipeline is dark. |180| SQS `ApproximateAgeOfOldestMessage > 120` | Messages are older than the 2-minute window — the consumer (Lambda) is lagging. Interruption warnings in the queue are already stale. |181| Lambda `ReservedConcurrentExecutions: 0` | Stealth throttle. The graceful-shutdown Lambda will never execute. Every interruption is a hard termination. |182| Lambda `Timeout < 60` | May not complete drain + checkpoint within the 2-minute window. |183| Spot Fleet `AllocationStrategy: lowestPrice` with 1 pool | High interruption risk. Recommend switching to capacity-optimized with >= 3 pools. |184| Spot Fleet `FulfilledCapacity < TargetCapacity` | Fleet is below target — instances are being interrupted faster than replacements launch. |185| ASG `CapacityRebalance: false` | Capacity rebalance is off. The ASG does not proactively replace at-risk instances. |186| ASG `MixedInstancesPolicy` with 1 instance type | No diversification. High risk of simultaneous reclaim. |187188## Process — operation planning (apply in order)189190### Step 0: Expert knowledge - non-obvious EC2 Spot interruption behaviors (condensed)191192- The 2-minute warning is a maximum, not a guarantee - design the pipeline to finish within 90 seconds. EventBridge is the ONLY notification channel.193- `InstanceInterruptionBehavior` is set at launch and immutable (terminate / stop / hibernate).194- Capacity rebalance is proactive (launches replacements BEFORE the warning). Prefer `capacity-optimized` over `lowest-price` for production.195- Diversification math: 3 families x 3 AZs = 9 pools for 99.9% availability. Spot placement score is a forecast, not a guarantee.196- Spot Block is deprecated (2024+). ELB deregistration delay must fit within 2 minutes; lifecycle hooks fire on termination, not the warning.197- Full deep dives (Graviton rates, StatusCode early warning, Fleet auto-replacement): [Advanced patterns](references/advanced-patterns.md).198199### Step 1: Pre-check gate — REVIEW_REQUIRED if any check needs human attention200201Run ALL pre-checks for the chosen operation. If any check fails or202requires human review, the verdict is REVIEW_REQUIRED with the specific203items listed. Do NOT execute until the operator reviews.204205**For ALL operations:**2061. Spot Instance Request exists and `State: active`.2072. Instance is `running`.2083. EventBridge interruption-warning rule is `ENABLED`.2094. SQS queue (or Lambda target) is healthy — no consumer lag.210211**For handle-interruption (live EventBridge warning event):**2125. Event `detail.instance-action` determines shutdown sequence213 (`terminate`, `stop`, or `hibernate`).2146. Instance registered to a target group. Lambda `Active` with215 reserved concurrency >= 1, timeout >= 60. Checkpoint target exists.2167. The 2-minute window has not already elapsed.217218**For configure-pipeline (set up EventBridge + SQS + Lambda):**2195. EventBridge pattern matches `"EC2 Spot Instance Interruption220 Warning"`. SQS queue has DLQ configured.2216. Lambda timeout >= 60. Lambda role has `ec2:DescribeInstances`,222 `elasticloadbalancing:DeregisterTargets`, `s3:PutObject`.2237. Target group `deregistration_delay.timeout_seconds` is 30-60s.224225**For tune-replacement-strategy (modify Fleet or ASG):**2265. Current `AllocationStrategy` identified. Proposed strategy is227 `capacity-optimized` (recommended) or `diversified` with >= 3 pools.2286. For ASGs: `CapacityRebalance` enabled. `ExcessCapacityTerminationPolicy`229 reviewed (default `true`).230231**For audit-diversification (read-only):**2325. Fleet/ASG `Overrides` lists all instance types and AZs.2336. Count of unique instance families >= 3, unique AZs >= 3.234 Graviton included (recommended).235236**For validate-placement-score (read-only):**2375. `get-spot-placement-scores` called. Score >= 7 good, 4-6 marginal,238 <= 3 avoid.239240**For configure-checkpointing (stateful workload):**2415. Checkpoint target exists. Application supports checkpoint signals242 (SIGTERM handler). Checkpoint frequency configured. Restore path243 tested.244245**For audit-pipeline-health (read-only):**2465. EventBridge `ENABLED`. SQS `ApproximateAgeOfOldestMessage` < 120s.247 DLQ empty. Lambda `Active`, reserved concurrency >= 1. ELB delay248 30-60s.249250Interruption-handling failure-mode table (Lambda never invoked, ELB still sending traffic, Fleet did not auto-replace, etc.): see [Error handling](references/error-handling.md).251252### Step 2: OPERATION_COMPLETED or REVIEW_REQUIRED — emit operation plan253254If all pre-checks pass AND the operation is read-only or low-risk255(audit-diversification, validate-placement-score, audit-pipeline-health,256handle-interruption where the pipeline executes successfully), emit257`VERDICT: OPERATION_COMPLETED` (for already-finished operations) or258the executable plan with the CONFIRM gate.259260For operations requiring human review (tune-replacement-strategy changes261the allocation strategy, configure-pipeline on a production ASG, enabling262capacity rebalance, changing `InstanceInterruptionBehavior`), emit263`VERDICT: REVIEW_REQUIRED` with the specific items to review. The plan264includes:265266- The exact AWS CLI command with all flags populated from the Spot/Fleet/267 ASG configuration.268- The expected duration (Lambda execution 10-45 seconds; Spot Fleet269 replacement launch 30 seconds to 3 minutes; ASG capacity rebalance270 proactive replacement within minutes).271- The expected side-effects (instance deregistered, replacement launched,272 capacity restored, checkpoint written).273- The CONFIRM gate prompt.274- The monitoring step (CloudWatch metrics for `SpotInstanceInterruptionCount`,275 ELB `HealthyHostCount`, Lambda errors).276277### Step 3: Execute behind CONFIRM gate278279- **MANDATORY CONFIRMATION GATE.** Before any state-changing CLI280 (`modify-spot-fleet-request`, `deregister-targets`,281 `update-function-configuration`, `put-rule`, `put-targets`,282 `update-auto-scaling-group`, `put-lifecycle-hook`,283 `terminate-instances`), emit: `CONFIRM: About to <operation> on284 <target> in account <account> region <region>. This will285 <consequence>. Proceed? (yes/no)`. Do NOT execute until the operator286 confirms.287- Capture pre-state for audit: `aws ec2 describe-spot-instance-requests288 --spot-instance-request-id <id> --output json > /tmp/<id>-pre-$(date289 +%s).json`.290- For handle-interruption: the Lambda function is invoked by291 EventBridge (or manually for testing). The Lambda deregisters the292 instance from the ELB, writes a checkpoint, and signals the293 application to shut down. Monitor via:294 `aws logs tail /aws/lambda/<fn> --follow --since 2m`.295- For tune-replacement-strategy: execute the296 `modify-spot-fleet-request` or `update-auto-scaling-group`. The297 change is immediate — new launches use the new strategy. Existing298 instances are not restarted.299300### Step 4: Post-verification — OPERATION_COMPLETED301302After the operation finishes, run post-verification. ALL checks must303pass for `OPERATION_COMPLETED`.3043051. For handle-interruption: confirm the instance was deregistered from306 the ELB (`describe-target-health` shows `TargetHealth.State: unused`307 or the target is gone), the checkpoint was written to S3 or308 DynamoDB (verify the object or item exists with a recent309 timestamp), and the replacement launched (Spot Fleet310 `FulfilledCapacity` is back to `TargetCapacity`, or ASG311 `DesiredCapacity` is met).3122. For configure-pipeline: test the pipeline by emitting a synthetic313 interruption event (`aws events put-events` with a test payload) and314 confirm the Lambda executes successfully.3153. For tune-replacement-strategy: confirm the316 `AllocationStrategy` changed via `describe-spot-fleet-requests` or317 `describe-auto-scaling-groups`. Monitor for the next 24 hours —318 the interruption rate should decrease with `capacity-optimized`.3194. For audit-diversification: report the count of unique instance320 families and AZs. Flag if below the 3 x 3 minimum.3215. For audit-pipeline-health: report the health of each component322 (EventBridge, SQS, Lambda, ELB target group, ASG lifecycle hook).3236. Application error rate: confirm the ELB `HTTPCode_ELB_5XX_Count`324 and `TargetConnectionErrorCount` metrics did not spike during the325 interruption handling window.3267. Checkpoint integrity: for stateful workloads, verify the327 replacement loaded the checkpoint and resumed from the correct328 state.329330If ANY verification fails, emit `VERDICT: ERROR` with the failure331details — do not claim OPERATION_COMPLETED. A failed verification332typically means the pipeline is misconfigured (Lambda timeout, missing333permissions, ELB delay too long) or the diversification is insufficient.334335## Output format (per operation)336337```text338OPERATION: <handle-interruption | configure-pipeline | tune-replacement-strategy | audit-diversification | validate-placement-score | configure-checkpointing | audit-pipeline-health>339VERDICT: OPERATION_COMPLETED | REVIEW_REQUIRED340TARGET: <spot-instance-id-or-fleet-id-or-asg-name> (strategy: <current>, target: <target-config>)341PRE_CHECKS:342 - [PASS] <check description>343 - [REVIEW] <check description> — <item to review>344STEPS:345 1. <CLI command with flags populated>346 2. <wait / monitoring command>347 3. <next step>348POST_VERIFY:349 - [PASS] <verification description>350 - [FAIL] <verification description> — <reason>351NOTES: <monitoring, caveats, diversification recommendation>352```353354### Worked example — handle-interruption (OPERATION_COMPLETED)355356```text357OPERATION: handle-interruption358VERDICT: OPERATION_COMPLETED359TARGET: i-0abc123def456 (Spot Fleet: sfr-abc123, ASG: prod-web-asg)360PRE_CHECKS:361 - [PASS] EventBridge warning received: instance-action=terminate,362 event time 2026-08-11T14:23:00Z (105 seconds remaining in window)363 - [PASS] Instance i-0abc123def456 registered to target group364 arn:aws:elasticloadbalancing:us-east-1:111111111111:targetgroup/prod-web-tg/...365 - [PASS] Graceful-shutdown Lambda prod-spot-shutdown State: Active,366 Timeout: 90, ReservedConcurrentExecutions: 10367 - [PASS] Lambda execution role has elasticloadbalancing:DeregisterTargets,368 s3:PutObject on arn:aws:s3:::prod-checkpoints/*369 - [PASS] ELB deregistration_delay.timeout_seconds: 45 (fits within370 the 2-minute window)371 - [PASS] ASG InstanceTerminating lifecycle hook HeartbeatTimeout: 120372STEPS:373 1. Lambda prod-spot-shutdown invoked by EventBridge at 14:23:01Z.374 2. Lambda deregistered i-0abc123def456 from target group375 (deregistration draining started at 14:23:03Z).376 3. Lambda sent SIGTERM to the application process (via SSM377 SendCommand) — application flushed in-flight requests and wrote378 checkpoint to s3://prod-checkpoints/i-0abc123def456/state.json379 (completed 14:23:38Z).380 4. ELB target entered "unused" state at 14:23:48Z (45s drain).381 5. Instance terminated by Spot service at 14:25:00Z.382 6. ASG launched replacement i-0new456 (capacity-optimized, m6a.large383 in us-east-1b) at 14:25:15Z. Replacement "running" at 14:27:45Z.384 7. Replacement registered to target group, health checks passing at385 14:28:30Z.386POST_VERIFY:387 - [PASS] i-0abc123def456 deregistered from target group388 - [PASS] Checkpoint written: s3://prod-checkpoints/i-0abc123def456/state.json389 (timestamp 2026-08-11T14:23:38Z)390 - [PASS] Spot Fleet FulfilledCapacity: 10 (matches TargetCapacity)391 - [PASS] Replacement i-0new456 TargetHealth.State: healthy392 - [PASS] ELB HTTPCode_ELB_5XX_Count: no spike during the window393 - [PASS] Application resumed from checkpoint on replacement394NOTES:395 - Total handling time: 35 seconds (deregister + checkpoint + flush).396 Well within the 2-minute window.397 - Spot Fleet auto-replaced the interrupted instance via the398 capacity-optimized allocation strategy. Replacement launched in399 us-east-1b (different AZ from the interrupted instance, which was400 in us-east-1a).401 - The ASG CapacityRebalance was enabled — a proactive replacement402 was already in progress when the interruption warning fired,403 reducing the effective gap to zero.404```405406## Anti-Patterns — NEVER407408- NEVER rely on Spot without a graceful-shutdown pipeline. Without409 EventBridge + a consumer, the instance is terminated with no warning.410 The 2-minute warning is the ONLY notification channel.411412- NEVER set the ELB deregistration delay longer than the 2-minute413 window. The default ALB delay of 300 seconds means the instance is414 terminated before draining finishes. Set to 30-60 seconds for Spot.415416- NEVER use `lowest-price` with a single pool for production. Use417 `capacity-optimized` with 3+ families across 3+ AZs. The 5-8% cost418 premium is offset by the 5-10x reduction in interruptions.419420- NEVER assume the 2-minute warning is always exactly 120 seconds.421 Design the pipeline to complete within 90 seconds.422423- NEVER change `InstanceInterruptionBehavior` on a running instance —424 it is set at launch and immutable.425426- NEVER forget the Lambda resource-based policy for EventBridge427 (`events.amazonaws.com`). Without it, direct Lambda invocation fails428 silently.429430- NEVER use Spot Block for new capacity — it is deprecated (2024+).431 Use On-Demand Capacity Reservations for predictable capacity.432433- NEVER rely on hibernate as the only state-preservation. Hibernate434 saves memory to EBS but EC2 may terminate the instance afterward.435 Use application-level checkpointing to S3 or DynamoDB.436437- NEVER ignore SQS queue depth. Messages older than 120 seconds are438 stale — the instance is already terminated. Set a CloudWatch alarm.439440- NEVER diversify across instance types within a single family only.441 `c5.large`, `c5.xlarge`, `c5.2xlarge` share the same pools. Mix442 families (`c5`, `m5`, `c6g`) for true independence.443444- NEVER assume Spot Fleet auto-replacement restores state. Replacements445 start from scratch — stateful workloads must checkpoint independently.446447- NEVER auto-execute a state-changing Spot CLI without the CONFIRM gate.448 Allocation strategy, target capacity, and interruption behavior449 changes affect billing and availability.450451- NEVER assume capacity rebalance eliminates all interruptions. It is452 proactive but cannot prevent every reclaim. The graceful-shutdown453 pipeline is still required.454455- NEVER forget to test the pipeline with a synthetic event. The first456 real interruption is NOT the time to discover the pipeline is broken.457458## Pre-flight safety checks (run before any remediation CLI)459460- **MANDATORY CONFIRMATION GATE.** Before any state-changing operation461 (`modify-spot-fleet-request`, `update-auto-scaling-group`,462 `put-lifecycle-hook`, `modify-target-group-attributes`,463 `terminate-instances`, `deregister-targets`,464 `update-function-configuration`, `put-rule`, `put-targets`),465 emit: `CONFIRM: About to <operation> on <target> in account466 <account> region <region>. This will <consequence>. Proceed?467 (yes/no)`.468469- **Capture pre-state for audit.** `describe-spot-fleet-requests` or470 `describe-auto-scaling-groups` JSON saved to `/tmp/<id>-pre-$(date471 +%s).json`.472473- **Verify pipeline health.** EventBridge rule `ENABLED`. SQS queue474 depth stable, oldest message under 120s, DLQ configured. Lambda475 `State: Active`, `Timeout >= 60`, reserved concurrency not 0. ELB476 `deregistration_delay.timeout_seconds` 30-60s for Spot targets. ASG477 lifecycle hook `HeartbeatTimeout >= 120`.478479- **Verify diversification.** At least 3 instance families across 3480 AZs. Use `capacity-optimized` allocation strategy. Enable481 `CapacityRebalance` on ASGs for proactive replacement.482483## References (load on demand)484485- [Worked examples](references/worked-examples.md) - tune-replacement-strategy (REVIEW_REQUIRED) and configure-pipeline walkthroughs486- [Error handling](references/error-handling.md) - failure-mode table and malformed-input VERDICT: ERROR protocol487- [Diagnostic commands](references/diagnostic-commands.md) - live-account pre-flight command listing488- [Advanced patterns](references/advanced-patterns.md) - expert behaviors and 2024-2026 feature notes489- [Diversification and strategy](references/diversification-and-strategy.md) - allocation strategies, diversification math, placement score490- [Graceful shutdown pipeline](references/graceful-shutdown-pipeline.md) - EventBridge + SQS + Lambda architecture and checkpointing491492## Domain493494AWS CloudOps / EC2 Spot Instance Interruption Handling, Graceful495Shutdown & Capacity Resilience.496497## AWS documentation498499- **Amazon EC2 Spot Instances** — https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-spot-instances.html500- **Spot Instance interruption notices** — https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-interruptions.html501- **Best practices for Spot Instances** — https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-best-practices.html502- **Spot Fleet** — https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet.html503- **Capacity Rebalance** — https://docs.aws.amazon.com/autoscaling/ec2/userguide/capacity-rebalance.html504- **Spot Instance interruption handling** — https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/handling-spot-interruptions.html505- **Spot Placement Score** — https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_GetSpotPlacementScores.html506- **EventBridge EC2 Spot Instance Interruption Warning** — https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-service-event.html#eb-spot-instance507- **Auto Scaling lifecycle hooks** — https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html508- **ELB target group deregistration delay** — https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html#deregistration-delay