本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
的 “全局变量” 部分Amazon SAM模板
有时候你在Amazon 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 事件源。
支持的资源和属性
Amazon SAM支持以下资源和属性。
Globals: Function: Handler: Runtime: CodeUri: DeadLetterQueue: Description: MemorySize: Timeout: VpcConfig: Environment: Tags: Tracing: KmsKeyArn: Layers: AutoPublishAlias: DeploymentPreference: PermissionsBoundary: ReservedConcurrentExecutions: ProvisionedConcurrencyConfig: AssumeRolePolicyDocument: EventInvokeConfig: Architectures: EphemeralStorage: 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) 它们使模板难以理解。
隐式 API
Amazon SAM创建隐式 API当你在Events部分。您可以使用Globals以覆盖隐式 API 的所有属性。
可覆盖的属性
资源可以覆盖您在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" ]