

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

# 範例：ElastiCache
<a name="customize-environment-resources-elasticache"></a>

下列範例將 Amazon ElastiCache 叢集新增至 EC2-Classic 和 EC2-VPC (預設和自訂 [Amazon Virtual Private Cloud](https://docs.aws.amazon.com/vpc/latest/userguide/) (Amazon VPC)) 平台。如需這些平台以及如何判斷 EC2 為您的區域和 AWS 帳戶支援哪些平台的詳細資訊，請參閱 [https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-supported-platforms.html](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-supported-platforms.html)。之後，請參考本主題下適用您的平台的章節。
+ [EC2-Classic 平台](#customize-environment-resources-elasticache-classic)
+ [EC2-VPC (預設)](#customize-environment-resources-elasticache-defaultvpc)
+ [EC2-VPC (自訂)](#customize-environment-resources-elasticache-targetedvpc)

## EC2-Classic 平台
<a name="customize-environment-resources-elasticache-classic"></a>

本範例將 Amazon ElastiCache 叢集新增至具備 EC2-Classic 平台所啟動的執行個體的環境。本範例列出的所有屬性都是最低要求的屬性，各個資源類型均必須設定。您可以在 [ElastiCache 範例](https://elasticbeanstalk.s3.amazonaws.com/extensions/ElastiCache.config)下載範例。

**注意**  
此範例會建立您可能需要付費 AWS 的資源。如需 AWS 定價的詳細資訊，請參閱 [https://aws.amazon.com/pricing/](https://aws.amazon.com/pricing/)。有些服務是 AWS 免費用量方案的一部分。若您是新客戶，可以免費試用這些服務。如需詳細資訊，請參閱[https://aws.amazon.com/free/](https://aws.amazon.com/free/)。

要使用此範例，請依照下列項目：

1. 在原始碼套件的最上層目錄建立 `[.ebextensions](ebextensions.md)` 目錄。

1. 以 `.config` 延伸建立兩個組態檔案，並且置於 `.ebextensions` 目錄。一個組態檔案定義資源，另一個組態檔案定義選項。

1. 將您的應用程式部署至 Elastic Beanstalk。

   YAML 憑藉一致的縮排。請在取代範例組態檔中的內容時，讓縮排層級一致，並確認您的文字編輯器使用空格而非定位字元進行縮排。

建立定義資源的組態檔案 (如 `elasticache.config`)。在此範例中，我們指定 ElastiCache 叢集資源的名稱 (`MyElastiCache`)、宣告其類型，並設定叢集屬性，藉此建立 ElastiCache 叢集。本範例參考的 ElastiCache 安全群組資源名稱，均由此組態檔案建立並定義。接著，我們建立 ElastiCache 安全群組。我們定義資源名稱、宣告其類型，並新增安全群組的說明。最後，我們設定 ElastiCache 安全群組的輸入規則，僅允許 ElastiCache 安全群組 (`MyCacheSecurityGroup`) 和 Elastic Beanstalk 安全群組 (`AWSEBSecurityGroup`) 內的執行個體進行存取。參數名稱 `AWSEBSecurityGroup` 則是固定的資源名稱，由 Elastic Beanstalk 所提供。您必須將 `AWSEBSecurityGroup` 新增至您的 ElastiCache 安全群組輸入規則，如此一來，您的 Elastic Beanstalk 應用程式即可連接至您 ElastiCache 叢集內的執行個體。) 

```
#This sample requires you to create a separate configuration file that defines the custom option settings for CacheCluster properties.
          
Resources:
  MyElastiCache:
    Type: AWS::ElastiCache::CacheCluster
    Properties:
      CacheNodeType: 
         Fn::GetOptionSetting:
             OptionName : CacheNodeType
             DefaultValue: cache.m1.small
      NumCacheNodes: 
           Fn::GetOptionSetting:
             OptionName : NumCacheNodes
             DefaultValue: 1
      Engine: 
           Fn::GetOptionSetting:
             OptionName : Engine
             DefaultValue: memcached
      CacheSecurityGroupNames:
        - Ref: MyCacheSecurityGroup
  MyCacheSecurityGroup:
    Type: AWS::ElastiCache::SecurityGroup
    Properties:
      Description: "Lock cache down to webserver access only"
  MyCacheSecurityGroupIngress:
    Type: AWS::ElastiCache::SecurityGroupIngress
    Properties:
      CacheSecurityGroupName: 
        Ref: MyCacheSecurityGroup
      EC2SecurityGroupName:
        Ref: AWSEBSecurityGroup
```

如需本範例組態檔案所使用的資源的詳細資訊，請參閱下列參考：
+ [AWS::ElastiCache::CacheCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-cache-cluster.html)
+ [AWS::ElastiCache::SecurityGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-security-group.html)
+ [AWS::ElastiCache:SecurityGroupIngress](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-security-group-ingress.html)

建立另一個名為 `options.config` 的組態檔案，並定義自訂選項設定。

```
option_settings:
  "aws:elasticbeanstalk:customoption":
     CacheNodeType : cache.m1.small
     NumCacheNodes : 1
     Engine : memcached
```

這些行指示 Elastic Beanstalk 從組態檔案 （範例中的 options.config) 中取得 **CacheNodeType、NumCacheNodes 和 Engine** 屬性的值，其中包含具有 **aws：elasticbeanstalk：customoption** 區段的 option\$1settings 區段，其中包含要使用的實際值之名稱值對。 **CacheNodeType NumCacheNodes** 上述範例表示這些值為 cache.m1.small、1 和 memcached。如需有關 `Fn::GetOptionSetting` 的詳細資訊，請參閱 [函數](ebextensions-functions.md)。

## EC2-VPC (預設)
<a name="customize-environment-resources-elasticache-defaultvpc"></a>

本範例將 Amazon ElastiCache 叢集新增至具備 EC2-VPC 平台所啟動的執行個體的環境。具體而言，本章節的資訊適用 EC2 於預設 VPC 啟動執行個體的情境。本範例的所有屬性都是最低要求的屬性，各個資源類型均必須設定。如需預設 VPC 的詳細資訊，請參閱[您的預設 VPC 與子網路](https://docs.aws.amazon.com/vpc/latest/userguide/default-vpc.html)相關文章。

**注意**  
此範例會建立您可能需要付費 AWS 的資源。如需 AWS 定價的詳細資訊，請參閱 [https://aws.amazon.com/pricing/](https://aws.amazon.com/pricing/)。有些服務是 AWS 免費用量方案的一部分。若您是新客戶，可以免費試用這些服務。如需詳細資訊，請參閱[https://aws.amazon.com/free/](https://aws.amazon.com/free/)。

要使用此範例，請依照下列項目：

1. 在原始碼套件的最上層目錄建立 `[.ebextensions](ebextensions.md)` 目錄。

1. 以 `.config` 延伸建立兩個組態檔案，並且置於 `.ebextensions` 目錄。一個組態檔案定義資源，另一個組態檔案定義選項。

1. 將您的應用程式部署至 Elastic Beanstalk。

   YAML 憑藉一致的縮排。請在取代範例組態檔中的內容時，讓縮排層級一致，並確認您的文字編輯器使用空格而非定位字元進行縮排。

現請為資源組態檔案 `elasticache.config` 命名。本範例指定 ElastiCache 叢集資源的名稱 (`MyElastiCache`)、宣告其類型，並設定叢集屬性，藉此建立 ElastiCache 叢集。本範例參考的安全群組資源 ID，均由我們於此組態檔案中建立並定義。

接著，我們建立 EC2 安全群組。我們定義此資源的名稱、宣告其類型、新增說明，並設定安全群組的輸入規則，僅允許 Elastic Beanstalk 安全群組 (`AWSEBSecurityGroup`) 內的執行個體進行存取。(參數名稱 `AWSEBSecurityGroup` 是 Elastic Beanstalk 提供的固定資源名稱。您必須將 `AWSEBSecurityGroup` 新增至您的 ElastiCache 安全群組輸入規則，如此一來，您的 Elastic Beanstalk 應用程式即可連接至您 ElastiCache 叢集內的執行個體)。

EC2 安全群組的輸入規則亦定義快取節點可接受連線的 IP 通訊協定和連接埠號碼。以 Redis 而言，預設連接埠號碼為 `6379`。

```
#This sample requires you to create a separate configuration file that defines the custom option settings for CacheCluster properties.

Resources:
  MyCacheSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: "Lock cache down to webserver access only"
      SecurityGroupIngress :
        - IpProtocol : "tcp"
          FromPort :
            Fn::GetOptionSetting:
              OptionName : "CachePort"
              DefaultValue: "6379"
          ToPort :
            Fn::GetOptionSetting:
              OptionName : "CachePort"
              DefaultValue: "6379"
          SourceSecurityGroupName:
            Ref: "AWSEBSecurityGroup"
  MyElastiCache:
    Type: "AWS::ElastiCache::CacheCluster"
    Properties:
      CacheNodeType:
        Fn::GetOptionSetting:
          OptionName : "CacheNodeType"
          DefaultValue : "cache.t2.micro"
      NumCacheNodes:
        Fn::GetOptionSetting:
          OptionName : "NumCacheNodes"
          DefaultValue : "1"
      Engine:
        Fn::GetOptionSetting:
          OptionName : "Engine"
          DefaultValue : "redis"
      VpcSecurityGroupIds:
        -
          Fn::GetAtt:
            - MyCacheSecurityGroup
            - GroupId

Outputs:
  ElastiCache:
    Description : "ID of ElastiCache Cache Cluster with Redis Engine"
    Value :
      Ref : "MyElastiCache"
```

如需本範例組態檔案所使用的資源的詳細資訊，請參閱下列參考：
+ [AWS::ElastiCache::CacheCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-cache-cluster.html)
+ [AWS::EC2::SecurityGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html)

接著，請為選項組態檔案 `options.config` 命名，並定義自訂選項設定。

```
option_settings:
  "aws:elasticbeanstalk:customoption":
    CacheNodeType : cache.t2.micro
    NumCacheNodes : 1
    Engine : redis
    CachePort : 6379
```

這些行會指示 Elastic Beanstalk 自組態檔案 (本範例為 `options.config`) 中的 `CacheNodeType`、`NumCacheNodes`、`Engine` 和 `CachePort` 值，取得 `CacheNodeType`、`NumCacheNodes`、`Engine` 和 `CachePort` 屬性的值。該檔案具備 `aws:elasticbeanstalk:customoption` 區段 (`option_settings` 之下)，內含的名稱/值對帶有該使用的實際值。上述範例的這些值為 `cache.t2.micro`、`1`、`redis` 和 `6379`。如需有關 `Fn::GetOptionSetting` 的詳細資訊，請參閱 [函數](ebextensions-functions.md)。

## EC2-VPC (自訂)
<a name="customize-environment-resources-elasticache-targetedvpc"></a>

若您於 EC2-VPC 平台建立自訂 VPC，並將其指定為 EC2 啟動執行個體的 VPC，則將 Amazon ElastiCache 叢集新增至您環境的程序，與預設 VPC 的程序不同。主要差異在於，您必須建立 ElastiCache 叢集的子網路群組。本範例的所有屬性都是最低要求的屬性，各個資源類型均必須設定。

**注意**  
此範例會建立您可能需要付費 AWS 的資源。如需 AWS 定價的詳細資訊，請參閱 [https://aws.amazon.com/pricing/](https://aws.amazon.com/pricing/)。有些服務是 AWS 免費用量方案的一部分。若您是新客戶，可以免費試用這些服務。如需詳細資訊，請參閱[https://aws.amazon.com/free/](https://aws.amazon.com/free/)。

要使用此範例，請依照下列項目：

1. 在原始碼套件的最上層目錄建立 `[.ebextensions](ebextensions.md)` 目錄。

1. 以 `.config` 延伸建立兩個組態檔案，並且置於 `.ebextensions` 目錄。一個組態檔案定義資源，另一個組態檔案定義選項。

1. 將您的應用程式部署至 Elastic Beanstalk。

   YAML 憑藉一致的縮排。請在取代範例組態檔中的內容時，讓縮排層級一致，並確認您的文字編輯器使用空格而非定位字元進行縮排。

現請為資源組態檔案 `elasticache.config` 命名。本範例指定 ElastiCache 叢集資源的名稱 (`MyElastiCache`)、宣告其類型，並設定叢集屬性，藉此建立 ElastiCache 叢集。本範例屬性參考 ElastiCache 叢集的子網路群組名稱，以及由我們於此組態檔案建立並定義的安全群組資源 ID。

接著，我們建立 EC2 安全群組。我們定義此資源的名稱、宣告其類型、新增說明和 VPC ID，並設定安全群組的輸入規則，僅允許 Elastic Beanstalk 安全群組 (`AWSEBSecurityGroup`) 內的執行個體進行存取。(參數名稱 `AWSEBSecurityGroup` 是 Elastic Beanstalk 提供的固定資源名稱。您必須將 `AWSEBSecurityGroup` 新增至您的 ElastiCache 安全群組輸入規則，如此一來，您的 Elastic Beanstalk 應用程式即可連接至您 ElastiCache 叢集內的執行個體)。

EC2 安全群組的輸入規則亦定義快取節點可接受連線的 IP 通訊協定和連接埠號碼。以 Redis 而言，預設連接埠號碼為 `6379`。最後，本範例建立 ElastiCache 叢集的子網路群組。我們定義資源名稱、宣告其類型，並新增子網路群組的子網路說明和 ID。

**注意**  
我們建議您使用適用於 ElastiCache 叢集的私有子網路。如需 VPC 和私有子網路的詳細資訊，請參閱 [https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html)。

```
#This sample requires you to create a separate configuration file that defines the custom option settings for CacheCluster properties.

Resources:
  MyElastiCache:
    Type: "AWS::ElastiCache::CacheCluster"
    Properties:
      CacheNodeType:
        Fn::GetOptionSetting:
          OptionName : "CacheNodeType"
          DefaultValue : "cache.t2.micro"
      NumCacheNodes:
        Fn::GetOptionSetting:
          OptionName : "NumCacheNodes"
          DefaultValue : "1"
      Engine:
        Fn::GetOptionSetting:
          OptionName : "Engine"
          DefaultValue : "redis"
      CacheSubnetGroupName:
        Ref: "MyCacheSubnets"
      VpcSecurityGroupIds:
        - Ref: "MyCacheSecurityGroup"
  MyCacheSecurityGroup:
    Type: "AWS::EC2::SecurityGroup"
    Properties:
      GroupDescription: "Lock cache down to webserver access only"
      VpcId:
        Fn::GetOptionSetting:
          OptionName : "VpcId"
      SecurityGroupIngress :
        - IpProtocol : "tcp"
          FromPort :
            Fn::GetOptionSetting:
              OptionName : "CachePort"
              DefaultValue: "6379"
          ToPort :
            Fn::GetOptionSetting:
              OptionName : "CachePort"
              DefaultValue: "6379"
          SourceSecurityGroupId:
            Ref: "AWSEBSecurityGroup"
  MyCacheSubnets:
    Type: "AWS::ElastiCache::SubnetGroup"
    Properties:
      Description: "Subnets for ElastiCache"
      SubnetIds:
        Fn::GetOptionSetting:
          OptionName : "CacheSubnets"
Outputs:
  ElastiCache:
    Description : "ID of ElastiCache Cache Cluster with Redis Engine"
    Value :
      Ref : "MyElastiCache"
```

如需本範例組態檔案所使用的資源的詳細資訊，請參閱下列參考：
+ [AWS::ElastiCache::CacheCluster](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-cache-cluster.html)
+ [AWS::EC2::SecurityGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-security-group.html)
+ [AWS::ElastiCache::SubnetGroup](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-elasticache-subnetgroup.html)

接著，請為選項組態檔案 `options.config` 命名，並定義自訂選項設定。

**注意**  
在下列範例中，請使用您自己的子網路和 VPC，取代範例 `CacheSubnets` 和 `VpcId` 的值。

```
option_settings:
  "aws:elasticbeanstalk:customoption":
    CacheNodeType : cache.t2.micro
    NumCacheNodes : 1
    Engine : redis
    CachePort : 6379
    CacheSubnets:
      - subnet-1a1a1a1a
      - subnet-2b2b2b2b
      - subnet-3c3c3c3c
    VpcId: vpc-4d4d4d4d
```

這些行會指示 Elastic Beanstalk 自組態檔案 (本範例為 `options.config`) 中的 `CacheNodeType`、`NumCacheNodes`、`Engine`、`CachePort`、`CacheSubnets` 和 `VpcId` 值，取得 `CacheNodeType`、`NumCacheNodes`、`Engine`、`CachePort`、`CacheSubnets` 和 `VpcId` 屬性的值。該檔案具備 `aws:elasticbeanstalk:customoption` 區段 (`option_settings` 之下)，內含的名稱/值對帶有範例值。上述範例的這些值為 `cache.t2.micro`、`1`、`redis`、`6379`、`subnet-1a1a1a1a`、`subnet-2b2b2b2b`、`subnet-3c3c3c3c` 和 `vpc-4d4d4d4d`。如需有關 `Fn::GetOptionSetting` 的詳細資訊，請參閱 [函數](ebextensions-functions.md)。