gpt4 book ai didi

amazon-web-services - 具有自定义授权和 Rest API 的 AWS 无服务器模板配置

转载 作者:行者123 更新时间:2023-12-04 08:08:41 24 4
gpt4 key购买 nike

我是 AWS Lambda 服务的新手。我创建了一个无服务器 lambda 方法并成功将其部署在 AWS 云上。

接下来,我创建了一个 Lambda 自定义授权器,并为 Lambda 方法和自定义授权器配置了 API 网关。

因为,我需要公开许多其他无服务器 lambda 方法,因此我决定将我的 lambda 方法移到无服务器 .Net API 项目中。我可以将此 api 项目部署到 AWS 云,然后我可以手动设置授权方以使用我的自定义 Authorize lambda 方法。

困难的部分是,我想通过 serverless.template 文件配置所有这些东西。

我正在努力为我的自定义授权方方法获取 RESTAPIID 以及如何使用 serverless.template 文件为我的 lambda 函数设置授权方。以下是我所做的配置。还有,如何获取AuthorizerUri

我不想硬编码任何东西。

    "Resources" : {
**//How I can create this serverless function to use my custom authorizer?**
"Create" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "Osn.Ott.Telco.Connector.UI.Web.Controllers.V10::Osn.Ott.Telco.Connector.UI.Web.Controllers.V10.SubscriptionController::Create",
"Runtime": "dotnetcore2.1",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"FunctionName" : "CreateCustomer",
"Policies": [ "AWSLambdaBasicExecutionRole" ],
"Events": {
"PutResource": {
"Type": "Api",
"Properties": {
"Path": "/create",
"Method": "POST"
}
}
}
}
},
"CustomAuthorizer" : {
"Type" : "AWS::ApiGateway::Authorizer",
"Properties" : {
"AuthorizerUri" : {"Fn::GetAtt" : [ "Create", "Arn"]},
"IdentitySource" : "method.request.header.Authorization,method.request.context.resourcePath, method.request.context.path",
"Name" : "CustomAuthorizer",
"Type" : "REQUEST",
**//How I can get this id?**
"RestApiId" : {"Fn::GetAtt" : [ "ServerlessRespApi", ""]}
}
}
}

最佳答案

AWS 宣布支持 AWS Serverless Application Model Supports Amazon API Gateway Authorizers上周(以前也可以完成,但后来必须在 SAM 模板中使用内联 Swagger)。

上面的页面链接了几个 GitHub 示例,我猜 Lambda Request Authorizer最接近你的问题。下面的代码是从 template.yaml 复制的.另请参阅 API Auth Object AWS SAM 规范的一部分。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: API Gateway with Lambda Token Authorizer
Resources:
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
Auth:
DefaultAuthorizer: MyLambdaRequestAuthorizer
Authorizers:
MyLambdaRequestAuthorizer:
FunctionPayloadType: REQUEST
FunctionArn: !GetAtt MyAuthFunction.Arn
# FunctionInvokeRole: !Ref MyRole
Identity:
QueryStrings:
- auth
# NOTE: Additional options:
# Headers:
# - Authorization
# StageVariables:
# - AUTHORIZATION
# Context:
# - authorization
# ReauthorizeEvery: 100 # seconds

MyFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src
Handler: index.handler
Runtime: nodejs8.10
Events:
GetRoot:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /
Method: get
Auth:
Authorizer: NONE
GetUsers:
Type: Api
Properties:
RestApiId: !Ref MyApi
Path: /users
Method: get

MyAuthFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: ./src
Handler: authorizer.handler
Runtime: nodejs8.10

Outputs:
ApiURL:
Description: "API URL"
Value: !Sub 'https://${MyApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'

关于amazon-web-services - 具有自定义授权和 Rest API 的 AWS 无服务器模板配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53104847/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com