AWSTemplateFormatVersion: "2010-09-09" Transform: AWS::Serverless-2016-10-31 Description: > Powertools for AWS Lambda (Python) data masking example Globals: # https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-specification-template-anatomy-globals.html Function: Timeout: 5 Runtime: python3.11 Tracing: Active Environment: Variables: POWERTOOLS_SERVICE_NAME: PowertoolsHelloWorld POWERTOOLS_LOG_LEVEL: INFO KMS_KEY_ARN: !GetAtt DataMaskingMasterKey.Arn # In production, we recommend you split up the encrypt and decrypt for fine-grained security. # For example, one function can act as the encryption proxy via HTTP requests, data pipeline, etc., # while only authorized personnel can call decrypt via a separate function. Resources: DataMaskingEncryptFunctionExample: Type: AWS::Serverless::Function Properties: Handler: data_masking_function_example.lambda_handler CodeUri: ../src Description: Data Masking encryption function # Cryptographic operations demand more CPU. CPU is proportionally allocated based on memory size. # We recommend allocating a minimum of 1024MB of memory. MemorySize: 1024 # DataMaskingDecryptFunctionExample: # Type: AWS::Serverless::Function # Properties: # Handler: data_masking_function_decrypt.lambda_handler # CodeUri: ../src # Description: Data Masking decryption function # MemorySize: 1024 # KMS Key DataMaskingMasterKey: Type: "AWS::KMS::Key" Properties: Description: KMS Key for encryption and decryption using Powertools for AWS Lambda Data masking feature # KMS Key support both IAM Resource Policies and Key Policies # For more details: https://docs.aws.amazon.com/kms/latest/developerguide/key-policies.html KeyPolicy: Version: "2012-10-17" Id: data-masking-enc-dec Statement: # For security reasons, ensure your KMS Key has at least one administrator. # In this example, the root account is granted administrator permissions. # However, we recommended configuring specific IAM Roles for enhanced security in production. - Effect: Allow Principal: AWS: !Sub "arn:aws:iam::${AWS::AccountId}:root" # (1)! Action: "kms:*" Resource: "*" # We must grant Lambda's IAM Role access to the KMS Key - Effect: Allow Principal: AWS: !GetAtt DataMaskingEncryptFunctionExampleRole.Arn # (2)! Action: - kms:Decrypt # to decrypt encrypted data key - kms:GenerateDataKey # to create an unique and random data key for encryption # Encrypt permission is required only when using multiple keys - kms:Encrypt # (3)! Resource: "*"