Skip to main content

Command Palette

Search for a command to run...

πŸš€ 5 Essential AWS CLI Scripts Every DevOps Engineer Should Know ☁️

DevOps Drop #12πŸ’»

Published
β€’4 min read
πŸš€ 5 Essential AWS CLI Scripts Every DevOps Engineer Should Know ☁️
G

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-instances or stop-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 text makes 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

  1. Use variables for AMI IDs, security groups, and regions.

  2. Use --query and --output text for clean, script-friendly outputs.

  3. Always use IAM roles or profiles instead of hardcoding credentials.

  4. Add sleep if your scripts depend on instance readiness.

  5. 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