我正在调用adminInitiateAuth并为我自己的lambda返回一个奇怪的AccessDeniedException。
以下是我致电的代码:
var params = {
AuthFlow: "ADMIN_NO_SRP_AUTH",
ClientId: "@cognito_client_id@",
UserPoolId: "@cognito_pool_id@",
AuthParameters: {
USERNAME : username,
PASSWORD : tempPassword
},
};
cognitoIdentityServiceProvider.adminInitiateAuth(params, function(error, data) {
if (error) {
console.log("ERROR! Login failed: " + JSON.stringify(error), error.stack);
} else {
console.log("Login sent back: " + JSON.stringify(data));
}
});
我收到的错误消息是:
ERROR! Login failed: {"message":"arn:aws:lambda:us-east-1:201473124518:function:main-devryan-users_onCognitoLogin failed with error AccessDeniedException.","code":"UnexpectedLambdaException","time":"2017-02-25T18:54:15.109Z","requestId":"ce42833f-fb8b-11e6-929b-2f78b63faa12","statusCode":400,"retryable":false,"retryDelay":1.0853444458916783} UnexpectedLambdaException: arn:aws:lambda:us-east-1:201473124518:function:main-devryan-users_onCognitoLogin failed with error AccessDeniedException.
有人知道我为什么会收到此错误吗?
答案 0 :(得分:31)
这种情况正在发生,因为我重新创建了我的API网关& Lambdas(使用无服务器)并且事实证明,当通过控制台作为触发器添加时,Cognito控制台偷偷添加了联系给定Lambda函数的权限。
要在您的CloudFormation / serverless.yml文件中修复此问题:
resources:
Resources:
OnCognitoSignupPermission:
Type: 'AWS::Lambda::Permission'
Properties:
Action: "lambda:InvokeFunction"
FunctionName:
Fn::GetAtt: [ "UsersUnderscoreonCognitoSignupLambdaFunction", "Arn"]
Principal: "cognito-idp.amazonaws.com"
SourceArn:
Fn::Join: [ "", [ "arn:aws:cognito-idp", ":", Ref: "AWS::Region", ":", Ref: "AWS::AccountId", ":", "userpool/", "@cognito_pool_id@" ] ]
要在AWS控制台中解决此问题:
这里an interesting Amazon forum post让我走上正轨。
答案 1 :(得分:22)
我遇到了与您类似的问题,除了我尝试通过CloudFormation配置我的Cognito用户池Lambda。
在Ryan发布的链接中,有人发布了一个代码示例。即Cognito需要适当的权限才能调用lambda函数。
MyLambdaInvocationPermission:
Type: AWS::Lambda::Permission
Properties:
Action: lambda:InvokeFunction
FunctionName: !GetAtt MyLambdaFunctionName.Arn
Principal: cognito-idp.amazonaws.com
SourceArn: !GetAtt MyCognitoUserPoolName.Arn
答案 2 :(得分:4)
对于最终在此处尝试通过terraform添加认知触发器的人,您要做的就是添加aws_lambda_permission资源:
resource "aws_lambda_permission" "allow_execution_from_user_pool" {
statement_id = "AllowExecutionFromUserPool"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.<lambda>.function_name
principal = "cognito-idp.amazonaws.com"
source_arn = aws_cognito_user_pool.<pool>.arn
}
在以下出色的文章中找到:https://www.integralist.co.uk/posts/cognito/
答案 3 :(得分:0)
您可以添加来自lambda角色的许可(创建用于cognito的策略并添加到lamda角色)。这可以解决我遇到的问题
答案 4 :(得分:0)
User:arn:aws::12345678:user/xyz is not authorized to perform:
cognito-idp:CreateUserPool on resource:*(Service:AWSCognitoIdentityProviderService;
Status Code: 400; Error Code: AccessDeniedException;Request ID: xxxxx)
aws-cognito-idp
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Cognito-IDP",
"Effect": "Allow",
"Action": [
"lambda:InvokeFunction",
"cognito-idp:*"
],
"Resource": "*"
}
]
}
注意:您可以限制对资源和cognito-idp用户的访问。
有了这个-我可以成功地为模块创建和部署cloudformation堆栈。