AWS::Serverless::Api - AWS Serverless Application Model

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS::Serverless::Api

建立可透過 HTTPS 端點叫用之 Amazon API Gateway 資源和方法的集合。

AWS::Serverless::Api 資源不需要明確新增至無 AWS 伺服器應用程式定義範本。此類型的資源是從在範本中未參考 AWS::Serverless::Api 資源所定義AWS::Serverless::Function資源上定義的 Api 事件聯集隱含建立的。

應使用 AWS::Serverless::Api 資源來定義和記錄使用 OpenApi 的 API,以便更能夠設定基礎 Amazon API Gateway 資源。

我們建議您使用 AWS CloudFormation 勾點或 IAM 政策來驗證 API Gateway 資源是否已連接授權方,以控制對它們的存取。

如需使用 AWS CloudFormation 勾點的詳細資訊,請參閱 AWS CloudFormation CLI 使用者指南中的註冊勾點apigw-enforce-authorizer GitHub 儲存庫。

如需使用 IAM 政策的詳細資訊,請參閱《 API Gateway 開發人員指南》中的要求 API 路由具有授權

注意

當您部署到 時 AWS CloudFormation, 會將您的 AWS SAM 資源 AWS SAM 轉換為 AWS CloudFormation 資源。如需詳細資訊,請參閱產生的 AWS CloudFormation 資源 AWS SAM

語法

若要在 AWS Serverless Application Model (AWS SAM) 範本中宣告此實體,請使用下列語法。

屬性

AccessLogSetting

設定階段的存取日誌設定。

類型AccessLogSetting

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 AccessLogSetting 屬性。

AlwaysDeploy

即使未偵測到 API 的變更,也一律部署 API。

類型:布林值

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

ApiKeySourceType

根據用量計畫進行量測請求的 API 金鑰來源,有效值為 HEADERAUTHORIZER

類型:字串

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 ApiKeySourceType 屬性。

Auth

設定授權以控制對 API Gateway API 的存取。

如需使用 設定存取權的詳細資訊, AWS SAM 請參閱 使用 AWS SAM 範本控制 API 存取。如需示範如何覆寫全域授權方的範例,請參閱 覆寫 Amazon API Gateway REST API 的全域授權方

類型ApiAuth

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

BinaryMediaTypes

您的 API 可能傳回的 MIME 類型清單。使用此項目可啟用 APIs 的二進位支援。

類型:列出

必要:否

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::RestApi 資源的 BinaryMediaTypes 屬性。BinaryMediaTypes 的清單會同時新增至 AWS CloudFormation 資源和 OpenAPI 文件。

CacheClusterEnabled

指出是否已為階段啟用快取。若要快取回應,您還必須在 trueCachingEnabled將 設定為 MethodSettings

類型:布林值

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 CacheClusterEnabled 屬性。

CacheClusterSize

階段的快取叢集大小。

類型:字串

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 CacheClusterSize 屬性。

CanarySetting

將 Canary 設定設定為一般部署的階段。

類型CanarySetting

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 CanarySetting 屬性。

Cors

管理所有 API Gateway APIs 的跨來源資源共用 (CORS)。指定要允許做為字串的網域,或指定具有其他 Cors 組態的字典。

注意

CORS 需要 AWS SAM 修改您的 OpenAPI 定義。在 中建立內嵌 OpenAPI 定義DefinitionBody以開啟 CORS。

如需 CORS 的詳細資訊,請參閱《 API Gateway 開發人員指南》中的為 API Gateway REST API 資源啟用 CORS

類型:字串 | CorsConfiguration

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等屬性。

DefinitionBody

描述 API 的 OpenAPI 規格。如果未指定 DefinitionUriDefinitionBody ,SAM 會根據您的範本組態DefinitionBody為您產生 。

若要參考定義 API 的本機OpenAPI檔案,請使用 AWS::Include轉換。如需詳細資訊,請參閱 如何 AWS SAM 上傳本機檔案

類型:JSON

必要:否

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::RestApi 資源的 Body 屬性。如果提供特定屬性,內容可能會在傳遞至 CloudFormation 之前插入或修改至 DefinitionBody。屬性包括 AuthBinaryMediaTypesModels、、CorsGatewayResponses,以及對應 EventSource的 Api 類型 AWS::Serverless::Function

