本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AWS::Serverless::HttpApi
创建 Amazon API Gateway HTTP API,这让您可以创建比 REST API 具有更低延迟和更低成本的 RESTful API。有关更多信息,请参阅《API Gateway 开发人员指南》中的使用 HTTP API。
我们建议你使用 Amazon CloudFormation 挂钩或 IAM 策略,以验证 API Gateway 资源是否附有授权者以控制对它们的访问权限。
有关使用的更多信息 Amazon CloudFormation 挂钩,参见 注册挂钩 在 Amazon CloudFormation CLI 用户指南 还有 apigw-enforce-授权器
有关使用 IAM 策略的更多信息,请参阅《API Gateway 开发人员指南》中的要求 API 路由具有授权。
此资源还支持 Connectors 资源 Attribute。有关更多信息,请参阅 使用管理资源权限 Amazon SAM 连接器。
当你部署到时 Amazon CloudFormation, Amazon SAM 改变你的 Amazon SAM 资源进入 Amazon CloudFormation 资源。有关更多信息,请参阅 生成 Amazon CloudFormation 的资源 Amazon SAM。
语法
在您的 Amazon Serverless Application Model (Amazon SAM) 模板,使用以下语法。
YAML
Type: AWS::Serverless::HttpApi Properties: AccessLogSettings:AccessLogSettingsAuth:HttpApiAuthCorsConfiguration:String | HttpApiCorsConfigurationDefaultRouteSettings:RouteSettingsDefinitionBody:JSONDefinitionUri:String | HttpApiDefinitionDescription:StringDisableExecuteApiEndpoint:BooleanDomain:HttpApiDomainConfigurationFailOnWarnings:BooleanName:StringPropagateTags:BooleanRouteSettings:RouteSettingsStageName:StringStageVariables:JsonTags:Map
Properties
-
AccessLogSettings -
某个阶段的访问日志记录的设置。
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
AccessLogSettings的财产AWS::ApiGatewayV2::Stage资源。 -
Auth -
配置授权,以控制对 API Gateway HTTP API 的访问。
有关更多信息,请参阅《API Gateway 开发人员指南》中的使用 JWT 授权方控制对 HTTP API 的访问。
类型:HttpApiAuth
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
CorsConfiguration -
为所有 API Gateway HTTP API 管理跨源资源共享 (CORS)。以字符串形式指定要允许的域,或者指定一个
HttpApiCorsConfiguration对象。请注意,CORS 需要 Amazon SAM 修改你的 OpenAPI 定义,因此 CORS 仅在以下情况下才起作用DefinitionBody属性已指定。有关更多信息,请参阅《API Gateway 开发人员指南》中的为 HTTP API 配置 CORS。
注意如果
CorsConfiguration既在 OpenAPI 定义中设置,也是在属性级别设置的,那么 Amazon SAM 将两个配置源合并,属性优先。如果将此属性设置为true,则允许所有来源。类型: 字符串 | HttpApiCorsConfiguration
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
DefaultRouteSettings -
此 HTTP API 的默认路由设置。这些设置适用于所有路由,除非被某些路由的
RouteSettings属性覆盖。必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
RouteSettings的财产AWS::ApiGatewayV2::Stage资源。 -
DefinitionBody -
描述您的 HTTP API 的 OpenAPI 定义。如果你没有指定
DefinitionUri或者DefinitionBody, Amazon SAM 生成一个DefinitionBody根据您的模板配置为您服务。类型:JSON
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
Body的财产AWS::ApiGatewayV2::Api资源。如果提供了某些属性, Amazon SAM 可以将内容插入或修改DefinitionBody在传递给之前 Amazon CloudFormation。房产包括Auth还有一个EventSource类型的 HttpApi 用于相应的AWS::Serverless::Function资源。 -
DefinitionUri -
定义 HTTP API 的 OpenAPI 定义的 Amazon Simple Storage Service (Amazon S3) URI、本地文件路径或位置对象。此属性引用的 Amazon S3 对象必须是有效的 OpenAPI 定义文件。如果你没有指定
DefinitionUri或者DefinitionBody已指定, Amazon SAM 生成一个DefinitionBody根据您的模板配置为您服务。如果您提供本地文件路径,则模板必须经过包含
sam deploy或sam package命令的工作流程,才能使定义正确转换。外部不支持内部函数 OpenApi 您引用的定义文件
DefinitionUri。要导入 OpenApi 定义到模板中,使用DefinitionBody的财产 包括转换。类型: 字符串 | HttpApiDefinition
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
BodyS3Location的财产AWS::ApiGatewayV2::Api资源。嵌套的 Amazon S3 属性的命名有所不同。 -
Description -
HTTP API 资源的描述。
当你指定时
Description, Amazon SAM 将修改 HTTP API 资源的 OpenApi 通过设置来定义description领域。以下情况将导致错误:-
这个
DefinitionBody属性是用指定的description在 Open API 定义中设置的字段 — 这会导致 Open API 定义中的冲突description那个字段 Amazon SAM 不会解决。 -
这个
DefinitionUri属性已指定 — Amazon SAM 不会修改从亚马逊 S3 检索到的 Open API 定义。
类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
-
DisableExecuteApiEndpoint -
指定客户端是否可以使用默认
execute-api端点https://{api_id}.execute-api.{region}.amazonaws.com调用您的 HTTP API。默认情况下,客户端可以使用默认端点调用您的 API。如果要求客户端仅使用自定义域名调用 API,请禁用默认端点。要使用此属性,您必须指定
DefinitionBody属性而不是DefinitionUri属性,或在 OpenAPI 定义中使用disableExecuteApiEndpoint定义x-amazon-apigateway-endpoint-configuration。类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
DisableExecuteApiEndpoint的财产AWS::ApiGatewayV2::Api资源。它直接传递给x-amazon-apigateway-endpoint-configuration扩展的disableExecuteApiEndpoint属性,然后添加到AWS::ApiGatewayV2::Api资源的Body属性中。 -
Domain -
为此 API Gateway HTTP API 配置自定义域。
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
FailOnWarnings -
指定在遇到警告时是回滚 (
true) 还是不回滚 (false) HTTP API 创建。默认值为false。类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
FailOnWarnings的财产AWS::ApiGatewayV2::Api资源。 Name-
HTTP API 资源的名称。
当你指定时
Name, Amazon SAM 将通过设置来修改 HTTP API 资源的 OpenAPI 定义title领域。以下情况将导致错误:-
这个
DefinitionBody属性是用指定的title在 Open API 定义中设置的字段 — 这会导致 Open API 定义中的冲突title那个字段 Amazon SAM 不会解决。 -
这个
DefinitionUri属性已指定 — Amazon SAM 不会修改从亚马逊 S3 检索到的 Open API 定义。
类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
-
指明是否将
Tags属性中的标签传递给 AWS::Serverless::HttpApi 生成的资源。指定True以在生成的资源中传播标签。类型:布尔值
必需:否
默认值:
FalseAmazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
RouteSettings -
此 HTTP API 的每个路由的路由设置。要了解更多信息,请参阅《API Gateway 开发人员指南》中的使用 HTTP API 的路由。
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
RouteSettings的财产AWS::ApiGatewayV2::Stage资源。 -
StageName -
API 阶段的名称。如果未指定名称, Amazon SAM 使用
$default从 API 网关开始。类型:字符串
必需:否
默认值:$default
Amazon CloudFormation 兼容性: 此属性直接传递给
StageName的财产AWS::ApiGatewayV2::Stage资源。 -
StageVariables -
一个定义阶段变量的映射。变量名可以包含字母数字和下划线字符。这些值必须匹配 [A-Za-z0-9-._~:/?#&=,] +。
类型:Json
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
StageVariables的财产AWS::ApiGatewayV2::Stage资源。 -
指定要添加到此 API Gateway 阶段的标签的映射(字符串到字符串)。密钥的长度可以在 1 到 128 个 Unicode 字符之间,并且不能包含前缀
aws:。您可以使用以下任一字符:Unicode 字母、数字、空格、_、.、/、=、+和-的组合。值的长度可以在 1 到 256 个 Unicode 字符之间。类型:映射
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
其他注意事项: 那个
Tags属性要求 Amazon SAM 修改你的 OpenAPI 定义,因此只有在以下情况下才会添加标签DefinitionBody属性已指定—如果是,则不添加任何标签DefinitionUri属性已指定。 Amazon SAM 自动添加一个httpapi:createdBy:SAM标签。标签还会添加到AWS::ApiGatewayV2::Stage资源和AWS::ApiGatewayV2::DomainName资源(如果已指定DomainName)。
返回值
Ref
在将此资源的逻辑 ID 传递给内置 Ref 函数时,Ref 会返回底层 AWS::ApiGatewayV2::Api 资源的 API ID,例如 a1bcdef2gh。
有关使用 Ref 函数的更多信息,请参阅《Amazon CloudFormation 用户指南》中的 Ref。
示例
简单 HttpApi
以下示例显示了设置由 Lambda 事件函数支持的 HTTP API 终端节点所需的最低要求。此示例使用默认 HTTP API Amazon SAM 创建。
YAML
AWSTemplateFormatVersion: '2010-09-09' Description: Amazon SAM template with a simple API definition Resources: ApiFunction: Type: AWS::Serverless::Function Properties: Events: ApiEvent: Type: HttpApi Handler: index.handler InlineCode: | def handler(event, context): return {'body': 'Hello World!', 'statusCode': 200} Runtime: python3.7 Transform: AWS::Serverless-2016-10-31
HttpApi 使用 Auth
以下示例说明如何在 HTTP API 端点上设置授权。
YAML
Properties: FailOnWarnings: true Auth: DefaultAuthorizer: OAuth2 Authorizers: OAuth2: AuthorizationScopes: - scope4 JwtConfiguration: issuer: "https://www.example.com/v1/connect/oauth2" audience: - MyApi IdentitySource: "$request.querystring.param"
HttpApi 使用 OpenAPI 定义
以下示例说明如何将 OpenAPI 定义添加到模板。
请注意 Amazon SAM 填写所有缺失的 Lambda 集成 HttpApi 引用此 HTTP API 的事件。 Amazon SAM 还会添加所有缺失的路径 HttpApi 事件参考。
YAML
Properties: FailOnWarnings: true DefinitionBody: info: version: '1.0' title: Ref: AWS::StackName paths: "/": get: security: - OpenIdAuth: - scope1 - scope2 responses: {} openapi: 3.0.1 securitySchemes: OpenIdAuth: type: openIdConnect x-amazon-apigateway-authorizer: identitySource: "$request.querystring.param" type: jwt jwtConfiguration: audience: - MyApi issuer: https://www.example.com/v1/connect/oidc openIdConnectUrl: https://www.example.com/v1/connect/oidc/.well-known/openid-configuration
HttpApi 使用配置设置
以下示例说明如何将 HTTP API 和阶段配置添加到模板。
YAML
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Parameters: StageName: Type: String Default: Prod Resources: HttpApiFunction: Type: AWS::Serverless::Function Properties: InlineCode: | def handler(event, context): import json return { "statusCode": 200, "body": json.dumps(event), } Handler: index.handler Runtime: python3.7 Events: ExplicitApi: # warning: creates a public endpoint Type: HttpApi Properties: ApiId: !Ref HttpApi Method: GET Path: /path TimeoutInMillis: 15000 PayloadFormatVersion: "2.0" RouteSettings: ThrottlingBurstLimit: 600 HttpApi: Type: AWS::Serverless::HttpApi Properties: StageName: !Ref StageName Tags: Tag: Value AccessLogSettings: DestinationArn: !GetAtt AccessLogs.Arn Format: $context.requestId DefaultRouteSettings: ThrottlingBurstLimit: 200 RouteSettings: "GET /path": ThrottlingBurstLimit: 500 # overridden in HttpApi Event StageVariables: StageVar: Value FailOnWarnings: true AccessLogs: Type: AWS::Logs::LogGroup Outputs: HttpApiUrl: Description: URL of your API endpoint Value: Fn::Sub: 'https://${HttpApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/${StageName}/' HttpApiId: Description: Api id of HttpApi Value: Ref: HttpApi