本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
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 資源。
我們建議您使用 CloudFormation 勾點或 IAM 政策來驗證 API Gateway 資源是否已連接授權方,以控制對它們的存取。
如需使用 CloudFormation 勾點的詳細資訊,請參閱 CloudFormation CLI 使用者指南中的註冊勾點和 apigw-enforce-authorizer
如需使用 IAM 政策的詳細資訊,請參閱《 API Gateway 開發人員指南》中的要求 API 路由具有授權。
注意
當您部署到 時 AWS CloudFormation, 會將您的 AWS SAM 資源 AWS SAM 轉換為 CloudFormation 資源。如需詳細資訊,請參閱產生的 CloudFormation 資源 AWS SAM。
語法
若要在 AWS Serverless Application Model (AWS SAM) 範本中宣告此實體,請使用下列語法。
YAML
Type: AWS::Serverless::Api Properties: AccessLogSetting:AccessLogSettingAlwaysDeploy:BooleanApiKeySourceType:StringAuth:ApiAuthBinaryMediaTypes:ListCacheClusterEnabled:BooleanCacheClusterSize:StringCanarySetting:CanarySettingCors:String | CorsConfigurationDefinitionBody:JSONDefinitionUri:String | ApiDefinitionDescription:StringDisableExecuteApiEndpoint:BooleanDomain:DomainConfigurationEndpointConfiguration:EndpointConfigurationFailOnWarnings:BooleanGatewayResponses:MapMergeDefinitions:BooleanMethodSettings:MethodSettingsMinimumCompressionSize:IntegerMode:StringModels:MapName:StringOpenApiVersion:StringPropagateTags:BooleanPolicy:JSONStageName:StringTags:MapTracingEnabled:BooleanVariables:Map
屬性
-
AccessLogSetting -
設定階段的存取日誌設定。
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::Stage資源的AccessLogSetting屬性。 -
AlwaysDeploy -
即使未偵測到 API 的變更,也一律部署 API。
類型:布林值
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等的。
-
ApiKeySourceType -
根據用量計畫進行量測請求的 API 金鑰來源,有效值為
HEADER和AUTHORIZER。類型:字串
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的ApiKeySourceType屬性。 -
Auth -
設定授權以控制對 API Gateway API 的存取。
如需使用 設定存取權的詳細資訊, AWS SAM 請參閱 使用 AWS SAM 範本控制 API 存取。如需示範如何覆寫全域授權方的範例,請參閱 覆寫 Amazon API Gateway REST API 的全域授權方。
類型:ApiAuth
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等的。
-
BinaryMediaTypes -
您的 API 可能傳回的 MIME 類型清單。使用此項目可啟用 APIs 的二進位支援。
類型:列出
必要:否
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::RestApi資源的BinaryMediaTypes屬性。BinaryMediaTypes 的清單會同時新增至 CloudFormation 資源和 OpenAPI 文件。 -
CacheClusterEnabled -
指出是否已為階段啟用快取。若要快取回應,您還必須在
true下CachingEnabled將 設定為MethodSettings。類型:布林值
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::Stage資源的CacheClusterEnabled屬性。 -
CacheClusterSize -
階段的快取叢集大小。
類型:字串
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::Stage資源的CacheClusterSize屬性。 -
CanarySetting -
將 Canary 設定設定為一般部署的階段。
必要:否
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
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等屬性。
-
DefinitionBody -
描述 API 的 OpenAPI 規格。如果既未指定
DefinitionUri也DefinitionBody未指定,SAM 會根據您的範本組態DefinitionBody為您產生 。若要參考定義 API 的本機OpenAPI檔案,請使用
AWS::Include轉換。如需詳細資訊,請參閱 如何 AWS SAM 上傳本機檔案。類型:JSON
必要:否
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::RestApi資源的Body屬性。如果提供特定屬性,內容可能會在傳遞至 CloudFormation 之前插入或修改至 DefinitionBody。屬性包括對應EventSource的Auth、BinaryMediaTypes、Models、CorsGatewayResponses和 Api 類型 的AWS::Serverless::Function。 -
DefinitionUri -
定義 API 之 OpenAPI 文件的 Amazon S3 Uri、本機檔案路徑或位置物件。此屬性參考的 Amazon S3 物件必須是有效的 OpenAPI 檔案。如果既未指定
DefinitionUri也DefinitionBody未指定,SAM 會根據您的範本組態DefinitionBody為您產生 。如果提供本機檔案路徑,範本必須經過包含
sam deploy或sam package命令的工作流程,才能正確轉換定義。參考的外部 OpenApi 檔案不支援內部函數
DefinitionUri。使用DefinitionBody屬性搭配包含轉換,將 OpenApi 定義匯入範本。類型:字串 | ApiDefinition
必要:否
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::RestApi資源的BodyS3Location屬性。巢狀 Amazon S3 屬性的名稱不同。 -
Description -
Api 資源的描述。
類型:字串
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的Description屬性。 -
DisableExecuteApiEndpoint -
指定用戶端是否可以使用預設
execute-api端點叫用您的 API。根據預設,用戶端可以使用預設的 叫用您的 APIhttps://{api_id}.execute-api.{region}.amazonaws.com。如要要求用戶端使用自訂網域名稱來叫用 API,請指定True。類型:布林值
必要:否
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::RestApi資源的DisableExecuteApiEndpoint屬性。它會直接傳遞至x-amazon-apigateway-endpoint-configuration延伸的disableExecuteApiEndpoint屬性,該延伸會新增至AWS::ApiGateway::RestApi資源的Body屬性。 -
Domain -
設定此 API Gateway API 的自訂網域。
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等屬性。
-
EndpointConfiguration -
REST API 的端點類型。
必要:否
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::RestApi資源的EndpointConfiguration屬性。巢狀組態屬性的名稱不同。 -
FailOnWarnings -
指定在遇到警告時是否要轉返 API 建立 (
true) 與否 (false)。預設值為false。類型:布林值
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的FailOnWarnings屬性。 -
GatewayResponses -
設定 API 的閘道回應。閘道回應是由 API Gateway 直接或透過使用 Lambda 授權方傳回的回應。如需詳細資訊,請參閱適用於閘道回應的 Api Gateway OpenApi 擴充功能文件。
類型:映射
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 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類型:布林值
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等的。
-
MethodSettings -
設定 API 階段的所有設定,包括記錄、指標、CacheTTL、調節。
類型:MethodSetting 的清單
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::Stage資源的MethodSettings屬性。 -
MinimumCompressionSize -
允許根據用戶端的 Accept-Encoding 標頭壓縮回應內文。當回應內文大小大於或等於您設定的閾值時,就會觸發壓縮。主體大小閾值上限為 10 MB (10,485,760 個位元組)。- 支援下列壓縮類型:gzip、deflate 和 identity。
類型:整數
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的MinimumCompressionSize屬性。 -
Mode -
只有當您使用 OpenAPI 定義 REST API 時,才會套用此屬性。
Mode可判定 API Gateway 如何處理資源更新。如需詳細資訊,請參閱 AWS::ApiGateway::RestApi 資源類型的模式屬性。有效值:
overwrite或merge類型:字串
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的Mode屬性。 -
Models -
您的 API 方法要使用的結構描述。您可以使用 JSON 或 YAML 描述這些結構描述。如需範例模型,請參閱此頁面底部的範例一節。
類型:映射
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等的。
-
Name -
API Gateway RestApi 資源的名稱
類型:字串
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的Name屬性。 -
OpenApiVersion -
要使用的 OpenApi 版本。這可以是
2.0Swagger 規格,或其中一個 OpenApi 3.0 版本,例如3.0.1。如需 OpenAPI 的詳細資訊,請參閱 OpenAPI 規格。 注意
AWS SAM 根據預設, 會建立名為
Stage的階段。將此屬性設定為任何有效值,將導致無法建立階段Stage。類型:字串
必要:否
CloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等屬性。
-
指出是否要將標籤從
Tags屬性傳遞至您AWS::Serverless::Api產生的資源。指定True在產生的資源中傳播標籤。類型:布林值
必要:否
預設:
FalseCloudFormation 相容性:此屬性對 是唯一的 AWS SAM ,並且沒有 CloudFormation 同等的。
Policy-
包含 API 許可的政策文件。若要設定政策的 ARN,請使用
!Join內部函數搭配""做為分隔符號,並使用值"execute-api:/"和"*"。類型:JSON
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::RestApi資源的 政策屬性。 -
StageName -
API Gateway 做為叫用統一資源識別符 (URI) 中第一個路徑區段的階段名稱。
若要參考階段資源,請使用
。如需參考指定資源時產生的AWS::Serverless::Api資源的詳細資訊,請參閱 CloudFormationAWS::Serverless::Api指定 時產生的資源。如需所產生 CloudFormation 資源的一般資訊,請參閱 產生的 CloudFormation 資源 AWS SAM。<api-logical-id>.Stage類型:字串
必要:是
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::Stage資源的StageName屬性。在 SAM 中為必要,但在 API Gateway 中為非必要其他備註:隱含 API 的階段名稱為 "Prod"。
-
映射 (字串到字串),指定要新增至此 API Gateway 階段的標籤。如需標籤有效金鑰和值的詳細資訊,請參閱AWS CloudFormation 《 使用者指南》中的資源標籤。
類型:映射
必要:否
CloudFormation 相容性:此屬性類似於
AWS::ApiGateway::Stage資源的Tags屬性。SAM 中的標籤屬性包含 Key:Value 對;在 CloudFormation 中包含標籤物件清單。 -
TracingEnabled -
指出是否已啟用階段的 X-Ray 主動追蹤。如需 X-Ray 的詳細資訊,請參閱《 APIs Gateway 開發人員指南》中的使用 X-Ray 追蹤 REST API 的使用者請求。
類型:布林值
必要:否
CloudFormation 相容性:此屬性會直接傳遞至
AWS::ApiGateway::Stage資源的TracingEnabled屬性。 -
Variables -
定義階段變數的映射 (字串到字串),其中變數名稱是索引鍵,而變數值是值。變數名稱限制為英數字元。值必須符合下列規則表達式:
[A-Za-z0-9._~:/?#&=,-]+。類型:映射
必要:否
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