在 SQS 中为无服务器启用静态加密

cod*_*ess 3 encryption amazon-sqs amazon-web-services aws-cloudformation

我们正在为我们的应用程序使用无服务器框架和微服务(lambda 函数)。在 serverless.yml 文件中,我们列出了部署时需要创建的资源。

serverles.yml 文件的资源部分如下所示:

resources:
    Resources:
        GatewayResponse:
            Type: "AWS::ApiGateway::GatewayResponse"
            Properties:
                ResponseParameters:
                    gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
                    gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
                ResponseType: EXPIRED_TOKEN
                RestApiId:
                    Ref: "ApiGatewayRestApi"
                StatusCode: "401"
        AuthFailureGatewayResponse:
            Type: "AWS::ApiGateway::GatewayResponse"
            Properties:
                ResponseParameters:
                    gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
                    gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
                ResponseType: UNAUTHORIZED
                RestApiId:
                    Ref: "ApiGatewayRestApi"
                StatusCode: "401"
        SqsQueue:
            Type: "AWS::SQS::Queue"
            Properties:
                QueueName: ${opt:stage}-${opt:product}-sqs-queue
                VisibilityTimeout: 900
                RedrivePolicy:
                    deadLetterTargetArn:
                        Fn::GetAtt:
                        - SqsDeadLetterQueue
                        - Arn
                    maxReceiveCount: 1
        SqsDeadLetterQueue:
            Type: AWS::SQS::Queue
            Properties:
                QueueName: ${opt:stage}-${opt:product}-deadletter-queue
                MessageRetentionPeriod: 1209600
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我们也在那里创建 SQS 队列资源。最初,我们没有在 SQS 中启用静态加密,但现在出现了这种需求。

我可以进入 AWS 控制台并从那里为我们创建的每个队列手动启用静态加密,但这会很乏味,而且我想将其包含在 serverless.yml 创建中,以便从现在开始创建的任何 SQS 资源都具有默认启用加密。

我想知道需要添加到 serverless.yml 的资源部分中。我是否添加 CMK(客户主密钥)别名,是否可以使用默认 CMK 别名,还是需要为此目的生成一个新别名。我是否还需要修改引用 SQS 的其他 lambda,以便他们能够访问它?

Mar*_*cin 5

要向队列添加加密,您必须将KmsMasterKeyId添加到模板中的队列中。如果您想使用 AWS 托管 CMK,则 id 将为alias/aws/sqs(假设两个队列):

resources:
    Resources:
        GatewayResponse:
            Type: "AWS::ApiGateway::GatewayResponse"
            Properties:
                ResponseParameters:
                    gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
                    gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
                ResponseType: EXPIRED_TOKEN
                RestApiId:
                    Ref: "ApiGatewayRestApi"
                StatusCode: "401"
        AuthFailureGatewayResponse:
            Type: "AWS::ApiGateway::GatewayResponse"
            Properties:
                ResponseParameters:
                    gatewayresponse.header.Access-Control-Allow-Origin: "'*'"
                    gatewayresponse.header.Access-Control-Allow-Headers: "'*'"
                ResponseType: UNAUTHORIZED
                RestApiId:
                    Ref: "ApiGatewayRestApi"
                StatusCode: "401"
        SqsQueue:
            Type: "AWS::SQS::Queue"
            Properties:
                QueueName: ${opt:stage}-${opt:product}-sqs-queue
                VisibilityTimeout: 900
                RedrivePolicy:
                    deadLetterTargetArn:
                        Fn::GetAtt:
                        - SqsDeadLetterQueue
                        - Arn
                    maxReceiveCount: 1
                KmsMasterKeyId: alias/aws/sqs
        SqsDeadLetterQueue:
            Type: AWS::SQS::Queue
            Properties:
                QueueName: ${opt:stage}-${opt:product}-deadletter-queue
                MessageRetentionPeriod: 1209600
                KmsMasterKeyId: alias/aws/sqs
Run Code Online (Sandbox Code Playgroud)