DefinitionUri

定義 API 之 OpenAPI 文件的 Amazon S3 Uri、本機檔案路徑或位置物件。此屬性參考的 Amazon S3 物件必須是有效的 OpenAPI 檔案。如果未指定 DefinitionUriDefinitionBody ,SAM 會根據您的範本組態DefinitionBody為您產生 。

如果提供本機檔案路徑,範本必須經過包含 sam deploysam package命令的工作流程,才能正確轉換定義。

參考的外部 OpenApi 檔案不支援內部函數DefinitionUri。使用 DefinitionBody 屬性搭配包含轉換,將 OpenApi 定義匯入範本。

類型:字串 | ApiDefinition

必要:否

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::RestApi 資源的 BodyS3Location 屬性。巢狀 Amazon S3 屬性的名稱不同。

Description

Api 資源的描述。

類型:字串

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 Description 屬性。

DisableExecuteApiEndpoint

指定用戶端是否可以使用預設 execute-api 端點叫用您的 API。根據預設,用戶端可以使用預設的 叫用您的 APIhttps://{api_id}.execute-api.{region}.amazonaws.com。如要要求用戶端使用自訂網域名稱來叫用 API,請指定 True

類型:布林值

必要:否

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::RestApi 資源的 DisableExecuteApiEndpoint 屬性。它會直接傳遞至 x-amazon-apigateway-endpoint-configuration延伸的 disableExecuteApiEndpoint 屬性,該延伸會新增至 AWS::ApiGateway::RestApi 資源的 Body 屬性。

Domain

設定此 API Gateway API 的自訂網域。

類型DomainConfiguration

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

EndpointConfiguration

REST API 的端點類型。

類型EndpointConfiguration

必要:否

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::RestApi 資源的 EndpointConfiguration 屬性。巢狀組態屬性的名稱不同。

FailOnWarnings

指定在遇到警告時是否要轉返 API 建立 (true) 與否 (false)。預設值為 false

類型:布林值

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 FailOnWarnings 屬性。

GatewayResponses

設定 API 的閘道回應。閘道回應是由 API Gateway 直接或透過使用 Lambda 授權方傳回的回應。如需詳細資訊,請參閱適用於閘道回應的 Api Gateway OpenApi 擴充功能文件。

類型:映射

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,且沒有 AWS CloudFormation 同等屬性。

MergeDefinitions

AWS SAM 會從 API 事件來源產生OpenAPI規格。指定 AWS SAM true讓 將此合併到 AWS::Serverless::Api 資源中定義的內嵌OpenAPI規格。指定 false不合併。

MergeDefinitions 需要AWS::Serverless::Api定義 的 DefinitionBody 屬性。 MergeDefinitions 與 的 DefinitionUri 屬性不相容AWS::Serverless::Api

預設值false

類型:布林值

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

MethodSettings

設定 API 階段的所有設定,包括記錄、指標、CacheTTL、調節。

類型MethodSetting 的清單

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 MethodSettings 屬性。

MinimumCompressionSize

允許根據用戶端的 Accept-Encoding 標頭壓縮回應內文。當回應內文大小大於或等於您設定的閾值時,就會觸發壓縮。主體大小閾值上限為 10 MB (10,485,760 個位元組)。- 支援下列壓縮類型:gzip、deflate 和 identity。

類型:整數

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 MinimumCompressionSize 屬性。

Mode

只有當您使用 OpenAPI 定義 REST API 時,才會套用此屬性。Mode 可判定 API Gateway 如何處理資源更新。如需詳細資訊,請參閱 AWS::ApiGateway::RestApi 資源類型的模式屬性。

有效值overwritemerge

類型:字串

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 Mode 屬性。

Models

您的 API 方法要使用的結構描述。您可以使用 JSON 或 YAML 描述這些結構描述。如需範例模型,請參閱此頁面底部的範例一節。

類型:映射

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

Name

API Gateway RestApi 資源的名稱

類型:字串

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 Name 屬性。

OpenApiVersion

要使用的 OpenApi 版本。這可以是2.0用於 Swagger 規格,或其中一個 OpenApi 3.0 版本,例如 3.0.1。如需 OpenAPI 的詳細資訊,請參閱 OpenAPI 規格

