1

我正在尝试使用 Boto3 和 Python 获取 ElastiCache 标签。在 boto3 中有一个名为 list_tags_for_resource() 的函数。但是,我面临的问题是,如何找到资源名称。我正在使用以下代码:

from boto3.session import Session

sess = Session(aws_access_key_id=id,aws_secret_access_key=key)
conn = sess.client(service_name='elasticache', region_name='us-east-1')
arn="arn:aws:elasticache:us-east-1:123456:cluster:name_of_cluster"
print conn.list_tags_for_resource(ResourceName=name)

这给出了以下错误:

botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the ListTagsForResource operation: Unauthorized call. Please check the region or customer id
4

2 回答 2

3

如果您使用的是 ReadOnlyAccess 托管策略,您将无法列出 elasticache 的标签。确保您的用户已elasticache:ListTagsForResource在其策略中明确设置。亚马逊目前并未在其 ReadOnlyAccess 政策中包含该权限。允许查看 elasticache 标签的策略如下所示:

{
    "Version": "2015-06-26",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "elasticache:ListTagsForResource"
            ],
            "Resource": "*"
        }
    ]
}

我相信这应该是 ReadOnlyAccess 政策的一部分,并与亚马逊开了一张票。这是他们的回应:

您提出了关于“elasticache:ListTagsForResource”未包含在 AWS 提供的“ReadOnlyAccess”托管策略中的有效观点。在我看来,当它不允许某人在命名资源上列出标签时,我们不能很好地将其称为只读。因此,我已向控制这些托管策略的团队开具了内部票证;请求将 API“elasticache:ListTagsForResource”添加到“ReadOnlyAccess”。

于 2015-06-26T22:12:30.180 回答
0

那么发布的脚本有一些问题。您传递了未定义的变量“名称”。我认为您的意思是 arn,也许还有其他方法可以做到这一点,但我在会话中定义 region_name 而不是客户端。尝试这样的事情。

session = boto3.Session(region_name='us-east-1',aws_access_key_id=id,aws_secret_access_key=key)
client = session.client("elasticache")
arn = "arn:aws:elasticache:us-east-1:1234567889:cluster:rand57hzn577a78-0001-001"
client.list_tags_for_resource(ResourceName=arn)
于 2017-07-19T02:53:29.597 回答