π 5 Essential AWS CLI Scripts Every DevOps Engineer Should Know βοΈ
DevOps Drop #12π»

About Dedicated Software Engineer with 3+ years of experience across industry having hands-on experience supporting, automating, troubleshooting and customer success management .Proven track record in optimizing network performance and ensuring uninterrupted services at Cisco. Skilled in Devsecops Python, and various technologies like AWS, Linux, and Kubernetes. Cloud Expertise: Hands-on experience with AWS (EC2, EKS, ECR, VPC, Route 53, S3, DynamoDB, CloudWatch, CloudTrail)
Successfully handled major incidents, demonstrating strong problem-solving skills and the ability to resolve issue.
Consistently met or exceeded service level agreement (SLA) commitments, leading to a customer satisfaction.
Played a pivotal role in root cause analysis and remediation efforts, resulting in a reduction in recurring issues and a significant improvement in overall system stability.
Automate routine tasks and workflows using Python, reduce manual effort and improve efficiency.
Monitored production clusters, leveraging Prometheus and Grafana to track performance, generate actionable alerts, and resolve issues proactively.
Demonstrated exceptional troubleshooting skills, identifying and addressing root causes of issues promptly, reduction in system downtime and minimal impact on operations.
If youβre a DevOps engineer or cloud enthusiast, automating AWS tasks is a must. The AWS CLI allows you to manage your infrastructure directly from the terminal β saving time β±οΈ, reducing errors β, and enabling repeatable workflows π.
Here are 5 essential AWS CLI scripts with explanations, emojis, and example outputs β perfect for real-world DevOps workflows.
1οΈβ£ Launch an EC2 Instance π₯οΈ
Quickly spin up servers for development, testing, or production:
#!/bin/bash
AMI="ami-09c813fb71547fc4f"
TYPE="t2.micro"
SG="sg-07c8acf3fa6b923fa"
KEY="my-key"
INSTANCE_ID=$(aws ec2 run-instances \
--image-id $AMI \
--instance-type $TYPE \
--security-group-ids $SG \
--key-name $KEY \
--tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=DevServer}]" \
--query "Instances[0].InstanceId" \
--output text)
echo "π Launched instance: $INSTANCE_ID"
π What This Script Does:
aws ec2 run-instancesβ Launches a new EC2 instance with the specified AMI, instance type, security group, and key pair.--tag-specificationsβ Tags the instance with a name (DevServer) for easy identification.--query "Instances[0].InstanceId"β Extracts only the Instance ID from the JSON output.--output textβ Formats output as plain text for use in scripts.π Example Output:
π Launched instance: i-0a1b2c3d4e5f6g7h8
β Use case: Automate provisioning of development or test servers.
2οΈβ£ List All Running Instances π
Check which instances are active in your AWS account:
#!/bin/bash
aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query "Reservations[*].Instances[*].{ID:InstanceId,Name:Tags[?Key=='Name']|[0].Value,IP:PrivateIpAddress}" \
--output table
π What This Script Does:
describe-instancesβ Lists all EC2 instances.--filters "Name=instance-state-name,Values=running"β Only shows instances that are currently running.--query ...β Extracts Instance ID, Name tag, and Private IP.--output tableβ Displays results in a clean table format.
π Example Output:
----------------------------------------------
| DescribeInstances |
+----------------------+------------+--------+
| ID | Name | IP |
+----------------------+------------+--------+
| i-0a1b2c3d4e5f6g7h8 | DevServer | 10.0.1.25 |
| i-0a2b3c4d5e6f7g8h9 | TestServer | 10.0.1.30 |
+----------------------+------------+--------+
β Use case: Quick monitoring without logging into the AWS console.
3οΈβ£ Start or Stop an Instance by Name β―οΈ
Control instance state with a single command:
#!/bin/bash
NAME=$1
INSTANCE_ID=$(aws ec2 describe-instances \
--filters "Name=tag:Name,Values=$NAME" \
--query "Reservations[0].Instances[0].InstanceId" \
--output text)
STATE=$(aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--query "Reservations[0].Instances[0].State.Name" \
--output text)
if [ "$STATE" == "running" ]; then
aws ec2 stop-instances --instance-ids $INSTANCE_ID
echo "π Stopped $NAME"
else
aws ec2 start-instances --instance-ids $INSTANCE_ID
echo "βΆοΈ Started $NAME"
fi
π What This Script Does:
Gets the Instance ID for the server with the specified name tag.
Checks the current state (running or stopped) of the instance.
Starts it if stopped, stops it if running using
start-instancesorstop-instances.
π Example Output:
βΆοΈ Started DevServer
π Stopped TestServer
β Use case: Save costs by automatically stopping dev servers after work hours.
4οΈβ£ Count Running EC2 Instances π’
Quickly check how many servers are active:
#!/bin/bash
COUNT=$(aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=running" \
--query "length(Reservations[].Instances[])" \
--output text)
echo "π Running EC2 instances: $COUNT"
π What This Script Does:
Counts all instances in the running state using
length()in the query.--output textmakes it easy to print or use in other scripts.
π Example Output:
π Running EC2 instances: 3
β Use case: Monitor infrastructure usage or include in automated reports.
5οΈβ£ Terminate All Stopped Instances ποΈ
Clean up unused resources and reduce costs:
#!/bin/bash
STOPPED=$(aws ec2 describe-instances \
--filters "Name=instance-state-name,Values=stopped" \
--query "Reservations[].Instances[].InstanceId" \
--output text)
if [ -n "$STOPPED" ]; then
aws ec2 terminate-instances --instance-ids $STOPPED
echo "π Terminated stopped instances: $STOPPED"
else
echo "β
No stopped instances to terminate."
fi
π What This Script Does:
describe-instancesβ Gets all stopped instances.terminate-instancesβ Permanently deletes them to free up resources.if [ -n "$STOPPED" ]β Checks if there are any stopped instances before trying to terminate.
π Example Output:
π Terminated stopped instances: i-0a2b3c4d5e6f7g8h9
β
No stopped instances to terminate.
β Use case: Prevent forgotten instances from accumulating costs in dev/test environments.
π‘ Why These Scripts Matter for DevOps
β‘ Automation: Reduce repetitive manual clicks.
π Monitoring: Quickly check infrastructure status.
πΈ Cost Control: Stop or terminate idle instances automatically.
π Repeatability: Ensure consistent environments for testing or production.
π Efficiency: Combine multiple scripts for CI/CD workflows.
π Pro Tips
Use variables for AMI IDs, security groups, and regions.
Use
--queryand--output textfor clean, script-friendly outputs.Always use IAM roles or profiles instead of hardcoding credentials.
Add
sleepif your scripts depend on instance readiness.Use tables and emojis in outputs for clarity and readability.
π‘ Automating AWS tasks with CLI scripts is a game-changer for DevOps teams. Start small, combine scripts, and soon youβll manage entire environments with just a few commands!
πΈ Thatβs a Wrap! Stay tuned for the next Learning Drop π§
Thanks for joining me on this little journey β¨.
Iβll keep sharing my notes, and learnings β one page at a time πβ.
π± Peek into my GitHub for more shell scripts
π Letβs be friends on LinkedIn
πCheck out my Google Drive Notes



