EC2 Instance Lifecycle

A Complete Guide to Amazon EC2 Instance States and Transitions

Primary Instance States

Pending (pending)

  • Initial state after launch request
  • Instance preparing to enter running state
  • Not billed during this state
  • Network interfaces being configured

Running (running)

  • Instance is running and ready for use
  • Billing is active
  • Can connect to the instance
  • Applications are accessible

Stopping (stopping)

  • Instance is preparing to stop or hibernate
  • Transitional state
  • No billing for instance usage
  • EBS volumes remain attached

Stopped (stopped)

  • Instance is shut down
  • No compute charges
  • EBS volumes are preserved
  • Can be restarted at any time

Special States

Hibernating (hibernating)

  • RAM contents saved to EBS
  • Instance state preserved
  • Faster startup than cold start
  • Only EBS storage charges apply

Shutting Down (shutting-down)

  • Instance preparing for termination
  • Cannot be stopped or recovered
  • Final state before termination
  • Data cleanup in progress

Terminated (terminated)

  • Instance permanently deleted
  • Cannot be restarted
  • All instance data lost
  • EBS volumes can be preserved if configured

State Transitions

Pending Running Stopping Stopped Terminated

State Transition Actions

Launch → Running

  • AMI selection
  • Instance type configuration
  • Network setup
  • Security group assignment

Running → Stopping

  • OS shutdown command
  • Application cleanup
  • Network disconnection
  • Billing stops

Stopped → Running

  • Instance start command
  • Network reattachment
  • Public IP reassignment
  • Billing resumes

Any State → Terminated

  • Instance deletion
  • EBS volume handling
  • Network cleanup
  • Final billing

Management Actions

Instance Control

# Start instance aws ec2 start-instances --instance-ids i-1234567890abcdef0 # Stop instance aws ec2 stop-instances --instance-ids i-1234567890abcdef0 # Reboot instance aws ec2 reboot-instances --instance-ids i-1234567890abcdef0

Instance Monitoring

# Get instance status aws ec2 describe-instance-status --instance-ids i-1234567890abcdef0 # Enable detailed monitoring aws ec2 monitor-instances --instance-ids i-1234567890abcdef0

State Change Events

CloudWatch Events

{
  "source": "aws.ec2",
  "detail-type": "EC2 Instance State-change Notification",
  "detail": {
    "instance-id": "i-1234567890abcdef0",
    "state": "running"
  }
}

Event Handling

  • Lambda function triggers
  • SNS notifications
  • CloudWatch alarms
  • Custom metrics

Lifecycle Management

  • Use instance scheduler for cost optimization
  • Implement proper shutdown procedures
  • Monitor state transitions
  • Automate routine operations
  • Use tags for lifecycle tracking

Data Management

  • Back up data before state changes
  • Use EBS snapshots regularly
  • Implement data persistence strategies
  • Plan for data recovery
  • Monitor storage performance

Cost Optimization

  • Stop instances when not needed
  • Use Auto Scaling groups
  • Monitor instance utilization
  • Right-size instances
  • Consider reserved instances