我的 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 (AWS KMS) 权限。如果 AWS KMS 密钥和 IAM 角色位于不同的 AWS 账户,请同时更新 IAM 策略和 AWS KMS 密钥策略。
注意: 如果您在运行 AWS 命令行界面 (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 密钥的权限。
- 要获取函数当前执行角色的 Amazon 资源名称 (ARN) 和 AWS KMS 密钥,请运行以下 get-function-configuration 命令:
aws lambda get-function-configuration --function-name your-function-name
注意: 将 your-function-name 替换为您的函数名称。
- 要将函数的执行角色更新为不同的临时值,请运行以下 update-function-configuration 命令:
aws lambda update-function-configuration --function-name yourFunctionName --role temporary-value
注意: 将 temporary-value 替换为临时执行角色的 ARN。
- 要将函数的执行角色更新回原始执行角色,请运行以下 update-function-configuration 命令:
aws lambda update-function-configuration --function-name yourFunctionName --role originalValue
注意: 将 originalValue 替换为原始执行角色的 ARN。
解决 "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 密钥的 ARN。
解决 "You are not authorized to perform" 错误
如果您的 IAM 身份没有访问 AWS KMS 密钥所需的权限,您会收到以下错误消息之一:
"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 密钥权限的 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 密钥的 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 密钥访问错误?
相关信息
如何对来自 AWS Lambda 的 HTTP 502 和 HTTP 500 状态代码错误进行故障排除?
如何排查 Lambda 函数故障?