I'm learning more about AWS SAM and looking at this template that is the codebase for a featured DZone article describing how to use AWS SAM to wire up Lambdas that encrypt data in DynamoDB tables. In that template the author defines a KMS Key:
KmsKey:
Type: AWS::KMS::Key
Properties:
Description: CMK for encrypting and decrypting
KeyPolicy:
Version: '2012-10-17'
Id: key-default-1
Statement:
- Sid: Enable IAM User Permissions
Effect: Allow
Principal:
AWS: !Sub arn:aws:iam::${AWS::AccountId}:root
Action: kms:*
Resource: '*'
- Sid: Allow administration of the key
Effect: Allow
Principal:
AWS: !Sub arn:aws:iam::${AWS::AccountId}:user/${KeyAdmin}
Action:
- kms:Create*
- kms:Describe*
- kms:Enable*
- kms:List*
- kms:Put*
- kms:Update*
- kms:Revoke*
- kms:Disable*
- kms:Get*
- kms:Delete*
- kms:ScheduleKeyDeletion
- kms:CancelKeyDeletion
Resource: '*'
- Sid: Allow use of the key
Effect: Allow
Principal:
AWS: !Sub arn:aws:iam::${AWS::AccountId}:user/${KeyUser}
Action:
- kms:DescribeKey
- kms:Encrypt
- kms:Decrypt
- kms:ReEncrypt*
- kms:GenerateDataKey
- kms:GenerateDataKeyWithoutPlaintext
Resource: '*'
And later on, they define 2 Lambdas that will use that key. One of those Lambdas is defined as:
SignupFunction:
Type: AWS::Serverless::Function
Properties:
Environment:
Variables:
userTable: !Ref myDynamoDBTable
keyid: !Ref KmsKey
CodeUri: Lambda/
Handler: signup.lambda_handler
Runtime: python3.8
Policies:
- DynamoDBCrudPolicy:
TableName: !Ref myDynamoDBTable
- KMSEncryptPolicy:
KeyId: !Ref KmsKey
- KMSDecryptPolicy:
KeyId: !Ref KmsKey
Events:
getCounter:
Type: Api
Properties:
Path: /signup
Method: POST
RestApiId: !Ref ApiGatewaySignupApi
So here the author specifies that a parameter of KeyUser
(you provide this as an input parameter to the template at deploy-time) is allowed to use the key:
AWS: !Sub arn:aws:iam::${AWS::AccountId}:user/${KeyUser}
But nowhere do I see the connection between Lambda and the KeyUser
. If the Lambda is expected to use the key, I would think that somewhere we need to say "create this Lambda and give it KeyUser
permission/role." But I'm not seeing that anywhere.
So I ask: how and where are the Lambdas endowed with KeyUser
's privileges, thus giving them permission to use the KMS Key?