AWS Lambda (sdk) KMSAccessDeniedException
The Lambda function does not have permission to access the specified AWS KMS key.
Debug aws-lambda-sdk automatically with DrDroid AI →
Connect your tools and ask AI to solve it for you
Understanding AWS Lambda and Its Purpose
AWS Lambda is a serverless compute service that allows you to run code without provisioning or managing servers. It automatically scales your applications by running code in response to triggers such as changes in data, shifts in system state, or user actions. Lambda supports a variety of programming languages and is commonly used for building event-driven architectures, automating tasks, and integrating with other AWS services.
Identifying the Symptom: KMSAccessDeniedException
When working with AWS Lambda, you might encounter the KMSAccessDeniedException. This error typically manifests when your Lambda function attempts to access an AWS Key Management Service (KMS) key but lacks the necessary permissions. As a result, the function fails to execute as expected, and you may see error messages in the AWS Lambda console or logs.
Exploring the Issue: What is KMSAccessDeniedException?
The KMSAccessDeniedException is an error that occurs when a Lambda function does not have the appropriate permissions to access a specified KMS key. AWS KMS is a managed service that makes it easy to create and control the encryption keys used to encrypt your data. If your Lambda function needs to encrypt or decrypt data using a KMS key, it must have the necessary permissions granted through its execution role.
Common Scenarios Leading to KMSAccessDeniedException
- The Lambda execution role lacks the
kms:Decryptorkms:Encryptpermissions. - The KMS key policy does not allow access to the Lambda execution role.
- The Lambda function is attempting to use a KMS key in a different AWS account without proper cross-account permissions.
Steps to Resolve KMSAccessDeniedException
To resolve the KMSAccessDeniedException, follow these steps to ensure that your Lambda function has the correct permissions:
Step 1: Update the Lambda Execution Role
Ensure that the IAM role associated with your Lambda function has the necessary permissions to access the KMS key. You can do this by attaching a policy to the role. Here is an example policy that grants access to a specific KMS key:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "kms:Decrypt", "kms:Encrypt" ], "Resource": "arn:aws:kms:region:account-id:key/key-id" } ]}
Replace region, account-id, and key-id with your specific values.
Step 2: Modify the KMS Key Policy
Ensure that the KMS key policy allows the Lambda execution role to use the key. You can add a statement to the key policy like this:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "arn:aws:iam::account-id:role/lambda-role-name" }, "Action": "kms:Decrypt", "Resource": "*" } ]}
Ensure the Principal matches your Lambda execution role.
Step 3: Verify Cross-Account Permissions
If your Lambda function is accessing a KMS key in a different AWS account, ensure that the key policy allows cross-account access. You may need to add a statement to the key policy to allow the external account's role to use the key.
Additional Resources
For more information on AWS Lambda and KMS permissions, consider visiting the following resources:
Frequently asked questions
What causes KMSAccessDeniedException? +
The Lambda function does not have permission to access the specified AWS KMS key.
Still debugging? Let DrDroid AI investigate for you →
Connect your tools and debug with AI
Get root cause analysis in minutes
- Connect your existing monitoring tools
- Ask AI to debug issues automatically
- Get root cause analysis in minutes