【问题标题】:How can I tag an AWS Lambda function using boto3如何使用 boto3 标记 AWS Lambda 函数
【发布时间】:2024-01-22 07:23:01
【问题描述】:

我有创建“lambda”类型的 boto3 客户端的代码。然后我使用该客户端调用 list_functions()、create_function() 和 update_function() 方法。如本文档中所述,一切正常: http://boto3.readthedocs.io/en/latest/reference/services/lambda.html#Lambda.Client.list_functions

但是当我去使用这里列出的 list_tags() 或 tag_resource() 方法时: http://boto3.readthedocs.io/en/latest/reference/services/lambda.html#Lambda.Client.list_tags

我收到一条错误消息:

AttributeError:“Lambda”对象没有属性“list_tags”

我做错了什么?这些方法列在同一个文档页面上,所以我认为它们是在同一个客户端上调用的。什么给了:

    l = boto3.client(
    'lambda',
    region_name='us-east-1', 
    aws_access_key_id = 'AletitgoQ',
    aws_secret_access_key = 'XvHowdyW',
)
    l.list_tags(
         Resource="myArn"
        )        

    l.tag_resource(
            Resource="myArn",
            Tags={
                'action': 'test'
          }
      )

更糟糕的是,我似乎无法在 create_function() 调用中包含标签,尽管文档在此处对此进行了说明: http://boto3.readthedocs.io/en/latest/reference/services/lambda.html#Lambda.Client.create_function

当我在调用中包含标签时,我得到了这个响应:

botocore.exceptions.ParamValidationError:参数验证失败: 输入中的未知参数:“Tags”,必须是以下之一:FunctionName、Runtime、Role、Handler、Code、Description、Timeout、MemorySize、Publish、VpcConfig、DeadLetterConfig、Environment、KMSKeyArn

将该列表与 boto3 文档中显示的内容进行比较,您会发现最后缺少一些内容,包括标签

我在 python 2.7 中并且 pip 确认我的 boto3 是 1.4.4

【问题讨论】:

    标签: aws-lambda boto3


    【解决方案1】:

    它对我来说很好用:

    >>> import boto3
    >>> client = boto3.client('lambda')
    
    >>> response=client.create_function(FunctionName='bar', Runtime='python2.7', Handler='index.handler', Tags={'Action': 'Test'}, Role='arn:aws:iam::123456789012:role/my-role', Code={'S3Bucket':'my-bucket', 'S3Key':'files.zip'})
    
    >>> client.tag_resource(Resource='arn:aws:lambda:ap-southeast-2:123456789012:function:bar', Tags={'Food':'Cheese'})
    {'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 204, 'RequestId': '93963c42-36d5-11e7-a457-8730520029b8', 'HTTPHeaders': {'date': 'Fri, 12 May 2017 05:40:58 GMT', 'x-amzn-requestid': '93963c42-36d5-11e7-a457-8730520029b8', 'connection': 'keep-alive', 'content-type': 'application/json'}}}
    
    >>> client.list_tags(Resource='arn:aws:lambda:ap-southeast-2:123456789012:function:bar')
    {'ResponseMetadata': {'RetryAttempts': 0, 'HTTPStatusCode': 200, 'RequestId': '9e826957-36d5-11e7-a554-a30d477976ba', 'HTTPHeaders': {'date': 'Fri, 12 May 2017 05:41:16 GMT', 'x-amzn-requestid': '9e826957-36d5-11e7-a554-a30d477976ba', 'content-length': '42', 'content-type': 'application/json', 'connection': 'keep-alive'}}, u'Tags': {u'Action': u'Test', u'Food': u'Cheese'}}
    

    【讨论】:

    • 这很奇怪。你能帮我一个忙,在 create 方法中添加一个虚假标签(如 taggg),看看你是否得到与我上面相同的错误消息?
    • 我的 boto3 是最新的,但不是我的 botocore。运行 pip install -U boto3 修复的东西