8

Tôi làm cách nào để tạo một API với AWS SAM có ủy quyền sử dụng trình ủy quyền của Người dùng Cognito?API SAM AWS với người dùng Cognito Author Pools

Theres AWS::ApiGateway::Authorizer. Nhưng ...

{ 
    "Type" : "AWS::ApiGateway::Authorizer", 
    "Properties" : { 
    "AuthorizerCredentials" : String, 
    "AuthorizerResultTtlInSeconds" : Integer, 
    "AuthorizerUri" : String, 
    "IdentitySource" : String, 
    "IdentityValidationExpression" : String, 
    "Name" : String, 
    "ProviderARNs" : [ String, ... ], 
    "RestApiId" : String, 
    "Type" : String 
    } 
} 

có vẻ như RestApiId đề cập đến API sử dụng trình ủy quyền này? Nhưng với AWS SAM, API của tôi được xác định là

Resources: 
    Ec2Index: 
    Type: AWS::Serverless::Function 
    Properties: 
     Handler: ec2/index.handler 
     Runtime: nodejs6.10 
     CodeUri: ./src 
     FunctionName: 'ApiEc2IndexHandler' 
     Description: 'List EC2 resources' 
     Timeout: 30 
     Role: 'arn:aws:iam::598545985414:role/awsmanagement-lambda-management' 
     Events: 
     Ec2Index: 
      Type: Api 
      Properties: 
      Path: /ec2 
      Method: get 

Tôi không biết cách liên kết chúng với nhau?

Trả lời

2

Tôi không chắc chắn bạn có thể chỉ định người ủy quyền trong SAM nhưng bạn có thể nhúng Swagger trong các tệp SAM có thể thực hiện việc này. Đó là một tính năng mới kể từ ngày 17 tháng 2 [ref].

Tôi chắc chắn không phải là một chuyên gia về Swagger hoặc SAM nhưng nó có vẻ như bạn muốn một cái gì đó như:

AWSTemplateFormatVersion: '2010-09-09' 
Transform: AWS::Serverless-2016-10-31 
Description: Simple API Endpoint configured using Swagger specified inline and backed by a Lambda function 
Resources: 
    Ec2Index: 
    Type: AWS::Serverless::Api 
    Properties: 
     StageName: <stage> 
     DefinitionBody: 
      swagger: 2.0 
      info: 
       title: 
       Ref: AWS::StackName 
      securityDefinitions: 
       cognitoUserPool: 
       type: apiKey, 
       name: "Authorization" 
       in: header 
       x-amazon-apigateway-authtype: cognito_user_pools 
       x-amazon-apigateway-authorizer: 
        type: cognito_user_pools 
        providerARNs: 
        - arn:aws:cognito-idp:${AWS::Region}:{AWS::AccountId}:userpool/<user_pool_id> 
      paths: 
       "/ec2": 
       get: 
        security: 
        cognitoUserPool: [] 
        x-amazon-apigateway-integration: 
        httpMethod: POST 
        type: aws_proxy 
        uri: 
         Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Ec2IndexLamb.Arn}/invocations 
        responses: {} 
      swagger: '2.0' 
    Ec2IndexLamb: 
    Type: AWS::Serverless::Function 
    Properties: 
     Handler: ec2/index.handler 
     Runtime: nodejs6.10 
     CodeUri: ./src 
     FunctionName: 'ApiEc2IndexHandler' 
     Description: 'List EC2 resources' 
     Timeout: 30 
     Role: 'arn:aws:iam::598545985414:role/awsmanagement-lambda-management' 
     Events: 
     Ec2Index: 
      Type: Api 
      Properties: 
      Path: /ec2 
      Method: get 

Tài liệu tham khảo:

https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-integrate-with-cognito.html#apigateway-enable-cognito-user-pool

https://github.com/awslabs/serverless-application-model/blob/master/examples/2016-10-31/inline_swagger/template.yaml

Các vấn đề liên quan