Query AWS VPC security groups like SQL.
sgq is a two-step workflow tool for analyzing AWS VPC security groups:
- Refresh: Downloads all security groups from your AWS VPCs and saves them as CSV files (one per VPC). Records are sorted by security group ID in ascending order for consistent output.
- Query: Use SQL syntax to search across the downloaded security group data
This approach allows you to run complex queries against your security groups without repeatedly hitting AWS APIs.
It also provides a consistent inventory of your security groups for easy comparison over time. The stable sorting order makes the CSV files compatible with diff and other comparison tools.
Build the Docker image:
docker build -t chrisx86/sgq .If you prefer to run without Docker:
# Install uv if you don't have it
pipx install uv
# Install dependencies
uv sync
# Run sgq
uv run sgq.py <command>-
AWS Credentials: Configure AWS credentials and default region on your host using awscli:
aws configureYou can also use AWS SSO login.
-
VPC Name Tags: All VPCs you wish to examine must have
Nametags set. The VPC name becomes the table name in SQL queries. -
Docker or Podman: Recommended for the easiest setup.
-
For non-Docker usage:
docker run \
--rm \
-v $HOME/.aws:/sgq/.aws:Z,ro \
-v $(pwd)/csvs:/sgq/csvs:Z \
-e AWS_PROFILE=default \
chrisx86/sgq \
refreshOptions:
-e AWS_PROFILE=<profile>: Specify AWS profile (default or SSO profile name)- You may also specify desired regions in your AWS profile.
This creates CSV files in ./csvs/ named after your VPC Name tags (e.g., production, staging, development).
docker run \
--rm \
-v $(pwd)/csvs:/sgq/csvs:Z \
chrisx86/sgq \
query 'SELECT * FROM production WHERE rules_grants_cidr_ip = "203.0.113.0/24"'The table name in your SQL query corresponds to the VPC's Name tag. Each CSV file becomes a queryable table.
docker run --rm -v $(pwd)/csvs:/sgq/csvs:Z chrisx86/sgq \
query 'SELECT * FROM production WHERE rules_grants_cidr_ip = "203.0.113.0/24"'docker run --rm -v $(pwd)/csvs:/sgq/csvs:Z chrisx86/sgq \
query 'SELECT * FROM production WHERE rules_to_port = "22" AND rules_grants_cidr_ip = "0.0.0.0/0"'docker run --rm -v $(pwd)/csvs:/sgq/csvs:Z chrisx86/sgq \
query 'SELECT security_group_id, security_group_name FROM production'docker run --rm -v $(pwd)/csvs:/sgq/csvs:Z chrisx86/sgq \
query 'SELECT * FROM staging WHERE security_group_name LIKE "%web%"'docker run --rm -v $(pwd)/csvs:/sgq/csvs:Z chrisx86/sgq \
query 'SELECT * FROM production UNION SELECT * FROM staging'The security group CSV files contain the following columns (from ec2-security-groups-dumper):
security_group_idsecurity_group_namesecurity_group_descriptionvpc_idrules_direction(ingress/egress)rules_ip_protocolrules_from_portrules_to_portrules_grants_cidr_iprules_grants_security_group_id- And more...
This project uses:
- boto3 - AWS SDK for Python
- ec2-security-groups-dumper - Exports security groups to CSV
- q - SQL engine for CSV files
- uv - Fast Python package manager
This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.