AWS EC2 Instance Manager
Lists, starts, stops and manages AWS EC2 instances.
Published: May 11, 2024
Detailed Information
This script manages AWS EC2 instances. You can list, start, stop and restart instances.
What Does This Script Do?
This script provides EC2 instance management:
- Lists EC2 instances
- Starts instance
- Stops instance
- Restarts instance
- Shows instance status
Why Should You Use It?
EC2 instance management is critical for cloud infrastructure management:
- Automation: Automate instance management
- Cost Savings: Stop unused instances
- Control: Manage instances centrally
How to Use
Step-by-Step Usage Guide
1. AWS CLI Configuration
aws configure
2. List Instances
./ec2_manager.sh list
3. Start Instance
./ec2_manager.sh start i-1234567890abcdef0
4. Stop Instance
./ec2_manager.sh stop i-1234567890abcdef0 Requirements
Requirements
- AWS CLI: AWS command line tool
- AWS Credentials: AWS access keys
- EC2 Permissions: EC2 management permissions
Use Cases
Use Cases
1. Instance Management
Manage EC2 instances centrally.
2. Cost Optimization
Automatically stop unused instances.
Examples
Usage Examples
Example 1: List Instances
./ec2_manager.sh list
Example 2: Start Instance
./ec2_manager.sh start i-1234567890abcdef0 Code
#!/bin/bash
# AWS EC2 Instance Manager
ACTION="${1:-list}"
if ! command -v aws &> /dev/null; then
echo "Error: AWS CLI not installed"
exit 1
fi
case "$ACTION" in
list)
echo "======================================"
echo " EC2 INSTANCES"
echo "======================================"
aws ec2 describe-instances --query "Reservations[*].Instances[*].[InstanceId,State.Name,InstanceType,PublicIpAddress,PrivateIpAddress]" --output table
;;
start)
if [ -z "$2" ]; then
echo "Usage: $0 start <instance-id>"
exit 1
fi
echo "Starting instance: $2"
aws ec2 start-instances --instance-ids "$2"
;;
stop)
if [ -z "$2" ]; then
echo "Usage: $0 stop <instance-id>"
exit 1
fi
echo "Stopping instance: $2"
aws ec2 stop-instances --instance-ids "$2"
;;
restart)
if [ -z "$2" ]; then
echo "Usage: $0 restart <instance-id>"
exit 1
fi
echo "Restarting instance: $2"
aws ec2 reboot-instances --instance-ids "$2"
;;
*)
echo "Usage: $0 [list|start|stop|restart] [instance-id]"
exit 1
;;
esac
Usage
chmod +x ec2_manager.sh
./ec2_manager.sh list
./ec2_manager.sh start <instance-id>
./ec2_manager.sh stop <instance-id>
Troubleshooting
Troubleshooting
Problem: "Unable to locate credentials"
Solution: Configure AWS credentials:
aws configure