注意

AWS SAM 根據預設, 會建立名為 Stage 的階段。將此屬性設定為任何有效值將阻止建立階段 Stage

類型:字串

必要:否

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

PropagateTags

指出是否要將標籤從 Tags 屬性傳遞至您AWS::Serverless::Api產生的資源。指定 True 在產生的資源中傳播標籤。

類型:布林值

必要:否

預設False

AWS CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 AWS CloudFormation 同等的。

Policy

包含 API 許可的政策文件。若要設定政策的 ARN,請使用 !Join 內部函數搭配 "" 做為分隔符號,並使用值 "execute-api:/""*"

類型:JSON

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::RestApi 資源的 政策屬性。

StageName

API Gateway 做為叫用統一資源識別符 (URI) 中第一個路徑區段的階段名稱。

若要參考階段資源,請使用 <api-logical-id>.Stage。如需參考指定資源時產生的AWS::Serverless::Api資源的詳細資訊,請參閱 AWS CloudFormationAWS::Serverless::Api指定 時產生的資源。如需所產生 AWS CloudFormation 資源的一般資訊,請參閱 產生的 AWS CloudFormation 資源 AWS SAM

類型:字串

必要:是

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::Stage 資源的 StageName 屬性。在 SAM 中為必要,但在 API Gateway 中為非必要

其他備註:隱含 API 的階段名稱為 "Prod"。

Tags

映射 (字串到字串),指定要新增至此 API Gateway 階段的標籤。如需標籤有效金鑰和值的詳細資訊,請參閱AWS CloudFormation 《 使用者指南》中的資源標籤

類型:映射

必要:否

AWS CloudFormation 相容性:此屬性類似於 AWS::ApiGateway::Stage 資源的 Tags 屬性。SAM 中的標籤屬性包含 Key:Value 對;在 CloudFormation 中包含標籤物件清單。

TracingEnabled

指出是否已針對階段啟用使用 X-Ray 的主動追蹤。如需 X-Ray 的詳細資訊,請參閱《 APIs Gateway 開發人員指南》中的使用 X-Ray 追蹤使用者對 REST API 的請求

類型:布林值

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 TracingEnabled 屬性。

Variables

定義階段變數的映射 (字串到字串),其中變數名稱是索引鍵,而變數值是值。變數名稱限制為英數字元。值必須符合下列規則表達式:[A-Za-z0-9._~:/?#&=,-]+

類型:映射

必要:否

AWS CloudFormation 相容性:此屬性會直接傳遞至 AWS::ApiGateway::Stage 資源的 Variables 屬性。

傳回值

Ref

當將此資源的邏輯 ID 提供給Ref內部 函數時,它會傳回基礎 API Gateway API 的 ID。

如需使用 Ref函數的詳細資訊,請參閱AWS CloudFormation 《 使用者指南Ref》中的 。

Fn::GetAtt

Fn::GetAtt 會傳回此類型之指定屬性的值。以下為可用屬性及傳回值的範例。

如需使用 的詳細資訊Fn::GetAtt,請參閱AWS CloudFormation 《 使用者指南Fn::GetAtt》中的 。

RootResourceId

RestApi 資源的根資源 ID,例如:a0bc123d4e

範例

SimpleApiExample

Hello World AWS SAM 範本檔案,其中包含具有 API 端點的 Lambda 函數。這是運作中無伺服器應用程式的完整 AWS SAM 範本檔案。

YAML

AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: AWS 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

具有外部 Swagger 檔案中定義之 API 的 AWS SAM 範本程式碼片段,以及 Lambda 整合和 CORS 組態。這只是 AWS 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

具有 API 的 AWS SAM 範本程式碼片段,該 API 使用 Amazon Cognito 來授權對 API 的請求。這只是 AWS 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

包含模型結構描述之 API 的 AWS SAM 範本程式碼片段。這只是 AWS 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

快取範例

Hello World AWS 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 範例的自訂網域

Hello World AWS SAM 範本檔案,其中包含與私有網域對應之 API 端點的 Lambda 函數。範本會在 VPC 端點與私有網域之間建立網域存取關聯。如需詳細資訊,請參閱 API Gateway 中私有 APIs的自訂網域名稱

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