SAM Doctor

ERROR REFERENCE

Lambda could not use the KMS key for its environment variables

The error arrives wrapped in a Lambda InvalidParameterValueException, so it reads like a bad template value. It usually is not. The diagnosis is the KMS Exception: buried inside it.

WHAT IT MEANS

What this error means

When a function sets KmsKeyArn, Lambda encrypts its environment variables with that customer-managed key and needs a grant on it at deploy time. If anything about the key blocks that, the whole create or update fails:

CREATE_FAILED  AWS::Lambda::Function  Worker  Lambda was unable to configure
access to your environment variables because the KMS key is invalid for
CreateGrant. Please check your KMS key settings. KMS Exception:
InvalidArnException (Service: Lambda, Error Code: InvalidParameterValueException)

Read the exception name after KMS Exception: before changing anything. It splits three causes that look identical in the wrapper and need completely different fixes:

The message The ciphertext refers to a customer master key that does not exist, does not exist in this region, or you are not allowed to access is the same family — and note it deliberately does not tell you which of the three it was, so the checks below are the way to find out.

FIX

How to fix it

  1. Confirm the key exists and check its state, in the function's own Region:
    aws kms describe-key --key-id <key-arn>
    Read KeyState in the output. Disabled or PendingDeletion explains the failure by itself.
  2. Review the key policy, not just IAM identity policies. Check the key policy and grants for both the deploying principal and the function's execution role. Confirm kms:CreateGrant, kms:Encrypt and kms:DescribeKey for the deployment and Lambda grant path, plus kms:Decrypt wherever the execution role actually decrypts values. A KMS key is one of the resources where the resource policy is authoritative.
  3. Check the Region and account in the ARN. If it is malformed or points elsewhere, fix KmsKeyArn in the template — no permission change makes a cross-Region key work.
  4. For a key owned by another account, confirm the key policy in the owning account grants the deploying principal, and check whether an existing grant constrains the operation.
  5. Re-save or re-encrypt the environment configuration, then re-run the deployment once describe-key shows an enabled key in the right Region and the policy permits the grant.

AUTOMATE THE TRIAGE

Diagnose this automatically

SAM Doctor reads the KMS exception out of the Lambda wrapper (high confidence) and reports the key checks instead of routing you to the IAM policy simulator, which is where the generic denial finding would have sent you. Runs locally; no AWS access, no log upload.

python -m pip install sam-doctor
sam-doctor diagnose deployment.log --format markdown

RELATED