AWS Serverless Application Model(SAM)使用AWS::Serverless::Api和AWS::Serverless::HttpApi定义REST API,两者之间有一些区别。
AWS::Serverless::Api适用于旧版API,它使用Amazon API Gateway旧版,并且包含Lambda函数集成和其他功能。以下是一个使用AWS::Serverless::Api创建REST API的示例:
Resources:
ApiGatewayRestApi:
Type: AWS::Serverless::Api
Properties:
StageName: Prod
DefinitionBody:
swagger: '2.0'
info:
title: My API
paths:
/hello:
get:
responses:
'200':
description: OK
AWS::Serverless::HttpApi适用于新版API,它使用Amazon API Gateway v2,较低的延迟和更好的扩展性。以下是一个使用AWS::Serverless::HttpApi创建REST API的示例:
Resources:
HttpApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: Prod
DefinitionBody:
openapi: '3.0.1'
info:
title: My API
paths:
/hello:
get:
responses:
'200':
description: OK
可以看到,两者之间的区别在于所使用的API Gateway版本和不同的API定义格式。