如何解決 Lambda 函式的「KMS Exception」權限錯誤?
我的 AWS Lambda 函式傳回「KMS Exception」、「You are not authorized to perform」或「Access to KMS is not allowed」錯誤。
解決方法
請根據錯誤訊息,更新 AWS Identity and Access Management (IAM) 身分的 AWS Key Management Service (KMS) 權限。如果 AWS KMS key 與 IAM 角色位於不同的 AWS 帳戶,請同時更新 IAM 政策與 AWS KMS key 政策。
注意: 如果執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤,請參閱疑難排解 AWS CLI 錯誤。此外,請確認您使用的是最新版本的 AWS CLI。
解決「KMS Exception: UnrecognizedClientExceptionKMS Message」錯誤
如果您刪除 Lambda 函式的角色後,使用相同名稱但不同的主體重新建立該角色,系統會傳回以下錯誤:
「Calling the invoke API action failed with this message: Lambda was unable to decrypt the environment variables because KMS access was denied.Please check the function's AWS KMS key settings.KMS Exception: UnrecognizedClientExceptionKMS Message: The security token included in the request is invalid.」
若要解決此錯誤,您必須重設函式執行角色的 AWS KMS 授權。
注意: 建立及更新 Lambda 函式的 IAM 使用者必須具有使用 AWS KMS key 的權限。
- 若要取得函式目前執行角色與 AWS KMS key 的 Amazon Resource Name (ARN),請執行以下 get-function-configuration 命令:
注意: 將 your-function-name 替換為您的函式名稱。aws lambda get-function-configuration --function-name your-function-name - 若要暫時將函式的執行角色更新為其他值,請執行以下 update-function-configuration 命令:
注意: 將 temporary-value 替換為臨時執行角色 ARN。aws lambda update-function-configuration --function-name yourFunctionName --role temporary-value - 若要將函式的執行角色改回原始執行角色,請執行以下 update-function-configuration 命令:
注意: 將 originalValue 替換為原始執行角色 ARN。aws lambda update-function-configuration --function-name yourFunctionName --role originalValue
解決「KMS Exception: AccessDeniedException KMS Message」錯誤
如果 IAM 身分缺少執行 kms:Decrypt API 動作所需的權限,系統會傳回以下錯誤訊息:
「Lambda was unable to decrypt your environment variables because the KMS access was denied.Please check your KMS permissions.KMS Exception: AccessDeniedException KMS 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.」
若要解決此錯誤,請使用 IAM 主控台,將以下政策陳述式新增至您的 IAM 使用者或角色:
{ "Version": "2012-10-17", "Statement": [ { "Sid": "VisualEditor0", "Effect": "Allow", "Action": "kms:Decrypt", "Resource": "your-KMS-key-arn" } ] }
注意: 將 your-KMS-key-arn 替換為您的 AWS KMS key ARN。
解決「You are not authorized to perform」錯誤
如果 IAM 身分缺少存取 AWS KMS key 所需的權限,系統會傳回以下其中一則錯誤訊息:
「You are not authorized to perform: kms:Encrypt.」
「You are not authorized to perform: kms:CreateGrant.」
「User: user-arn is not authorized to perform: kms:ListAliases on resource: * with an explicit deny.」
注意: 如果您使用預設金鑰政策,IAM 身分或函式執行角色便不需要 AWS KMS 權限。
若要解決這些錯誤,請確認 IAM 使用者或角色具有執行以下 AWS KMS API 動作所需的權限:
如果 IAM 使用者或角色缺少上述權限,請使用 IAM 主控台新增權限。
授予客戶自管 AWS KMS key 存取權限的 IAM 政策範例
{ "Version": "2012-10-17", "Statement": [ { "Sid": "statement1", "Effect": "Allow", "Action": [ "kms:Decrypt", "kms:Encrypt", "kms:CreateGrant" ], "Resource": "your-kms-key-arn" }, { "Sid": "statement2", "Effect": "Allow", "Action": "kms:ListAliases", "Resource": "*" } ] }
注意: Resource 值必須是 "*"。kms:ListAliases 動作不支援低階權限。另請務必將 your-kms-key-arn 替換為您的 AWS KMS key ARN。
解決「Access to KMS is not allowed」錯誤
如果 IAM 實體缺少取得 AWS Secrets Manager 祕密的權限,系統會傳回以下錯誤訊息:
「Access to KMS is not allowed (Service: AWSSecretsManager; Status Code: 400; Error Code: AccessDeniedException; Request ID: 123a4bcd-56e7-89fg-hij0-1kl2m3456n78)」
若要解決此錯誤,請確認 IAM 使用者或角色具有執行以下 AWS KMS API 動作所需的權限:
如果 IAM 使用者或角色缺少上述權限,請使用 IAM 主控台新增權限。
如需更多資訊,請參閱嘗試擷取加密的 Secrets Manager 祕密後,如何解決 AWS KMS key 存取錯誤?
相關資訊
This article was reviewed and updated on 2026-02-27.
相關內容
已提問 1 年前
已提問 3 年前
AWS 官方已更新 2 年前
