Skip to content

Instantly share code, notes, and snippets.

@mahrous-amer
Created January 7, 2025 04:29
Show Gist options
  • Save mahrous-amer/d32b19304f1db604feaadcbb9465e348 to your computer and use it in GitHub Desktop.
Save mahrous-amer/d32b19304f1db604feaadcbb9465e348 to your computer and use it in GitHub Desktop.
Gather detailed information about various AWS resources in a specific region, using a specified AWS CLI profile.
#!/bin/bash
# Define variables
REGION="eu-west-1"
PROFILE="default"
OUTPUT_FORMAT="json"
# Helper function to fetch and print results
function fetch_and_print {
local description=$1
local command=$2
local query=$3
echo "=========================================="
echo "Fetching: $description"
echo "=========================================="
eval "$command --profile $PROFILE --region $REGION --query \"$query\" --output $OUTPUT_FORMAT"
echo ""
}
# Fetch EC2 instances
fetch_and_print "EC2 Instances" \
"aws ec2 describe-instances" \
"Reservations[*].Instances[*].[InstanceId, InstanceType, State.Name, Tags[?Key=='Name'].Value|[0], PrivateIpAddress, PublicIpAddress]"
# Fetch security groups
fetch_and_print "Security Groups" \
"aws ec2 describe-security-groups" \
"SecurityGroups[*].[GroupId, GroupName, Description, VpcId]"
# Fetch Elastic Load Balancers
fetch_and_print "Elastic Load Balancers" \
"aws elbv2 describe-load-balancers" \
"LoadBalancers[*].[LoadBalancerName, DNSName, Scheme, Type, State.Code, VpcId, AvailabilityZones[*].ZoneName | join(', ', @)]"
# Fetch target groups for load balancers
fetch_and_print "Target Groups for Load Balancers" \
"aws elbv2 describe-target-groups" \
"TargetGroups[*].[TargetGroupName, Protocol, Port, VpcId]"
# Fetch VPC details
fetch_and_print "VPC Details" \
"aws ec2 describe-vpcs" \
"Vpcs[*].[VpcId, CidrBlock, IsDefault, State]"
# Fetch subnets
fetch_and_print "Subnets Details" \
"aws ec2 describe-subnets" \
"Subnets[*].[SubnetId, VpcId, CidrBlock, AvailabilityZone, State]"
# Fetch Elastic IPs
fetch_and_print "Elastic IPs" \
"aws ec2 describe-addresses" \
"Addresses[*].[PublicIp, InstanceId, AllocationId, AssociationId, Domain]"
# Fetch all stacks and their resources
echo "=========================================="
echo "Fetching: CloudFormation Stacks and Resources"
echo "=========================================="
STACKS=$(aws --profile $PROFILE cloudformation list-stacks --region $REGION --query "StackSummaries[?StackStatus!='DELETE_COMPLETE'].StackName" --output text)
for STACK_NAME in $STACKS; do
fetch_and_print "Resources for Stack: $STACK_NAME" \
"aws cloudformation describe-stack-resources --stack-name $STACK_NAME" \
"StackResources[*].[LogicalResourceId, ResourceType, PhysicalResourceId]"
done
echo "Data collection complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment