Skip to content

Instantly share code, notes, and snippets.

@jpeach
Created July 28, 2023 04:22
Show Gist options
  • Save jpeach/dc110f4f6d4c5197f92a3ffd0c123a4d to your computer and use it in GitHub Desktop.
Save jpeach/dc110f4f6d4c5197f92a3ffd0c123a4d to your computer and use it in GitHub Desktop.
Scripts to list/stop/start EC2 instances
#! /usr/bin/env bash
source "$(cd $(dirname $0) && pwd)/conf.sh"
for r in $REGIONS; do
aws ec2 describe-instances \
--profile sso --region $r --output json | \
jq '
.Reservations[] |
.Instances[] |
{
"id": .InstanceId,
"name": .Tags[] | select(.Key=="Name").Value,
"az": .Placement.AvailabilityZone,
"state": .State.Name,
"public_ip": .PublicIpAddress,
"public_dns": .PublicDnsName,
"private_ip": .PrivateIpAddress,
"private_dns": .PrivateDnsName,
}
'
done
#! /usr/bin/env bash
"$(cd $(dirname $0) && pwd)/instances.sh" | \
jq -r 'select(.state == "stopped") | [.id, .az] | @tsv' | \
awk '{
sub(/[a-z]$/, "", $2) # drop the last character of the AZ to make a region
cmd = sprintf("aws ec2 start-instances --profile sso --instance-id %s --region %s", $1, $2)
system(cmd)
}'
#! /usr/bin/env bash
"$(cd $(dirname $0) && pwd)/instances.sh" | \
jq -r 'select(.state == "running") | [.id, .az] | @tsv' | \
awk '{
sub(/[a-z]$/, "", $2) # drop the last character of the AZ to make a region
cmd = sprintf("aws ec2 stop-instances --profile sso --instance-id %s --region %s", $1, $2)
system(cmd)
}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment