SAM 无服务器隐式 API 与 AWS::Serverless::Api
SAM Serverless implicit API vs AWS::Serverless::Api
配置 SAM 模板并定义 AWS::Serverless::Function
时,事件参数接受 Api 类型。这会创建 API 网关资源吗?此事件类型与独立 AWS::Serverless::Api
资源有何区别?
An AWS::Serverless::Api resource need not be explicitly added to a AWS Serverless Application Definition template. A resource of this type is implicitly created from the union of Api events defined on AWS::Serverless::Function resources defined in the template that do not refer to an AWS::Serverless::Api resource.
题目问的是SAMAWS::Serverless::Function类型的事件源块中引用的APIs,例如:
MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Events:
MyApi:
Type: Api
Properties:
Path: /resource
Method: GET
如各个地方的文档中所述,这些在 SAM 中称为 "implicit APIs"。
SAM 从 AWS::Serverless::Function 资源上定义的 Api 事件的联合创建类型 AWS::Serverless::Api 的资源 - 但仅限那些不引用的资源(通过 RestApi Id 属性) 到 AWS::Serverless::Api 在模板中明确定义。
在幕后,SAM 收集所有这些隐式 APIs,生成一个 Swagger,并使用此 Swagger 创建隐式 APIs。此 API 默认为名为 "Prod" 的 StageName,无法配置。
如果您确实需要控制在 Swagger 中定义和记录 API,则应显式创建 AWS::Serverless::Api 资源。然后将以这种方式引用它:
MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Events:
MyApi:
Type: Api
Properties:
Path: /resource
Method: GET
RestApiId: !Ref MyAPI # Add this line
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionBody:
...
所以它们之间的唯一区别是您对它们的配置有多少控制权,关键考虑因素是您是否需要定义其中之一:
- 艺名
- Swagger 定义(通过 DefinitionBody)
如果您需要控制其中一个或两个,那么您需要明确定义您的 API。否则,您可能可以使用隐式 APIs.
另请注意,SAM 中的 AWS::Serverless::Api 资源 "transformed" 到 AWS::ApiGateway::RestApi、AWS::ApiGateway::Stage 和 AWS::ApiGateway::Deployment 类型的 CloudFormation 资源中。
请注意,此信息是在这三个源文档中找到的信息的摘要:
- https://github.com/awslabs/serverless-application-model/blob/develop/versions/2016-10-31.md#awsserverlessapi
- https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
- https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst#implicit-apis
配置 SAM 模板并定义 AWS::Serverless::Function
时,事件参数接受 Api 类型。这会创建 API 网关资源吗?此事件类型与独立 AWS::Serverless::Api
资源有何区别?
An AWS::Serverless::Api resource need not be explicitly added to a AWS Serverless Application Definition template. A resource of this type is implicitly created from the union of Api events defined on AWS::Serverless::Function resources defined in the template that do not refer to an AWS::Serverless::Api resource.
题目问的是SAMAWS::Serverless::Function类型的事件源块中引用的APIs,例如:
MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Events:
MyApi:
Type: Api
Properties:
Path: /resource
Method: GET
如各个地方的文档中所述,这些在 SAM 中称为 "implicit APIs"。
SAM 从 AWS::Serverless::Function 资源上定义的 Api 事件的联合创建类型 AWS::Serverless::Api 的资源 - 但仅限那些不引用的资源(通过 RestApi Id 属性) 到 AWS::Serverless::Api 在模板中明确定义。
在幕后,SAM 收集所有这些隐式 APIs,生成一个 Swagger,并使用此 Swagger 创建隐式 APIs。此 API 默认为名为 "Prod" 的 StageName,无法配置。
如果您确实需要控制在 Swagger 中定义和记录 API,则应显式创建 AWS::Serverless::Api 资源。然后将以这种方式引用它:
MyFunction:
Type: AWS::Serverless::Function
Properties:
...
Events:
MyApi:
Type: Api
Properties:
Path: /resource
Method: GET
RestApiId: !Ref MyAPI # Add this line
MyApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionBody:
...
所以它们之间的唯一区别是您对它们的配置有多少控制权,关键考虑因素是您是否需要定义其中之一:
- 艺名
- Swagger 定义(通过 DefinitionBody)
如果您需要控制其中一个或两个,那么您需要明确定义您的 API。否则,您可能可以使用隐式 APIs.
另请注意,SAM 中的 AWS::Serverless::Api 资源 "transformed" 到 AWS::ApiGateway::RestApi、AWS::ApiGateway::Stage 和 AWS::ApiGateway::Deployment 类型的 CloudFormation 资源中。
请注意,此信息是在这三个源文档中找到的信息的摘要:
- https://github.com/awslabs/serverless-application-model/blob/develop/versions/2016-10-31.md#awsserverlessapi
- https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
- https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst#implicit-apis