本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
的Globals部分 AWS SAM 模板
有时,您在 AWS SAM 模板具有通用配置。例如,您可能有一个应用程序 AWS::Serverless::Function
这些资源 Runtime
, Memory
, VPCConfig
, Environment
,和 Cors
配置。不是在每个资源中复制此信息,而是可以在 Globals
并让您的资源继承它们。
的 Globals
部分受 AWS::Serverless::Function
, AWS::Serverless::Api
, AWS::Serverless::HttpApi
,和 AWS::Serverless::SimpleTable
资源。
示例:
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
,和的“index.handler” Handler
。 HelloWorldFunction
除了继承的TABLE_NAME之外,还添加了MESSAGE环境变量。ThumbnailFunction
继承所有 Globals
属性并添加API事件源。
支持的资源和属性
AWS SAM 支持以下资源和属性。
Globals: Function: Handler: Runtime: CodeUri: DeadLetterQueue: Description: MemorySize: Timeout: VpcConfig: Environment: Tags: Tracing: KmsKeyArn: Layers: AutoPublishAlias: DeploymentPreference: PermissionsBoundary: ReservedConcurrentExecutions: ProvisionedConcurrencyConfig: AssumeRolePolicyDocument: EventInvokeConfig: Api: Auth: Name: DefinitionUri: CacheClusterEnabled: CacheClusterSize: Variables: EndpointConfiguration: MethodSettings: BinaryMediaTypes: MinimumCompressionSize: Cors: GatewayResponses: AccessLogSetting: CanarySetting: TracingEnabled: OpenApiVersion: Domain: HttpApi: Auth: AccessLogSettings: StageVariables: Tags: SimpleTable: SSESpecification:
不支持前一个列表中未包含的任何资源和属性。一些不支持他们的原因包括: 1)他们打开潜在的安全问题,或2)他们使模板难以理解。
隐含的 APIs
AWS SAM 创造 隐式 APIs 当您在 Events
第节。您可以使用 Globals
覆盖隐式的所有属性 APIs.
覆盖属性
资源可以覆盖您在 Globals
第节。例如,您可以向环境变量映射添加新变量,或者可以覆盖全局声明的变量。但是,资源无法移除在 Globals
第节。
更一般地说 Globals
部分将声明所有资源共享的属性。某些资源可以为全球申报的酒店提供新值,但无法将其删除。如果某些资源使用酒店,但其他资源不使用,则不得在 Globals
第节。
以下部分描述了不同数据类型的覆盖工作原理。
原语数据类型已替换
原始数据类型包括字符串、数字、布尔值等。
在中指定的值 Resources
部分将替换 Globals
第节。
示例:
Globals: Function: Runtime: nodejs12.x Resources: MyFunction: Type: AWS::Serverless::Function Properties: Runtime: python3.6
的 Runtime
为 MyFunction
设定为 python3.6
.
映射已合并
映射也称为字典或键值对集合。
映射中的条目 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
的 SecurityGroupIds
为 MyFunction
的 VpcConfig
设置为以下内容:
[ "sg-123", "sg-456", "sg-first" ]