Jie*_*eng 13 amazon-web-services aws-cloudformation amazon-cognito
如何使用AWS SAM创建API,使用Cognito User Pools授权程序进行授权?
Theres AWS :: ApiGateway :: Authorizer.但......
{
"Type" : "AWS::ApiGateway::Authorizer",
"Properties" : {
"AuthorizerCredentials" : String,
"AuthorizerResultTtlInSeconds" : Integer,
"AuthorizerUri" : String,
"IdentitySource" : String,
"IdentityValidationExpression" : String,
"Name" : String,
"ProviderARNs" : [ String, ... ],
"RestApiId" : String,
"Type" : String
}
}
Run Code Online (Sandbox Code Playgroud)
它看起来像RestApiId是指使用此授权程序的API?但是使用AWS SAM,我的API被定义为
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
Run Code Online (Sandbox Code Playgroud)
我不知道如何将它们联系在一起?
小智 6
您现在可以使用“ServerlessRestApi”引用隐式创建的 API 网关。因此,在您的 SAM 模板中添加这段常规的 Cloudformation,一切都会正常工作
ApiCognitoAuthorizer:
Type: AWS::ApiGateway::Authorizer
Properties:
IdentitySource: 'method.request.header.Authorization'
Name: ApiCognitoAuthorizer
ProviderARNs:
- 'arn:aws:cognito-idp:{region}:{userpoolIdentifier}'
RestApiId: !Ref ServerlessRestApi
Type: COGNITO_USER_POOLS
Run Code Online (Sandbox Code Playgroud)
我不确定您是否可以在 SAM 中指定授权者,但您可以将 Swagger 嵌入 SAM 文件中来执行此操作。这是自 2 月 17 日起的一项新功能 [参考]。
我绝对不是 Swagger 或 SAM 方面的专家,但您似乎想要类似的东西:
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
Run Code Online (Sandbox Code Playgroud)
参考:
编辑:修复了“安全”部分的 Swagger 2.0 语法,它应该是一个列表。
| 归档时间: |
|
| 查看次数: |
4170 次 |
| 最近记录: |