为了创建Cognito用户池授权程序,请在AWS SAM模板的Resources部分添加以下代码示例:
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: prod
Auth:
DefaultAuthorizer: MyCognitoAuthorizer
Authorizers:
MyCognitoAuthorizer:
UserPoolArn: !GetAtt MyUserPool.Arn
IdentitySource: method.request.header.Authorization
Type: COGNITO_USER_POOLS
MyUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: MyUserPool
Policies:
PasswordPolicy:
MinimumLength: 8
RequireLowercase: true
RequireNumbers: true
RequireSymbols: true
RequireUppercase: true
Schema:
- Name: email
AttributeDataType: String
Mutable: true
Required: true
UsernameAttributes:
- email
此示例创建了一个名为“MyApi”的API,其中包含“DefaultAuthorizer”属性和“Authorizers”属性。它还创建了一个名为“MyUserPool”的Cognito用户池,并将其绑定到API的“DefaultAuthorizer”属性。要确保在添加用户池授权程序时更新身份源(IdentitySource)属性。