CloudFormation 配置 API 网关方法以使用 Cognito 授权方
CloudFormation to Configure API Gateway Method to use Cognito Authorizer
我正在尝试使用 CloudFormation 定义 API 网关资源。具体来说,我正在尝试为使用 Cognito 进行身份验证的 API 网关资源方法创建一个模板。我已经创建了授权者,并且使用控制台我可以毫无问题地执行此配置(见附图)。我只是找不到使用 Cognito 用户池指定 API 方法请求授权的方法。这让我疯狂。据我所知,没有文档涵盖这一点。
有谁知道这是否可行,如果可行,该怎么做?我意识到我可以使用 Swagger 实现这一点,但我并不期待在 Swagger 与 CloudFormation 中重新定义我的所有 API 网关资源。
提前致谢!
我手边没有代码示例,但您需要执行以下操作:
1) 将 Authorizer 资源添加到类型为 "COGNITO_USER_POOLS"、
的模板
2) 将 API method 资源上的 authorizerId 设置为来自授权方的 ID 引用。将方法上的 authorizationType 设置为 "COGNITO_USER_POOLS"
至于用户池本身,您将需要使用自定义资源,至少在官方支持发布之前是这样。您可以使用多种 open-source 实现(这里是一个示例:https://github.com/aws-samples/aws-api-gateway-developer-portal/tree/7d0d1e56d54e9775ee2d18907ebdf1db9dafcc06/lambdas/cognito-cloudformation-custom-resource)
如果您使用 SAM,则将池设置为全局默认值并标记您不想进行身份验证的功能。
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Cors: "'*'"
Auth:
DefaultAuthorizer: MyCognitoAuthorizer
Authorizers:
MyCognitoAuthorizer:
UserPoolArn: !GetAtt MyCognitoUserPool.Arn
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src
Handler: lambda.handler
Runtime: nodejs8.10
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /
Method: GET
MyCognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: !Ref CognitoUserPoolName
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
MyCognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref MyCognitoUserPool
ClientName: !Ref CognitoUserPoolClientName
GenerateSecret: false
对于您不想落后于 cognito 的功能。在 AWS::Serverless::Function 定义的事件部分中定义。
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /
Method: GET
Auth:
Authorizer: 'NONE'
使用 AWS Sam 模板文档而不是 cloudformation 定义。
我正在尝试使用 CloudFormation 定义 API 网关资源。具体来说,我正在尝试为使用 Cognito 进行身份验证的 API 网关资源方法创建一个模板。我已经创建了授权者,并且使用控制台我可以毫无问题地执行此配置(见附图)。我只是找不到使用 Cognito 用户池指定 API 方法请求授权的方法。这让我疯狂。据我所知,没有文档涵盖这一点。
有谁知道这是否可行,如果可行,该怎么做?我意识到我可以使用 Swagger 实现这一点,但我并不期待在 Swagger 与 CloudFormation 中重新定义我的所有 API 网关资源。
提前致谢!
我手边没有代码示例,但您需要执行以下操作:
1) 将 Authorizer 资源添加到类型为 "COGNITO_USER_POOLS"、
的模板2) 将 API method 资源上的 authorizerId 设置为来自授权方的 ID 引用。将方法上的 authorizationType 设置为 "COGNITO_USER_POOLS"
至于用户池本身,您将需要使用自定义资源,至少在官方支持发布之前是这样。您可以使用多种 open-source 实现(这里是一个示例:https://github.com/aws-samples/aws-api-gateway-developer-portal/tree/7d0d1e56d54e9775ee2d18907ebdf1db9dafcc06/lambdas/cognito-cloudformation-custom-resource)
如果您使用 SAM,则将池设置为全局默认值并标记您不想进行身份验证的功能。
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Cors: "'*'"
Auth:
DefaultAuthorizer: MyCognitoAuthorizer
Authorizers:
MyCognitoAuthorizer:
UserPoolArn: !GetAtt MyCognitoUserPool.Arn
MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src
Handler: lambda.handler
Runtime: nodejs8.10
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /
Method: GET
MyCognitoUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: !Ref CognitoUserPoolName
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
MyCognitoUserPoolClient:
Type: AWS::Cognito::UserPoolClient
Properties:
UserPoolId: !Ref MyCognitoUserPool
ClientName: !Ref CognitoUserPoolClientName
GenerateSecret: false
对于您不想落后于 cognito 的功能。在 AWS::Serverless::Function 定义的事件部分中定义。
Events:
Root:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /
Method: GET
Auth:
Authorizer: 'NONE'
使用 AWS Sam 模板文档而不是 cloudformation 定义。