

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

# AWS SAM 範本的全域區段
<a name="sam-specification-template-anatomy-globals"></a>

有時，您在 AWS SAM 範本中宣告的資源具有常見的組態。例如，您可能有一個應用程式，其中包含多個具有相同 `Runtime`、`Memory`、`VPCConfig`、 `Environment`和 `Cors`組態`AWS::Serverless::Function`的資源。您可以在 `Globals`區段中宣告一次，並讓資源繼承這些資訊，而不是在每個資源中複製此資訊。

`Globals` 本節支援下列 AWS SAM 資源類型：
+ `AWS::Serverless::Api`
+ `AWS::Serverless::CapacityProvider`
+ `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`，以及針對 使用「index.handler」`Handler`。 除了繼承的 TABLE\_NAME 之外， 還會`HelloWorldFunction`新增 MESSAGE 環境變數。 會`ThumbnailFunction`繼承所有`Globals`屬性並新增 API 事件來源。

## 支援的資源和屬性
<a name="sam-specification-template-anatomy-globals-supported-resources-and-properties"></a>

AWS SAM 支援下列資源和屬性。

```
Globals:
  Api:
    AccessLogSetting:
    Auth:
    BinaryMediaTypes:
    CacheClusterEnabled:
    CacheClusterSize:
    CanarySetting:
    Cors:
    DefinitionUri:
    Domain:
    EndpointConfiguration:
    EndpointAccessMode:
    GatewayResponses:
    MethodSettings:
    MinimumCompressionSize:
    Name:
    OpenApiVersion:
    PropagateTags:
    SecurityPolicy:
    TracingEnabled:
    Variables:
  
  CapacityProvider:
    InstanceRequirements:
    KmsKeyArn:
    OperatorRole:
    PropagateTags:
    ScalingConfig:
    Tags:
    VpcConfig:
  
  Function:
    Architectures:
    AssumeRolePolicyDocument:
    AutoPublishAlias:
    AutoPublishAliasAllProperties:
    CapacityProviderConfig:
    CodeSigningConfigArn:
    CodeUri:
    DeadLetterQueue:
    DeploymentPreference:
    Description:
    DurableConfig:
    Environment:
    EphemeralStorage:
    EventInvokeConfig:
    FileSystemConfigs:
    FunctionScalingConfig:
    FunctionUrlConfig:
    Handler:
    KmsKeyArn:
    Layers:
    LoggingConfig:
    MemorySize:
    PermissionsBoundary:
    PropagateTags:
    ProvisionedConcurrencyConfig:
    PublishToLatestPublished:
    RecursiveLoop:
    ReservedConcurrentExecutions:
    RolePath:
    Runtime:
    RuntimeManagementConfig:
    SnapStart:
    SourceKMSKeyArn:
    Tags:
    TenancyConfig:
    Timeout:
    Tracing:
    VersionDeletionPolicy:
    VpcConfig:

  HttpApi:
    AccessLogSettings:
    Auth:
    PropagateTags:
    StageVariables:
    Tags:

  SimpleTable:
    SSESpecification:
    
  StateMachine:
    PropagateTags:
```

**注意**  
不支援任何未包含在上一個清單中的資源和屬性。不支援它們的一些原因包括：1) 它們開啟了潛在的安全問題，或 2) 它們使範本難以理解。

## 隱含 APIs
<a name="sam-specification-template-anatomy-globals-implicit-apis"></a>

AWS SAM 當您在 `Events`區段中宣告 * APIs 時， 會建立隱含* API。您可以使用 `Globals`覆寫隱含 APIs 的所有屬性。

## 可覆寫的屬性
<a name="sam-specification-template-anatomy-globals-overrideable"></a>

資源可以覆寫您在 `Globals`區段中宣告的屬性。例如，您可以將新變數新增至環境變數映射，也可以覆寫全域宣告的變數。但是資源無法移除 `Globals`區段中指定的屬性。

一般而言， `Globals`區段會宣告所有資源共用的屬性。有些資源可以為全域宣告的屬性提供新值，但無法將其移除。如果某些資源使用屬性，但其他資源不使用，則不得在 `Globals`區段中宣告它們。

下列各節說明覆寫對不同資料類型的運作方式。

### 取代基本資料類型
<a name="sam-specification-template-anatomy-globals-overrideable-primitives"></a>

基本資料類型包括字串、數字、布林值等。

`Resources` 區段中指定的值會取代`Globals`區段中的值。

範例：

```
Globals:
  Function:
    Runtime: nodejs12.x

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.9
```

`Runtime` 的 `MyFunction`設定為 `python3.9`。

### 映射已合併
<a name="sam-specification-template-anatomy-globals-overrideable-maps"></a>

映射也稱為字典或索引鍵/值對的集合。

`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"
}
```

### 清單是附加項目
<a name="sam-specification-template-anatomy-globals-overrideable-lists"></a>

清單也稱為陣列。

`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" ]
```