Writing Example Queries
Sample Guidelines
To help you get started on creating useful example queries, we've compiled a list of potential topics that can be used as guidelines and includes some basic and advanced examples of these from existing tables.
Please note though that the topics below are just suggestions and example queries are not limited to just these topics.
- Security
- Access policies
- Credential expiration and rotation
- Encryption
- Versioning
- Operations
- Audit logging
- Data retention and backups
- Tagging
- Cost management
- Capacity optimization
- Underutilized resources
Basic Examples
aws_s3_bucket
### Basic info```sqlselectname,regionfromaws_s3_bucket;```### List buckets which do not have default encryption enabled```sqlselectname,server_side_encryption_configurationfromaws_s3_bucketwhereserver_side_encryption_configuration is null;```### List buckets that are missing required tags```sqlselectname,tagsfromaws_s3_bucketwheretags -> 'owner' is nullor tags -> 'app_id' is null;```
aws_ebs_volume
### Basic info```sqlselectvolume_id,volume_type,encrypted,regionfromaws_ebs_volume;```### List unencrypted volumes```sqlselectvolume_id,encryptedfromaws_ebs_volumewherenot encrypted;```### Count the number of volumes by volume type```sqlselectvolume_type,count(volume_type) as countfromaws_ebs_volumegroup byvolume_type;```### List unattached volumes```sqlselectvolume_id,volume_typefromaws_ebs_volumewhereattachments is null;```
Advanced Examples
Joining information from the AWS EC2 instance and volume tables
### List unencrypted volumes attached to each instance```sqlselecti.instance_id,vols -> 'Ebs' ->> 'VolumeId' as vol_id,vol.encryptedfromaws_ec2_instance as icross joinjsonb_array_elements(block_device_mappings) as volsjoinaws_ebs_volume as volon vol.volume_id = vols -> 'Ebs' ->> 'VolumeId'wherenot vol.encrypted;```
Joining information from the Azure Compute virtual machine and network security group tables
### Get network security group rules for all security groups attached to a virtual machine```sqlselectvm.name,nsg.name,jsonb_pretty(security_rules)fromazure.azure_compute_virtual_machine as vm,jsonb_array_elements(vm.network_interfaces) as vm_nic,azure_network_security_group as nsg,jsonb_array_elements(nsg.network_interfaces) as nsg_intwherelower(vm_nic ->> 'id') = lower(nsg_int ->> 'id')and vm.name = 'warehouse-01';```
Querying complex jsonb columns for AWS S3 buckets
### List buckets that enforce encryption in transit```sqlselectname,p as principal,a as action,s ->> 'Effect' as effect,s ->> 'Condition' as conditions,sslfromaws_s3_bucket,jsonb_array_elements(policy_std -> 'Statement') as s,jsonb_array_elements_text(s -> 'Principal' -> 'AWS') as p,jsonb_array_elements_text(s -> 'Action') as a,jsonb_array_elements_text( s -> 'Condition' -> 'Bool' -> 'aws:securetransport' ) as sslwherep = '*'and s ->> 'Effect' = 'Deny'and ssl :: bool = false;```