MORE Cursor magic!
When managing AWS infrastructure, it’s often crucial to have a clear overview of all IP addresses and endpoints within your Virtual Private Cloud (VPC). With literally just one simple prompt, Cursor created a helpful bash script that provides a comprehensive view of all network-related resources in a VPC.
What Does the Script Do?
This script queries various AWS services and displays IP addresses, DNS names, and identifiers for:
- EC2 instances (both private and public IPs)
- RDS database endpoints
- Application Load Balancers
- NAT Gateways
- Elastic IPs
- Network Interfaces
- VPC Endpoints
Key Features
- Interactive VPC selection from a list of available VPCs
- AWS credential verification before execution
- Clean, tabulated output for easy reading
- Error handling with
set -e - Comprehensive coverage of network-related AWS resources
How to Use
- Save the script as
find-ips.sh - Make it executable:
chmod +x find-ips.sh - Ensure you have AWS CLI configured with appropriate credentials
- Run the script:
./find-ips.sh
Sample Output
The script provides a nicely formatted table for each resource type:
EC2 Instances:
----------------
+---------------+-------------+----------------+----------------+
| Name | Instance ID | Private IP | Public IP |
+---------------+-------------+----------------+----------------+
RDS Instances:
----------------
+------------------+-------------------------+-------+
| Instance ID | Endpoint | Port |
+------------------+-------------------------+-------+
Why Is This Useful?
This script is particularly helpful for:
- Auditing network resources
- Troubleshooting connectivity issues
- Documentation purposes
- Security reviews
- Migration planning
Technical Notes
The script leverages the AWS CLI’s powerful --query parameter with JMESPath expressions to filter and format the output. It uses the --output table option for clean, readable results.
Feel free to modify the script to add more resources or customize the output format according to your needs!
Here is the code.
Save this code as “find-ips.sh”, then run “chmod +x find-ips.sh” to give the script executable permissions, and then simply type ./find-ips.sh to run the script. Before running the script, make sure to log into whichever account you want to run using “aws configure”.
#!/bin/bash
# Enable error handling
set -e
# Verify AWS credentials
echo "Verifying AWS credentials..."
aws sts get-caller-identity > /dev/null
# Get VPC ID
echo "Available VPCs:"
aws ec2 describe-vpcs --query 'Vpcs[].[VpcId,Tags[?Key==`Name`].Value|[0]]' --output table
echo -e "\nEnter VPC ID from above list:"
read VPC_ID
echo "Finding IP addresses in VPC: ${VPC_ID}"
echo "=================================="
# EC2 Instances
echo -e "\nEC2 Instances:"
echo "----------------"
aws ec2 describe-instances \
--filters "Name=vpc-id,Values=${VPC_ID}" \
--query 'Reservations[].Instances[].[Tags[?Key==`Name`].Value | [0], InstanceId, PrivateIpAddress, PublicIpAddress]' \
--output table
# RDS Instances
echo -e "\nRDS Instances:"
echo "----------------"
aws rds describe-db-instances \
--query 'DBInstances[?DBSubnetGroup.VpcId==`'${VPC_ID}'`].[DBInstanceIdentifier, Endpoint.Address, Endpoint.Port]' \
--output table
# Load Balancers
echo -e "\nApplication Load Balancers:"
echo "----------------------------"
aws elbv2 describe-load-balancers \
--query 'LoadBalancers[?VpcId==`'${VPC_ID}'`].[LoadBalancerName, DNSName]' \
--output table
# NAT Gateways
echo -e "\nNAT Gateways:"
echo "--------------"
aws ec2 describe-nat-gateways \
--filter "Name=vpc-id,Values=${VPC_ID}" \
--query 'NatGateways[].[NatGatewayId, PublicIp, PrivateIp]' \
--output table
# Elastic IPs
echo -e "\nElastic IPs:"
echo "-------------"
aws ec2 describe-addresses \
--query 'Addresses[].[PublicIp, PrivateIpAddress, InstanceId, NetworkInterfaceId]' \
--output table
# Network Interfaces
echo -e "\nNetwork Interfaces:"
echo "-------------------"
aws ec2 describe-network-interfaces \
--filters "Name=vpc-id,Values=${VPC_ID}" \
--query 'NetworkInterfaces[].[NetworkInterfaceId, PrivateIpAddress, Description]' \
--output table
# VPC Endpoints
echo -e "\nVPC Endpoints:"
echo "--------------"
aws ec2 describe-vpc-endpoints \
--filters "Name=vpc-id,Values=${VPC_ID}" \
--query 'VpcEndpoints[].[VpcEndpointId, ServiceName, DnsEntries[0].DnsName]' \
--output table
echo -e "\nScript completed successfully!"
Leave a comment