[英]Work with RDS Tags in boto3 / How to get ARN in boto3
I try to tag various objects in AWS using python.我尝试使用 python 在 AWS 中标记各种对象。 AFAIK it's not possible for some services using boto. AFAIK 某些服务无法使用 boto。 Therefore, I decided to take a look at boto3.因此,我决定看看boto3。 I've got stacked on the RDS.我已经堆积在 RDS 上了。 Based on the documentation the add_tags_to_resource method needs a resource ARN.根据文档,add_tags_to_resource 方法需要一个资源 ARN。 I don't see a way to get it.我看不到获得它的方法。
To address above problem I thought about creating ARN on my own.为了解决上述问题,我想到了自己创建 ARN。 After all it's not so hard - RDS Tagging documentation .毕竟这不是那么难 - RDS 标记文档。 But there is another problem.但还有一个问题。 In my script I cannot guarantee to know account number, so I wonder how can I get account number to create ARN on my own.在我的脚本中,我不能保证知道帐号,所以我想知道如何获取帐号以自己创建 ARN。
I wonder how can I get account number我想知道如何获得帐号
Unfortunately it's not that easy to find.不幸的是,这并不容易找到。 But you can have some hacks:但是你可以有一些技巧:
If you have access to certain API calls, you can get security group or AMI and check for an OwnerId.如果您有权访问某些 API 调用,则可以获取安全组或 AMI 并检查 OwnerId。
>>> import boto3
>>> client = boto3.client('ec2')
>>> client.describe_security_groups()['SecurityGroups'][0]['OwnerId']
'1234567890'
This trick will only work if you can guarantee that SG or AMI was created by the account that you are looking for.只有当您可以保证 SG 或 AMI 是由您要查找的帐户创建时,此技巧才有效。
Use make an API call to the IAM and parse ARN of your own Role or User使用对 IAM 的 API 调用并解析您自己的角色或用户的 ARN
>>> client = boto3.client('iam')
>>> client.get_user()['User']['Arn'].split(':')[4]
upgrade your boto3 to the latest version, it now returns a new attribute DBInstanceArn
将您的 boto3 升级到最新版本,它现在返回一个新属性DBInstanceArn
http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_instances http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_instances
Using boto3 you can get the db arn with this code....使用 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']
You can get the rds arn from你可以从
rds_dict = self.rds_client.describe_db_instances()
if you iterate through rds_dict
you will get a key named DBInstanceArn
如果您遍历rds_dict
您将获得一个名为DBInstanceArn
的键
i am using boto3 for this我为此使用 boto3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.