首页 > 解决方案 > AWS:在多个无服务器应用程序中重用 amazon Cognito Userpool

问题描述

我已将无服务器项目分成多个项目,以避免模板错误资源 > 200。现在我面临另一个错误。

我已经使用 AWS cognito 实现了身份验证和授权,它适用于一个无服务器应用程序中的 lambda 函数。我正在尝试在另一个无服务器项目中重新使用相同的 Cognito 用户池来授权 API。但是,每个无服务器项目都会在 Cognito 中再次创建具有相同名称的 UserPool。如何避免这个问题。

这是我的 serverless.yml 和虚拟响应在 lambda 函数中的样子

CognitoUserPool:
  Type: "AWS::Cognito::UserPool"
  Properties:
    MfaConfiguration: OFF
    UserPoolName: userpool-impl
    #RequiredAttributes:
    #  - email
    UsernameAttributes:
      - email
    Policies:
      PasswordPolicy:
        MinimumLength: 8
        RequireLowercase: True
        RequireNumbers: True
        RequireSymbols: True
        RequireUppercase: True


CognitoUserPoolClient:
  Type: "AWS::Cognito::UserPoolClient"
  Properties:
    ClientName: client-impl
    GenerateSecret: False
    UserPoolId:
      Ref: CognitoUserPool

ApiGatewayAuthorizer:
  DependsOn:
    - ApiGatewayRestApi
  Type: AWS::ApiGateway::Authorizer
  Properties:
    Name: cognito-authorizer
    IdentitySource: method.request.header.Authorization
    RestApiId:
      Ref: ApiGatewayRestApi
    Type: COGNITO_USER_POOLS
    ProviderARNs:
      - Fn::GetAtt: [CognitoUserPool, Arn]

拉姆达函数:

response = {
"statusCode": 200,
'headers': {
             "Access-Control-Allow-Origin": "http://localhost:3000",
             "Access-Control-Allow-Credentials": True
        },

"body": "Dummy Response"

}

标签: amazon-web-servicesaws-lambdaamazon-cloudformationamazon-cognitoserverless-framework

解决方案


推荐阅读