在boto3中使用RDS标签/如何在boto3

时间:2015-09-17 15:32:20

标签: python amazon-web-services rds boto3

我尝试使用python标记AWS中的各种对象。 AFAIK对于使用boto的某些服务是不可能的。因此,我决定看看boto3。我已经堆积在RDS上了。基于documentation,add_tags_to_resource方法需要资源ARN。我没有办法找到它。

为了解决上述问题,我考虑过自己创建ARN。毕竟它不是那么难 - RDS Tagging documentation。但还有另一个问题。在我的脚本中,我无法保证知道帐号,所以我想知道如何获得帐号以自行创建ARN。

4 个答案:

答案 0 :(得分:4)

  

我想知道如何获得帐号

不幸的是,找到它并不容易。但你可以有一些黑客攻击:

如果您可以访问某些API调用,则可以获取安全组或AMI并检查OwnerId。

>>> import boto3
>>> client = boto3.client('ec2')
>>> client.describe_security_groups()['SecurityGroups'][0]['OwnerId']
'1234567890'

只有在您可以保证SG或AMI是由您正在寻找的帐户创建时,此技巧才有效。

OR

使用对IAM进行API调用并解析您自己的角色或用户的ARN

>>> client = boto3.client('iam')
>>> client.get_user()['User']['Arn'].split(':')[4]

答案 1 :(得分:1)

将您的boto3升级到最新版本,它现在会返回一个新属性DBInstanceArn

http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_instances

答案 2 :(得分:0)

使用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']

答案 3 :(得分:0)

您可以从那里获得rds arn

rds_dict = self.rds_client.describe_db_instances()

如果您通过rds_dict进行迭代,则会得到一个名为DBInstanceArn的密钥

我为此使用boto3