本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
AWS::Serverless:: Api
创建一组可通过 HTTPS 端点调用的 Amazon API Gateway 资源和方法。
一个 AWS::Serverless:: Api 无需将资源显式添加到 Amazon 无服务器应用程序定义模板。这种类型的资源是通过 AWS::Serverless:: 函数 资源上定义的 Api 事件的并集隐式创建的,这些资源定义于未引用 AWS::Serverless:: Api 资源的模板。
一个 AWS::Serverless:: Api 应使用资源来定义和记录 API OpenApi,它提供了更多配置基础亚马逊 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::Api Properties: AccessLogSetting:AccessLogSettingAlwaysDeploy:BooleanApiKeySourceType:StringAuth:ApiAuthBinaryMediaTypes:ListCacheClusterEnabled:BooleanCacheClusterSize:StringCanarySetting:CanarySettingCors:String | CorsConfigurationDefinitionBody:JSONDefinitionUri:String | ApiDefinitionDescription:StringDisableExecuteApiEndpoint:BooleanDomain:DomainConfigurationEndpointAccessMode:StringEndpointConfiguration:EndpointConfigurationFailOnWarnings:BooleanGatewayResponses:MapMergeDefinitions:BooleanMethodSettings:MethodSettingsMinimumCompressionSize:IntegerMode:StringModels:MapName:StringOpenApiVersion:StringPropagateTags:BooleanPolicy:JSONSecurityPolicy:StringStageName:StringTags:MapTracingEnabled:BooleanVariables:Map
Properties
-
AccessLogSetting -
配置某个阶段的访问日志设置。
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
AccessLogSetting的财产AWS::ApiGateway::Stage资源。 -
AlwaysDeploy -
即使未检测到 API 的更改,也要始终部署 API。
类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
ApiKeySourceType -
用于根据使用计划对请求进行计量的 API 键的源。有效值为
HEADER和AUTHORIZER。类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
ApiKeySourceType的财产AWS::ApiGateway::RestApi资源。 -
Auth -
配置授权,以控制对 API Gateway API 的访问。
有关使用配置访问权限的更多信息 Amazon SAM 看到 使用您的控制API访问权限 Amazon SAM 模板。有关如何覆盖全局授权方的示例,请参阅 覆盖 Amazon API Gateway REST API 的全局授权方。
类型:ApiAuth
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
BinaryMediaTypes -
您的 API 可能返回的 MIME 类型列表。使用它来启用 API 的二进制支持。
类型:列表
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
BinaryMediaTypes的财产AWS::ApiGateway::RestApi资源。的清单 BinaryMediaTypes 被添加到两者中 Amazon CloudFormation 资源和 OpenAPI 文档。 -
CacheClusterEnabled -
指示是否为阶段启用缓存。要缓存响应,还必须在
MethodSettings下将CachingEnabled设置为true。类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
CacheClusterEnabled的财产AWS::ApiGateway::Stage资源。 -
CacheClusterSize -
阶段的缓存群集大小。
类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
CacheClusterSize的财产AWS::ApiGateway::Stage资源。 -
CanarySetting -
将金丝雀设置配置为常规部署的某个阶段。
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
CanarySetting的财产AWS::ApiGateway::Stage资源。 -
Cors -
管理 Cross-origin 所有 API 网关 API 的资源共享 (CORS)。使用字符串指定允许使用的域,或使用其他 Cors 配置指定词典。
注意CORS 需要 Amazon SAM 修改你的 OpenAPI 定义。在
DefinitionBody中创建内联 OpenAPI 定义以开启 CORS。有关 CORS 的更多信息,请参阅《API Gateway 开发人员指南》中的为 API Gateway REST API 资源启用 CORS。
类型: 字符串 | CorsConfiguration
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
DefinitionBody -
描述您的 API 的 OpenAPI 规范。如果
DefinitionUri和DefinitionBody均未指定,SAM 将根据模板配置为您生成DefinitionBody。要引用定义 API 的本地 OpenAPI 文件,请使用
AWS::Include转换。要了解更多信息,请参阅如何 Amazon SAM 上传本地文件。类型:JSON
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
Body的财产AWS::ApiGateway::RestApi资源。如果提供了某些属性,则可以将内容插入或修改到 DefinitionBody 在传递给之前 CloudFormation。属性包括Auth、BinaryMediaTypes、Cors、GatewayResponses、Models、和AWS::Serverless::Function相应 Api 类型的EventSource。 -
DefinitionUri -
Amazon S3 Uri、本地文件路径或定义 API 的 OpenAPI 文档的位置对象。此属性引用的 Amazon S3 对象必须是有效的 OpenAPI 文件。如果
DefinitionUri和DefinitionBody均未指定,SAM 将根据模板配置为您生成DefinitionBody。如果提供了本地文件路径,则模板必须经过包含
sam deploy或sam package命令的工作流程,才能使定义正确转换。外部不支持内部函数 OpenApi 引用的文件
DefinitionUri。改用DefinitionBody的财产 包括转换 导入 OpenApi 定义到模板中。类型: 字符串 | ApiDefinition
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
BodyS3Location的财产AWS::ApiGateway::RestApi资源。嵌套的 Amazon S3 属性的命名有所不同。 -
Description -
Api 资源的描述。
类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
Description的财产AWS::ApiGateway::RestApi资源。 -
DisableExecuteApiEndpoint -
指定客户端是否可以使用默认
execute-api端点调用您的 API。默认情况下,客户端可以使用默认https://{api_id}.execute-api.{region}.amazonaws.com调用您的 API。如果要求客户端使用自定义域名来调用 API,请指定True。类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
DisableExecuteApiEndpoint的财产AWS::ApiGateway::RestApi资源。它直接传递给x-amazon-apigateway-endpoint-configuration扩展的disableExecuteApiEndpoint属性,然后添加到AWS::ApiGateway::RestApi资源的Body属性中。 -
Domain -
为此 API Gateway API 配置自定义域。
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
EndpointConfiguration -
REST API 的端点类型。
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
EndpointConfiguration的财产AWS::ApiGateway::RestApi资源。嵌套配置属性的命名有所不同。 -
EndpointAccessMode -
REST API 端点的访问模式。使用增强安全策略(前缀为
SecurityPolicy_)。有效值:
STRICT或BASIC类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
EndpointAccessMode的财产AWS::ApiGateway::RestApi资源。 -
FailOnWarnings -
指定在遇到警告时是回滚 (
true) 还是不回滚 (false) API 创建。默认值为false。类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
FailOnWarnings的财产AWS::ApiGateway::RestApi资源。 -
GatewayResponses -
为 API 配置网关响应。网关响应是 API Gateway 直接返回或使用 Lambda 授权方返回的响应。有关更多信息,请参阅的文档 Api 网关 OpenApi 网关响应的扩展。
类型:映射
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
MergeDefinitions -
Amazon SAM 生成一个 OpenAPI 来自您的 API 事件源的规范。请指定
true拥有 Amazon SAM 将其合并到内联中 OpenAPI 您的规格中定义AWS::Serverless::Api资源。指定false以不合并。MergeDefinitions需要定义AWS::Serverless::Api的DefinitionBody属性。MergeDefinitions与AWS::Serverless::Api的DefinitionUri属性不兼容。默认值:
false类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
MethodSettings -
配置 API 阶段的所有设置,包括日志、指标、CacheTTL、节流。
类型: MethodSetting 列表
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
MethodSettings的财产AWS::ApiGateway::Stage资源。 -
MinimumCompressionSize -
允许根据客户的要求压缩响应正文 Accept-Encoding 标题。当响应正文大小大于或等于您配置的阈值时,就会触发压缩。最大正文大小阈值为 10 MB(10,485,760 字节)。- 支持以下压缩类型:gzip、deflate 和身份。
类型:整数
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
MinimumCompressionSize的财产AWS::ApiGateway::RestApi资源。 -
Mode -
此属性仅在您使用 OpenAPI 定义 REST API 时才适用。
Mode确定 API Gateway 如何处理资源更新。有关更多信息,请参阅 AWS::ApiGateway::RestApi 资源类型的模式属性。有效值:
overwrite或merge类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
Mode的财产AWS::ApiGateway::RestApi资源。 -
Models -
您的 API 方法要使用的架构。可以使用 JSON 或 YAML 描述这些架构。有关示例模型,请参阅本页底部的“示例”部分。
类型:映射
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
Name -
API 网关的名称 RestApi 资源
类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
Name的财产AWS::ApiGateway::RestApi资源。 -
OpenApiVersion -
的版本 OpenApi 使用。这可以是
2.0适用于 Swagger 规范,或其中一个 OpenApi 3.0 版本,比如3.0.1。有关 OpenAPI 的更多信息,请参阅 OpenAPI 规范。 注意Amazon SAM 创建一个名为的阶段
Stage默认情况下。将此属性设置为任何有效值将阻止阶段Stage的创建。类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
-
指明是否将
Tags属性中的标签传递给 AWS::Serverless::Api 生成的资源。指定True以在生成的资源中传播标签。类型:布尔值
必需:否
默认值:
FalseAmazon CloudFormation 兼容性: 此属性是独有的 Amazon SAM 而且没有 Amazon CloudFormation 等效。
Policy-
一个策略文档,其中包含 API 的权限。要设置策略的 ARN,请使用
!Join内部函数(""作为分隔符)以及值"execute-api:/"和"*"。类型:JSON
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给 政策 的财产
AWS::ApiGateway::RestApi资源。 -
SecurityPolicy -
TLS 版本加上 REST API 的密码套件。使用增强安全策略时(前缀为
SecurityPolicy>),你还必须设置EndpointAccessMode。类型:字符串
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
SecurityPolicy的财产AWS::ApiGateway::RestApi资源。 -
StageName -
阶段的名称,API Gateway 将它用作调用的统一资源标识符 (URI) 中的第一个路径部分。
要引用阶段资源,请使用
。有关指定 AWS::Serverless:: Api 资源时引用生成资源的更多信息,请参阅 Amazon CloudFormation 指定时AWS::Serverless::Api生成的资源。有关生成的基本信息 Amazon CloudFormation 资源,请参阅 生成 Amazon CloudFormation 的资源 Amazon SAM。<api-logical-id>.Stage类型:字符串
是否必需:是
Amazon CloudFormation 兼容性: 此属性类似于
StageName的财产AWS::ApiGateway::Stage资源。它在 SAM 中为必需,但在 API Gateway 中并非必需附加说明:隐式 API 的阶段名称为 “Prod”。
-
指定要添加到此 API Gateway 阶段的标签的映射(字符串到字符串)。有关标签的有效键和值的详细信息,请参阅《Amazon CloudFormation 用户指南》中的资源标签。
类型:映射
必需:否
Amazon CloudFormation 兼容性: 此属性类似于
Tags的财产AWS::ApiGateway::Stage资源。SAM 中的标签属性包括 Key:Value 对;在 CloudFormation 它由标签对象列表组成。 -
TracingEnabled -
表示活动跟踪是否使用 X-Ray 已为舞台启用。有关以下内容的更多信息 X-Ray,参见 使用跟踪用户对 REST API 的请求 X-Ray 在 API 网关开发者指南。
类型:布尔值
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
TracingEnabled的财产AWS::ApiGateway::Stage资源。 -
Variables -
一个定义阶段变量的映射 (字符串到字符串),其中变量名作为键,变量值作为值。变量名称只能包含字母数字字符。值必须匹配以下正则表达式:
[A-Za-z0-9._~:/?#&=,-]+。类型:映射
必需:否
Amazon CloudFormation 兼容性: 此属性直接传递给
Variables的财产AWS::ApiGateway::Stage资源。
返回值
Ref
当向 Ref 内置函数提供此资源的逻辑 ID 时,它将返回底层 API Gateway API 的 ID。
有关使用 Ref 函数的更多信息,请参阅《Amazon CloudFormation 用户指南》中的 Ref。
Fn::GetAtt
Fn::GetAtt 返回一个此类型指定属性的值。以下为可用属性和示例返回值。
有关使用 Fn::GetAtt 的更多信息,请参阅《Amazon CloudFormation 用户指南》中的 Fn::GetAtt。
RootResourceId-
RestApi资源的根资源 ID,例如a0bc123d4e。
示例
SimpleApiExample
你好 World Amazon SAM 模板文件,其中包含带有 API 端点的 Lambda 事件函数。这是完整的 Amazon SAM 正在运行的无服务器应用程序的模板文件。
YAML
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Amazon SAM template with a simple API definition Resources: ApiGatewayApi: Type: AWS::Serverless::Api Properties: StageName: prod ApiFunction: # Adds a GET method at the root resource via an Api event Type: AWS::Serverless::Function Properties: Events: ApiEvent: Type: Api Properties: Path: / Method: get RestApiId: Ref: ApiGatewayApi Runtime: python3.10 Handler: index.handler InlineCode: | def handler(event, context): return {'body': 'Hello World!', 'statusCode': 200}
ApiCorsExample
一个 Amazon SAM 模板片段,其中包含在外部 Swagger 文件中定义的 API 以及 Lambda 集成和 CORS 配置。这只是其中的一部分 Amazon SAM 模板文件显示 AWS::Serverless:: Api 定义。
YAML
Resources: ApiGatewayApi: Type: AWS::Serverless::Api Properties: StageName: Prod # Allows www.example.com to call these APIs # SAM will automatically add AllowMethods with a list of methods for this API Cors: "'www.example.com'" DefinitionBody: # Pull in an OpenApi definition from S3 'Fn::Transform': Name: 'AWS::Include' # Replace "bucket" with your bucket name Parameters: Location: s3://bucket/swagger.yaml
ApiCognitoAuthExample
一个 Amazon SAM 模板代码片段,其中包含一个使用亚马逊 Cognito 来授权针对该 API 的请求的 API。这只是其中的一部分 Amazon SAM 模板文件显示 AWS::Serverless:: Api 定义。
YAML
Resources: ApiGatewayApi: Type: AWS::Serverless::Api Properties: StageName: Prod Cors: "'*'" Auth: DefaultAuthorizer: MyCognitoAuthorizer Authorizers: MyCognitoAuthorizer: UserPoolArn: Fn::GetAtt: [MyCognitoUserPool, Arn]
ApiModelsExample
一个 Amazon SAM 带有包含模型架构的 API 的模板片段。这只是其中的一部分 Amazon SAM 模板文件,显示一个 AWS::Serverless:: Api 使用两个模型架构进行定义。
YAML
Resources: ApiGatewayApi: Type: AWS::Serverless::Api Properties: StageName: Prod Models: User: type: object required: - username - employee_id properties: username: type: string employee_id: type: integer department: type: string Item: type: object properties: count: type: integer category: type: string price: type: integer
缓存示例
你好 World Amazon SAM 模板文件,其中包含带有 API 端点的 Lambda 函数。API 已为一种资源和方法启用缓存。有关缓存的更多信息,请参阅的《API Gateway 开发人员指南》中的启用 API 缓存以增强响应能力。
YAML
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: AWS SAM template with a simple API definition with caching turned on Resources: ApiGatewayApi: Type: AWS::Serverless::Api Properties: StageName: prod CacheClusterEnabled: true CacheClusterSize: '0.5' MethodSettings: - ResourcePath: / HttpMethod: GET CachingEnabled: true CacheTtlInSeconds: 300 Tags: CacheMethods: All ApiFunction: # Adds a GET method at the root resource via an Api event Type: AWS::Serverless::Function Properties: Events: ApiEvent: Type: Api Properties: Path: / Method: get RestApiId: Ref: ApiGatewayApi Runtime: python3.10 Handler: index.handler InlineCode: | def handler(event, context): return {'body': 'Hello World!', 'statusCode': 200}
带私有 API 的自定义域示例
你好 World Amazon SAM 模板文件,其中包含 Lambda 函数,其 API 终端节点映射到私有域。该模板在 VPC 端点与私有域之间创建域访问关联。有关更多信息,请参阅 API 网关中私有 API 的自定义域名。
YAML
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: AWS SAM template configured with a custom domain using a private API Parameters: DomainName: Type: String Default: mydomain.example.com CertificateArn: Type: String HostedZoneId: Type: String VpcEndpointId: Type: String VpcEndpointDomainName: Type: String VpcEndpointHostedZoneId: Type: String Resources: MyFunction: Type: AWS::Serverless::Function Properties: InlineCode: | def handler(event, context): return {'body': 'Hello World!', 'statusCode': 200} Handler: index.handler Runtime: python3.13 Events: Fetch: Type: Api Properties: RestApiId: Ref: MyApi Method: Get Path: /get MyApi: Type: AWS::Serverless::Api Properties: StageName: Prod EndpointConfiguration: Type: PRIVATE VPCEndpointIds: - !Ref VpcEndpointId Policy: Version: '2012-10-17' Statement: - Effect: Allow Principal: '*' Action: execute-api:Invoke Resource: execute-api:/* - Effect: Deny Principal: '*' Action: execute-api:Invoke Resource: execute-api:/* Condition: StringNotEquals: aws:SourceVpce: !Ref VpcEndpointId Domain: DomainName: !Ref DomainName CertificateArn: !Ref CertificateArn EndpointConfiguration: PRIVATE BasePath: - / Route53: HostedZoneId: !Ref HostedZoneId VpcEndpointDomainName: !Ref VpcEndpointDomainName VpcEndpointHostedZoneId: !Ref VpcEndpointHostedZoneId AccessAssociation: VpcEndpointId: !Ref VpcEndpointId Policy: Version: '2012-10-17' Statement: - Effect: Allow Principal: '*' Action: execute-api:Invoke Resource: execute-api:/* - Effect: Deny Principal: '*' Action: execute-api:Invoke Resource: execute-api:/* Condition: StringNotEquals: aws:SourceVpce: !Ref VpcEndpointId