AWS 无服务器 - 我可以在我的 serverless.yml 文件中配置我的提供商或 lambda 函数以使用同一 yml 文件中的 API 网关资源吗?
AWS Serverless - Can I configure my provider or lambda function in my serverless.yml file to use an API Gateway resource in the same yml file?
我有点难以理解这一点。我想在我的 serverless.yml 中定义一个 API 网关资源,然后我使用 http 事件创建的任何 lamdba 函数都将使用该 API 资源。
我这样做的原因是我有几个单独定义的服务,我想将它们分组在一个 API 下,但在我的主要 API 中,包括与我交互的 lambda 函数应用数据。虽然这些服务将在应用程序中使用,但这些服务执行多种不同类型的操作,因此将它们分开是有意义的。
我的 serverless.yml 非常简单,但每次部署它时,都会创建 2 API。一个是空白的,是我在我的资源下定义的一个,第二个是 AWS 由于我的 lamdba 函数而创建的。
# NOTE: update this with your service name
frameworkVersion: '^2.28.7'
service: web-api
# Create an optimized package for our functions
package:
individually: true
plugins:
- serverless-bundle # Package our functions with Webpack
- serverless-offline
custom:
stage: ${opt:stage, self:provider.stage}
provider:
name: aws
runtime: nodejs12.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-2'}
apiGateway:
restApiId: xxxxxxxxxx # <--- there is where I am having trouble
restApiRootResourceId: xxxxxxxxxx # <--- there is where I am having trouble
environment:
VERSION: 'v1'
functions:
getAbout:
handler: src/api/about/get.main
events:
- http:
path: /about
method: get
cors: true
resources:
Resources:
WebApiGW:
Type: AWS::ApiGateway::RestApi
Properties:
Name: '${self:provider.stage}-${self:service}'
Outputs:
apiGatewayRestApiId:
Value:
Ref: WebApiGW
Export:
Name: WebApiGateway-restApiId
apiGatewayRestApiRootResourceId:
Value:
Fn::GetAtt:
- WebApiGW
- RootResourceId
Export:
Name: WebApiGateway-rootResourceId
您应该尝试添加 api 资源并在您的函数中使用它们,如下所述。
example to bind api resources
我不确定我们是否需要将 API 网关放在资源下,因为事件 http 已经为您创建了它。
从无服务器框架的角度来看,无法从 Resources
部分引用 动态值,因为它们仍待生成。 provider
部分将首先加载,以定义如何创建其他所有资源。这就是为什么您只能在 provider
.
中设置静态值或从环境变量加载的值
默认情况下,如果您没有设置 provider.apiGateway.restApiId
的值,Serverless Framework 将为您生成一个新的API。
要解决您描述的问题,您需要先在不同的堆栈中生成一个 API 网关,然后在您的 serverless.yaml
.
中使用此堆栈生成的 ID
无服务器框架文档中有一个很好的演练,展示了如何将 API 分解为小模块(或如您所说的组)的示例。
在GitHub中:
或在网站中:
他们创建了一个 serverless-base.yaml
来定义一个 API 网关并导出 IDS,然后定义两个服务,serverless-a.yaml
用于 ServiceA 和 serverless-b.yaml
用于 ServiceB,他们在那里将使用由 serverless-base.yaml
.
生成的 API 网关
我有点难以理解这一点。我想在我的 serverless.yml 中定义一个 API 网关资源,然后我使用 http 事件创建的任何 lamdba 函数都将使用该 API 资源。
我这样做的原因是我有几个单独定义的服务,我想将它们分组在一个 API 下,但在我的主要 API 中,包括与我交互的 lambda 函数应用数据。虽然这些服务将在应用程序中使用,但这些服务执行多种不同类型的操作,因此将它们分开是有意义的。
我的 serverless.yml 非常简单,但每次部署它时,都会创建 2 API。一个是空白的,是我在我的资源下定义的一个,第二个是 AWS 由于我的 lamdba 函数而创建的。
# NOTE: update this with your service name
frameworkVersion: '^2.28.7'
service: web-api
# Create an optimized package for our functions
package:
individually: true
plugins:
- serverless-bundle # Package our functions with Webpack
- serverless-offline
custom:
stage: ${opt:stage, self:provider.stage}
provider:
name: aws
runtime: nodejs12.x
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-2'}
apiGateway:
restApiId: xxxxxxxxxx # <--- there is where I am having trouble
restApiRootResourceId: xxxxxxxxxx # <--- there is where I am having trouble
environment:
VERSION: 'v1'
functions:
getAbout:
handler: src/api/about/get.main
events:
- http:
path: /about
method: get
cors: true
resources:
Resources:
WebApiGW:
Type: AWS::ApiGateway::RestApi
Properties:
Name: '${self:provider.stage}-${self:service}'
Outputs:
apiGatewayRestApiId:
Value:
Ref: WebApiGW
Export:
Name: WebApiGateway-restApiId
apiGatewayRestApiRootResourceId:
Value:
Fn::GetAtt:
- WebApiGW
- RootResourceId
Export:
Name: WebApiGateway-rootResourceId
您应该尝试添加 api 资源并在您的函数中使用它们,如下所述。
example to bind api resources
我不确定我们是否需要将 API 网关放在资源下,因为事件 http 已经为您创建了它。
从无服务器框架的角度来看,无法从 Resources
部分引用 动态值,因为它们仍待生成。 provider
部分将首先加载,以定义如何创建其他所有资源。这就是为什么您只能在 provider
.
默认情况下,如果您没有设置 provider.apiGateway.restApiId
的值,Serverless Framework 将为您生成一个新的API。
要解决您描述的问题,您需要先在不同的堆栈中生成一个 API 网关,然后在您的 serverless.yaml
.
无服务器框架文档中有一个很好的演练,展示了如何将 API 分解为小模块(或如您所说的组)的示例。
在GitHub中:
或在网站中:
他们创建了一个 serverless-base.yaml
来定义一个 API 网关并导出 IDS,然后定义两个服务,serverless-a.yaml
用于 ServiceA 和 serverless-b.yaml
用于 ServiceB,他们在那里将使用由 serverless-base.yaml
.