我尝试使用 python 在 AWS 中标记各种对象。AFAIK 对于某些使用 boto 的服务是不可能的。因此,我决定看一下boto3。我已经堆积在RDS上。根据文档, add_tags_to_resource 方法需要一个资源 ARN。我没有办法得到它。
为了解决上述问题,我考虑过自己创建 ARN。毕竟这并不难 - RDS 标记文档。但是还有另一个问题。在我的脚本中,我不能保证知道帐号,所以我想知道如何获取帐号以自己创建 ARN。
我想知道我怎样才能得到帐号
不幸的是,它并不容易找到。但是你可以有一些技巧:
如果您有权访问某些 API 调用,则可以获取安全组或 AMI 并检查 OwnerId。
>>> import boto3
>>> client = boto3.client('ec2')
>>> client.describe_security_groups()['SecurityGroups'][0]['OwnerId']
'1234567890'
仅当您可以保证 SG 或 AMI 是由您正在查找的帐户创建时,此技巧才有效。
使用对 IAM 进行 API 调用并解析您自己的角色或用户的 ARN
>>> client = boto3.client('iam')
>>> client.get_user()['User']['Arn'].split(':')[4]
将您的 boto3 升级到最新版本,它现在返回一个新属性DBInstanceArn
http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_instances
使用 boto3,您可以使用此代码获取 db arn....
from boto3 import client
REGION = "us-east-1"
INSTANCES = ["db-name-01"]
rds = client("rds", region_name=REGION)
if INSTANCES:
for instance in INSTANCES:
instance_counts = {}
response = rds.describe_db_instances(DBInstanceIdentifier=instance)
dbins = response['DBInstances']
dbin = dbins[0]
dbarn = dbin['DBInstanceArn']
您可以从中获取 rds arn
rds_dict = self.rds_client.describe_db_instances()
如果您遍历,rds_dict
您将获得一个名为DBInstanceArn
我为此使用 boto3