KMS_MANAGED 加密 SQS 队列(在 CDK 中)的 S3 事件通知不起作用

S3 Event notification to KMS_MANAGED encrypted SQS queue (in CDK) not working

我有一个使用 CDK 开发的架构,带有一个 S3 存储桶和一个事件通知,它会针对每个上传到 S3 的文件向 SQS 发送消息。

它工作正常。

现在我正在尝试激活加密,我有以下信息:

所以我假设缺少某些权限,但我不知道如何修复它。

我是否需要向 SQS 添加缺少的权限才能从 S3 读取?或者允许 S3 将消息发送到加密的 SQS?

TL;DR S3 通知不适用于 sqs.QueueEncryption.KMS_MANAGED。使用客户管理的密钥加密队列。

AWS Knowledge Base: Why aren’t Amazon S3 event notifications delivered to an Amazon SQS queue that uses server-side encryption?:

The default AWS managed KMS key can't be modified. You must use a customer managed key ... and add permissions to the KMS key to allow access to a specified service principal.

这是一个最小的工作示例:

// S3 Notifications to a Encrypted Queue
export class S3SqsStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: cdk.StackProps) {
    super(scope, id, props);

    const bucket = new s3.Bucket(this, 'MyBucket', {
      encryption: s3.BucketEncryption.S3_MANAGED,
    });

    // https://aws.amazon.com/premiumsupport/knowledge-center/sqs-s3-event-notification-sse/
    const key = new kms.Key(this, 'MyCustomerKey', {
      policy: new iam.PolicyDocument({
        statements: [
          new iam.PolicyStatement({
            actions: ['kms:GenerateDataKey', 'kms:Decrypt'],
            resources: ['*'], // avoid circularity by not limiting the resource
            principals: [new iam.ServicePrincipal('s3.amazonaws.com')],
          }),
        ],
      }),
    });

    const queue = new sqs.Queue(this, 'MyQueue', {
      encryption: sqs.QueueEncryption.KMS,
      encryptionMasterKey: key,
    });

    bucket.addEventNotification(s3.EventType.OBJECT_CREATED, new s3n.SqsDestination(queue));
  }
}