用sql查询aws资源

aq的Python项目详细描述


aq允许您使用普通sql查询aws资源(ec2实例、s3存储桶等)。

https://travis-ci.org/lebinh/aq.svg?branch=masterhttps://asciinema.org/a/79468.png

但是为什么?

很有趣,大部分都很有趣。但是,有关可以使用aq执行的有用查询,请参见下面的示例查询。

用法

Usage:
    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 logging

运行aq而不指定任何查询将启动repl以交互方式运行查询。

示例查询

能够查询哪个sql是聚合和连接的最重要的好处之一, 这可能非常复杂,甚至不可能与aws cli一起使用。

计算每个实例类型的运行实例数

> 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 |
+-----------------+---------+

查找具有最大附加ebs卷大小

的实例
> 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 |
+------------+-----------+--------+--------+--------+

查找允许在其安全组中访问端口22的实例

> 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认证

aq依赖于boto3进行aws api访问,因此 credential configuration mechanisms BOTO3会起作用的。如果您使用的是aws cli,则可以使用aq,而无需任何进一步配置。

可用表格

aws资源以<resource>_<collection>格式指定为表名,格式为:

resource
one of the resources defined in boto3: ^{tt8}$, ^{tt9}$, ^{tt10}$, etc.
collection
one of the resource’s collections defined in boto3: ^{tt11}$, ^{tt12}$, etc.

可选的模式(即数据库)名称可用于指定要查询的aws区域。 如果不指定架构名称,则将使用boto的默认区域。

-- to count the number of ec2 instances in AWS Singapore region
SELECT count(*) FROM ap_southeast_1.ec2_instances

请注意,区域名是使用下划线(ap_southeast_1)而不是破折号(ap-southeast-1)指定的。

目前awsus_east_1region的完整表列表是

cloudformation_stacks
cloudwatch_alarms
cloudwatch_metrics
dynamodb_tables
ec2_classic_addresses
ec2_dhcp_options_sets
ec2_images
ec2_instances
ec2_internet_gateways
ec2_key_pairs
ec2_network_acls
ec2_network_interfaces
ec2_placement_groups
ec2_route_tables
ec2_security_groups
ec2_snapshots
ec2_subnets
ec2_volumes
ec2_vpc_addresses
ec2_vpc_peering_connections
ec2_vpcs
glacier_vaults
iam_groups
iam_instance_profiles
iam_policies
iam_roles
iam_saml_providers
iam_server_certificates
iam_users
iam_virtual_mfa_devices
opsworks_stacks
s3_buckets
sns_platform_applications
sns_subscriptions
sns_topics
sqs_queues

使用结构化值查询

相当多的资源包含不能在sql中直接使用的结构化值(例如实例标记)。 我们将这些值保存并呈现为json序列化字符串,并添加一个新的运算符->,以便更轻松地查询它们。 ->(在执行前替换为json_get)可用于访问对象字段object->'fieldName',或访问 数组项,array->index

> SELECT '{"foo": "bar"}' -> 'foo'
+-------------------------------------+
| json_get('{"foo": "bar"}', 'foo')   |
|-------------------------------------|
| bar                                 |
+-------------------------------------+
> SELECT '["foo", "bar", "blah"]' -> 1
+--------------+
| json_get('   |
|--------------|
| bar          |
+--------------+

安装

pip install aq

测试(使用nose

nosetests

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java ZK我们可以保存Windows组件状态吗?   java中的xpath比较路径   将字符串解析为长字符串时出现java数字格式异常   Apache CXF中获取异常中的服务器stacktrace的java问题   java我正在用j2me编程,我需要用丰富的格式编写文本以获得帮助屏幕   Android Room数据库中id字段的java名称字段约定   对于API9,java ArrayList<HashMap<String,String>>无法正确转换为JSONArray   api是最初的Java理想死了吗?   opencv java中的python掩码图像   java为什么在实现克隆方法时返回super。克隆()不是此克隆()   java HttpUrlConnection是否存在并发惩罚?   有没有办法将java arraylist对象复制到codemodel生成的源代码中?