本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
模板的全局部分 AWS SAM
有時,您在AWS SAM模板中聲明的資源具有通用配置。例如,您的應用程式可能包含多個具有相同Runtime
、、Memory
VPCConfig
Environment
、和Cors
組態的AWS::Serverless::Function
資源。您可以在Globals
部分中聲明一次,並讓您的資源繼承它們,而不是在每個資源中複製這些信息。
此Globals
區段支援下列AWS SAM資源類型:
-
AWS::Serverless::Api
-
AWS::Serverless::Function
-
AWS::Serverless::HttpApi
-
AWS::Serverless::SimpleTable
-
AWS::Serverless::StateMachine
範例:
Globals: Function: Runtime: nodejs12.x Timeout: 180 Handler: index.handler Environment: Variables: TABLE_NAME: data-table Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: Environment: Variables: MESSAGE: "Hello From SAM" ThumbnailFunction: Type: AWS::Serverless::Function Properties: Events: Thumbnail: Type: Api Properties: Path: /thumbnail Method: POST
在這個例子中,HelloWorldFunction
並ThumbnailFunction
使用「nodejs12.x」表示Runtime
,「180」秒代表Timeout
,並使用「索引。Handler
HelloWorldFunction
除了繼承的 TABLE_NAME 之外,還會新增訊息環境變數。 ThumbnailFunction
繼承所有Globals
屬性並添加 API 事件源。
支援的資源和屬性
AWS SAM支援下列資源和屬性。
Globals: Api: AccessLogSetting: Auth: BinaryMediaTypes: CacheClusterEnabled: CacheClusterSize: CanarySetting: Cors: DefinitionUri: Domain: EndpointConfiguration: GatewayResponses: MethodSettings: MinimumCompressionSize: Name: OpenApiVersion: PropagateTags: TracingEnabled: Variables: Function: Architectures: AssumeRolePolicyDocument: AutoPublishAlias: CodeUri: DeadLetterQueue: DeploymentPreference: Description: Environment: EphemeralStorage: EventInvokeConfig: Handler: KmsKeyArn: Layers: MemorySize: PermissionsBoundary: PropagateTags: ProvisionedConcurrencyConfig: ReservedConcurrentExecutions: Runtime: Tags: Timeout: Tracing: VpcConfig: HttpApi: AccessLogSettings: Auth: PropagateTags: StageVariables: Tags: SimpleTable: SSESpecification: StateMachine: PropagateTags:
注意
不支援任何未包含在先前清單中的資源和屬性。不支持它們的一些原因包括:1)它們會打開潛在的安全問題,或 2)它們使模板難以理解。
隱含的 API
AWS SAM當您在區段中宣告 API 時,會建立隱含Events
的 API。您可以使Globals
用覆蓋隱式 API 的所有屬性。
可重新定義屬性
資源可以覆寫您在Globals
區段中宣告的屬性。例如,您可以將新變數新增至環境變數 map,或者您可以覆寫全域宣告的變數。但資源無法移除Globals
區段中指定的屬性。
更一般地說,該Globals
部分聲明了所有資源共享的屬性。某些資源可以為全域宣告的屬性提供新值,但無法移除它們。如果某些資源使用屬性,但其他資源不使用,那麼您不能在該Globals
部分中聲明它們。
下列各節說明覆寫如何針對不同的資料類型運作。
原始數據類型被替換
原始數據類型包括字符串,數字,布爾值等。
區段中指定的值會取代Resources
區Globals
段中的值。
範例:
Globals: Function: Runtime: nodejs12.x Resources: MyFunction: Type: AWS::Serverless::Function Properties: Runtime: python3.9
用Runtime
於MyFunction
設定為python3.9
。
地圖被合併
地圖也被稱為字典或鍵值對的集合。
Resources
區段中的地圖項目會與全域地圖項目合併。如果存在重複項目,則Resource
剖面項目會取代Globals
剖面項目。
範例:
Globals: Function: Environment: Variables: STAGE: Production TABLE_NAME: global-table Resources: MyFunction: Type: AWS::Serverless::Function Properties: Environment: Variables: TABLE_NAME: resource-table NEW_VAR: hello
的環境變數設MyFunction
定為下列項目:
{ "STAGE": "Production", "TABLE_NAME": "resource-table", "NEW_VAR": "hello" }
列表是可添加的
列表也被稱為數組。
Globals
區段中的清單項目會附加在區段中的清單之Resources
前。
範例:
Globals: Function: VpcConfig: SecurityGroupIds: - sg-123 - sg-456 Resources: MyFunction: Type: AWS::Serverless::Function Properties: VpcConfig: SecurityGroupIds: - sg-first
for SecurityGroupIds
設定VpcConfig
為下列項目:MyFunction
[ "sg-123", "sg-456", "sg-first" ]