Package: aq
aqallows you to query your AWS resources (EC2 instances, S3 buckets, etc.) with plain SQL.But why?Fun, mostly fun. But see sample queries below for useful queries that can be performed withaq.UsageUsage:
    aq [options]
    aq [options] <query>

Options:
    --table-cache-ttl=<seconds>  number of seconds to cache the tables
                                 before we update them from AWS again [default: 300]
    -v, --verbose  enable verbose loggingRunningaqwithout specifying any query will start a REPL to run your queries interactively.Sample queriesOne of the most important benefit of being able to query which SQL is aggregation and join,
which can be very complicated or even impossible to do with AWS CLI.To count how many running instances per instance type> SELECT instance_type, count(*) count
  FROM ec2_instances
  WHERE state->'Name' = 'running'
  GROUP BY instance_type
  ORDER BY count DESC
+-----------------+---------+
| instance_type   |   count |
|-----------------+---------|
| m4.2xlarge      |      15 |
| m4.xlarge       |       6 |
| r3.8xlarge      |       6 |
+-----------------+---------+Find instances with largest attached EBS volumes size> SELECT i.id, i.tags->'Name' name, count(v.id) vols, sum(v.size) size, sum(v.iops) iops
  FROM ec2_instances i
  JOIN ec2_volumes v ON v.attachments -> 0 -> 'InstanceId' = i.id
  GROUP BY i.id
  ORDER BY size DESC
  LIMIT 3
+------------+-----------+--------+--------+--------+
| id         | name      |   vols |   size |   iops |
|------------+-----------+--------+--------+--------|
| i-12345678 | foo       |      4 |   2000 |   4500 |
| i-12345679 | bar       |      2 |    332 |   1000 |
| i-12345687 | blah      |      1 |    320 |    960 |
+------------+-----------+--------+--------+--------+Find instances that allows access to port 22 in their security groups> SELECT i.id, i.tags->'Name' name, sg.group_name
  FROM ec2_instances i
  JOIN ec2_security_groups sg ON instr(i.security_groups, sg.id)
  WHERE instr(sg.ip_permissions, '"ToPort": 22,')
+------------+-----------+---------------------+
| id         | name      | group_name          |
|------------+-----------+---------------------|
| i-foobar78 | foobar    | launch-wizard-1     |
| i-foobar87 | blah      | launch-wizard-2     |
+------------+-----------+---------------------+AWS Credentialaqrelies onboto3for AWS API access so all thecredential configuration mechanismsof boto3 will work. If you are using the AWS CLI then you can useaqwithout any further configurations.Available tablesAWS resources are specified as table names in<resource>_<collection>format with:resourceone of theresourcesdefined in boto3:ec2,s3,iam, etc.collectionone of the resource’scollectionsdefined in boto3:instances,images, etc.An optional schema (i.e. database) name can be used to specify the AWS region to query.
If you don’t specify the schema name then boto’s default region will be used.-- to count the number of ec2 instances in AWS Singapore region
SELECT count(*) FROM ap_southeast_1.ec2_instancesNote that the region name is specified using underscore (ap_southeast_1) instead of dash (ap-southeast-1).At the moment the full table list for AWSus_east_1region iscloudformation_stackscloudwatch_alarmscloudwatch_metricsdynamodb_tablesec2_classic_addressesec2_dhcp_options_setsec2_imagesec2_instancesec2_internet_gatewaysec2_key_pairsec2_network_aclsec2_network_interfacesec2_placement_groupsec2_route_tablesec2_security_groupsec2_snapshotsec2_subnetsec2_volumesec2_vpc_addressesec2_vpc_peering_connectionsec2_vpcsglacier_vaultsiam_groupsiam_instance_profilesiam_policiesiam_rolesiam_saml_providersiam_server_certificatesiam_usersiam_virtual_mfa_devicesopsworks_stackss3_bucketssns_platform_applicationssns_subscriptionssns_topicssqs_queuesQuery with structured valueQuite a number of resource contain structured value (e.g. instance tags) that cannot be use directly in SQL.
We keep and present these values as JSON serialized string and add a new operator->to make querying on them easier.
The->(replaced tojson_getbefore execution) can be used to access an object field,object->'fieldName', or access
an array item,array->index:> SELECT '{"foo": "bar"}' -> 'foo'
+-------------------------------------+
| json_get('{"foo": "bar"}', 'foo')   |
|-------------------------------------|
| bar                                 |
+-------------------------------------+
> SELECT '["foo", "bar", "blah"]' -> 1
+--------------+
| json_get('   |
|--------------|
| bar          |
+--------------+Installpip install aqTests (withnose)nosetests