AWS CDK-如何将事件通知添加到现有的S3存储桶

问题描述 投票:2回答:1

我正在尝试修改this AWS提供的CDK示例,以改用现有的存储桶。其他documentation表示支持导入现有资源。到目前为止,我无法使用CDK向现有存储桶添加事件通知。

这是示例的修改后的版本:

class S3TriggerStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # create lambda function
        function = _lambda.Function(self, "lambda_function",
                                    runtime=_lambda.Runtime.PYTHON_3_7,
                                    handler="lambda-handler.main",
                                    code=_lambda.Code.asset("./lambda"))

        # **MODIFIED TO GET EXISTING BUCKET**
        #s3 = _s3.Bucket(self, "s3bucket")
        s3 = _s3.Bucket.from_bucket_arn(self, 's3_bucket',
            bucket_arn='arn:<my_region>:::<my_bucket>')

        # create s3 notification for lambda function
        notification = aws_s3_notifications.LambdaDestination(function)

        # assign notification for the s3 event type (ex: OBJECT_CREATED)
        s3.add_event_notification(_s3.EventType.OBJECT_CREATED, notification)

[尝试执行add_event_notification时,导致以下错误:

AttributeError: '_IBucketProxy' object has no attribute 'add_event_notification'

from_bucket_arn函数返回一个IBucket,而add_event_notification函数是Bucket类的方法,但是我似乎找不到其他方法可以做到这一点。也许不支持。任何帮助,将不胜感激。

amazon-web-services aws-cdk
1个回答
0
投票

我设法使它与custom resource一起使用。它是TypeScript,但应轻松转换为Python:

const uploadBucket = new s3.Bucket(this, 'UploadBucket', {
    bucketName: 'upload-bucket'
};

const fn = new lambda.Function(this, 'MyFunction', {
    runtime: lambda.Runtime.NODEJS_10_X,
    handler: 'index.handler',
    code: lambda.Code.fromAsset(path.join(__dirname, 'lambda-handler'))
});

const rsrc = new AwsCustomResource(this, 'S3NotificationResource', {
    onCreate: {
        service: 'S3',
        action: 'putBucketNotificationConfiguration',
        parameters: {
            // This bucket must be in the same region you are deploying to
            Bucket: uploadBucket.bucketName,
            NotificationConfiguration: {
                LambdaFunctionConfigurations: [
                    {
                        Events: ['s3:ObjectCreated:*'],
                        LambdaFunctionArn: fn.functionArn,
                        Filter: {
                            Key: {
                                FilterRules: [
                                    {
                                        Name: 'suffix',
                                        Value: 'csv'
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        },
        // Always update physical ID so function gets executed
        physicalResourceId: 'S3NotifCustomResource' + Date.now().toString()
    }
});

fn.addPermission('AllowS3Invocation', {
    action: 'lambda:InvokeFunction',
    principal: new iam.ServicePrincipal('s3.amazonaws.com'),
    sourceArn: uploadBucket.bucketArn
});

rsrc.node.addDependency(fn.permissionsNode.findChild('AllowS3Invocation'));

这基本上是this example中列出的CloudFormation模板的CDK版本。有关可能的AWS SDK参数,请参见NotificationConfiguration上的文档。

© www.soinside.com 2019 - 2024. All rights reserved.