是否可以使用 CFT 将 NotificationConfiguration 添加到现有的 S3 存储桶?

Is it possible to add NotificationConfiguration to an existing S3 buckets using CFT?

我还是 AWS 服务的新手。

我想使用 CloudFormation 模板 (CFT) 在现有 S3 存储桶上添加 Lambda 触发器。这可能吗?

以下 CFT 正在尝试创建新的 S3 存储桶并在其上添加事件通知。

    S3BUCKET_NOTIFCATION = Bucket(
        "S3Bucket",
        BucketName=s3_bucket("confidential", Ref(ENV)),
        NotificationConfiguration=NotificationConfiguration(
            LambdaConfigurations=[
                LambdaConfigurations(
                    Event="s3:ObjectCreated:*",
                    Filter=Filter(
                        S3Key=S3Key(
                            Rules=[Rules(Name="prefix", Value=Ref(inputKeyPrefix)),
                                   Rules(Name="suffix", Value=".json")]
                        )
                    ),
                    Function=Ref(cost_function)
                )
            ]
        )
    )

是否可以将 NotificationConfiguration 添加到现有存储桶?

我也无法将 NotificationConfiguration 添加到现有存储桶。当您尝试时,您会收到错误 CREATE_FAILED. Reason: S3_BUCKET already exists.

ServerFault question from 2013 详细说明不允许修改预先存在的存储桶。它似乎仍然是正确的。

正如其他人所指出的,由于各种原因,这仍然是不允许的。

选项 1)推荐

恕我直言,这里的理想方法是首先导入现有的云资源(在本例中为 S3),由 IaC/CloudFormation 管理。然后,导入资源后,您可以将 NotificationConfiguration 添加到 CloudFormation 模板。这种方法允许您管理现有的资源,这些资源最初可能是手动配置的,以便通过 IaC/Cfn 继续管理。

选项 2) 您要求的内容

如果您想通过 CloudFormation 将 NotificationConfiguration 添加到现有的 S3 存储桶,解决方法是使用

a Lambda-backed custom resource created... The custom resource triggers a Lambda function, which triggers the PutBucketNotification API to add a notification configuration to your S3 bucket.

详情here

注意:请考虑选项 2) 的限制。其中一些记录在引用的 link.