【问题标题】:Getting an error with this lambda function that is triggered by the createbucket event使用由 createbucket 事件触发的此 lambda 函数出现错误
【发布时间】:2020-10-29 07:29:11
【问题描述】:

我设置了触发此自动策略生成脚本的 Cloudwatch 事件CreateBucket

import json

s3 = boto3.client('s3')

def lambda_handler(event, context):

    # Get bucket name from the S3 event
    bucket_name = event['Records'][0]['s3']['bucket']['name']


    # Create a bucket policy
    bucket_policy =json.dumps({
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "MustBeEncryptedAtRest",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:PutObject",
            "Resource": [
                "arn:aws:s3:::{bucket_name}",
                "arn:aws:s3:::{bucket_name}/*"
            ],
            "Condition": {
                "StringNotEquals": {
                    "s3:x-amz-server-side-encryption": [
         
                        "aws:kms"
                    ]
                }
            }
        },
        {
            "Sid": "MustBeEncryptedInTransit",
            "Effect": "Deny",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::{bucket_name}",
                "arn:aws:s3:::{bucket_name}/*"
            ],
            "Condition": {
                "Bool": {
                    "aws:SecureTransport": "false"
                    }
            }
        } ] })


    # Set the new policy
    s3.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)

此 lambda 函数应运行并将此策略放置在创建的存储桶中。但是,它运行不正常,通过 lambda 接口测试给我这个错误:

  "stackTrace": [
    [
      "/var/task/lambda_function.py",
      9,
      "lambda_handler",
      "bucket_name = event['Records'][0]['s3']['bucket']['name']"
    ]
  ],
  "errorType": "KeyError",
  "errorMessage": "'Records'"
}

我每次都需要将该策略附加到一个新存储桶,无论名称如何,但似乎无法弄清楚它为什么不起作用

编辑::

  File "/var/task/lambda_function.py", line 10, in lambda_handler
    bucket_name = event['details']['requestParameters']['bucketName']
KeyError: 'details'```

Is the new error i get.

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-cloudwatch amazon-cloudwatch-events


    【解决方案1】:

    您使用的结构不适合该事件,实际上 AWS 在其文档中包含 examples of events

    真实的事件看起来像下面的json

    {
        "version": "0",
        "id": "36eb8523-97d0-4518-b33d-ee3579ff19f0",
        "detail-type": "AWS API Call via CloudTrail",
        "source": "aws.s3",
        "account": "123456789012",
        "time": "2016-02-20T01:09:13Z",
        "region": "us-east-1",
        "resources": [],
        "detail": {
            "eventVersion": "1.03",
            "userIdentity": {
                "type": "Root",
                "principalId": "123456789012",
                "arn": "arn:aws:iam::123456789012:root",
                "accountId": "123456789012",
                "sessionContext": {
                    "attributes": {
                        "mfaAuthenticated": "false",
                        "creationDate": "2016-02-20T01:05:59Z"
                    }
                }
            },
            "eventTime": "2016-02-20T01:09:13Z",
            "eventSource": "s3.amazonaws.com",
            "eventName": "CreateBucket",
            "awsRegion": "us-east-1",
            "sourceIPAddress": "100.100.100.100",
            "userAgent": "[S3Console/0.4]",
            "requestParameters": {
                "bucketName": "bucket-test-iad"
            },
            "responseElements": null,
            "requestID": "9D767BCC3B4E7487",
            "eventID": "24ba271e-d595-4e66-a7fd-9c16cbf8abae",
            "eventType": "AwsApiCall"
        }
    }
    

    要解决错误,您的 Lambda 需要使用此结构来访问存储桶名称属性。

    为此,请更新您的代码以分配 bucket_name 变量,如下所示。

    bucket_name = event['detail']['requestParameters']['bucketName']
    

    以上事件应作为要测试的事件。

    工作函数如下

    import json
    import boto3
    
    s3 = boto3.client('s3')
    
    def lambda_handler(event, context):
        # Get bucket name from the S3 event
        print(event)
    
        bucket_name = event['detail']['requestParameters']['bucketName']
    
        # Create a bucket policy
        bucket_policy =json.dumps({
            "Version": "2012-10-17",
            "Statement": [
                {
                    "Sid": "MustBeEncryptedAtRest",
                    "Effect": "Deny",
                    "Principal": "*",
                    "Action": "s3:PutObject",
                    "Resource": [
                        "arn:aws:s3:::{}".format(bucket_name),
                        "arn:aws:s3:::{}/*".format(bucket_name)
                    ],
                    "Condition": {
                        "StringNotEquals": {
                            "s3:x-amz-server-side-encryption": [
                                "AES256",
                                "aws:kms"
                            ]
                        }
                    }
                },
                {
                    "Sid": "MustBeEncryptedInTransit",
                    "Effect": "Deny",
                    "Principal": "*",
                    "Action": "s3:*",
                    "Resource": [
                        "arn:aws:s3:::{}".format(bucket_name),
                        "arn:aws:s3:::{}/*".format(bucket_name)
                    ],
                    "Condition": {
                        "Bool": {
                            "aws:SecureTransport": "false"
                            }
                    }
                } ] })
    
    
        # Set the new policy
        s3.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy)
    

    【讨论】:

    • 是否需要编辑整个存储桶策略代码以匹配该结构?
    • 不,只是顶部的bucket name变量需要引用事件的结构:)
    • 策略没问题,是传入 Lambda 的事件不是您在 bucket_name 变量中引用的格式
    • 我把我得到的新错误。好像这个事件的东西不带参数
    • 抱歉我打错了应该是细节而不是细节
    猜你喜欢
    • 2016-06-24
    • 2021-07-05
    • 2014-05-23
    • 1970-01-01
    • 2018-05-05
    • 2016-07-17
    • 2019-10-18
    • 2015-08-22
    相关资源
    最近更新 更多