LoginSignup
3

More than 5 years have passed since last update.

[JAWS-UG CLI] ElastiCache #1 キャッシュクラスタの作成 (Redis編)

Last updated at Posted at 2016-07-04

AWS CLIでElastiCacheのキャッシュクラスタ(Redis)を作成してみます。

前提条件

ElastiCacheへの権限

ElastiCacheに対してフル権限があること。

AWS CLI

以下のバージョンで動作確認済

  • AWS CLI 1.10.44
コマンド
aws --version
結果(例)
      aws-cli/1.10.44 Python/2.7.11 Darwin/15.5.0 botocore/1.4.34

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

AWSアカウントの属性

AWSアカウントがEC2-Classicに対応していないこと。

変数の設定
AWS_SUPPORT_PLATFORMS=$( \
        aws ec2 describe-account-attributes \
          --query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
          --output text \
) && echo ${AWS_SUPPORT_PLATFORMS}
結果
      VPC

注釈: 'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは利用してください。

デフォルトVPCの存在

デフォルトVPCが存在すること。

変数の設定
VPC_ID=$( \
          aws ec2 describe-vpcs \
            --filters Name=isDefault,Values=true \
            --query 'Vpcs[].VpcId' \
            --output text \
) && echo ${VPC_ID}
結果(例)
      vpc-xxxxxxxx

0. 準備

0.1. リージョンの決定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       elasticache-prjx-mbpr13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region        ap-northeast-1        env    AWS_DEFAULT_REGION

0.3. セキュリティグループの指定

変数の設定
VPC_SG_NAME='ec2-redis-global-inbound'
変数の設定
VPC_SG_ID=$( \
        aws ec2 describe-security-groups \
          --filter Name=group-name,Values=${VPC_SG_NAME} \
          --query 'SecurityGroups[].GroupId' \
          --output text \
) && echo ${VPC_SG_ID}

1. 事前作業

1.1. クラスタ名の決定

変数の設定
ECACHE_CLUSTER_ID="handson-$(date +%Y%m%d)" \
        && echo ${ECACHE_CLUSTER_ID}

同名のキャッシュサブネットグループが存在しないことを確認します。

コマンド
aws elasticache describe-cache-clusters \
        --cache-cluster-id ${ECACHE_CLUSTER_ID}
結果(例)
      An error occurred (CacheClusterNotFound) when calling the DescribeCacheClusters operation: CacheCluster not found: handson-20160705

1.2. エンジンの決定

今回は、キャッシュエンジンとしてredisを利用します。

変数の設定
ECACHE_ENGINE_NAME='redis'

注釈: キャッシュエンジンとして、'redis'もしくは'memcached'が選択できます。

1.3. パラメータグループの決定

パラメータグループを決定します。

まず、キャッシュパラメータグループファミリーのうちredisを利用するものを調べます。

コマンド
aws elasticache describe-cache-parameter-groups \
        --query "CacheParameterGroups[?contains(CacheParameterGroupFamily, \`${ECACHE_ENGINE_NAME}\`)]"
結果(例)
      [
          {
              "CacheParameterGroupName": "default.redis2.6", 
              "CacheParameterGroupFamily": "redis2.6", 
              "Description": "Default parameter group for redis2.6"
          }, 
          {
              "CacheParameterGroupName": "default.redis2.8", 
              "CacheParameterGroupFamily": "redis2.8", 
              "Description": "Default parameter group for redis2.8"
          }
      ]

今回は、キャッシュパラメータグループファミリーとして 'redis2.8'を指定します。

変数の設定
ECACHE_PARAM_GROUP_FAMILY='redis2.8'

指定したキャッシュパラメータグループファミリーのデフォルトパラメータを確認しておきましょう。

コマンド
aws elasticache describe-engine-default-parameters \
        --cache-parameter-group-family ${ECACHE_PARAM_GROUP_FAMILY}
結果(例)
(略)

次に、キャッシュパラメータグループを決定します。

今回は、標準で用意されている'default'というプレフィックスが付いたキャッシュパラメータグループを利用します。

変数の設定
ECACHE_PARAM_GROUP_PREFIX='default'
変数の設定
ECACHE_PARAM_GROUP_NAME=$( \
        aws elasticache describe-cache-parameter-groups \
          --query "CacheParameterGroups[?CacheParameterGroupFamily == \`${ECACHE_PARAM_GROUP_FAMILY}\` && contains(CacheParameterGroupName, \`${ECACHE_PARAM_GROUP_PREFIX}\`)].CacheParameterGroupName" \
          --output text \
) && echo ${ECACHE_PARAM_GROUP_NAME}
結果(例)
      default.redis2.8

キャッシュパラメータグループで設定されているパラメータを確認しておきましょう。

コマンド
aws elasticache describe-cache-parameters \
        --cache-parameter-group-name ${ECACHE_PARAM_GROUP_NAME}
結果(例)
(略)

1.4. エンジンのバージョン決定

次に、エンジンのバージョンを決定します。

まず、redisで選択できるバージョンの確認をします。

コマンド
aws elasticache describe-cache-engine-versions \
        --engine ${ECACHE_ENGINE_NAME}
結果(例)
(略)

今回は、'2.8.24'を利用します。

変数の設定
ECACHE_ENGINE_VERSION='2.8.24'

1.5. インスタンスタイプの決定

次に、インスタンスタイプを決定します。

利用できるインスタンスタイプを調べてみましょう。

リザーブドキャッシュインスタンスのオファーから抜き出してみます。

コマンド
aws elasticache describe-reserved-cache-nodes-offerings \
        --query 'ReservedCacheNodesOfferings[].CacheNodeType' \
        --output text
結果(例)
(略)

今回は、cache.t2.microを利用します。

変数の設定
ECACHE_NODE_TYPE='cache.t2.micro'

1.6. ノード数の決定

最後に、キャッシュクラスタで起動するノード数を決定します。

redisでは、キャッシュクラスター構築時にノードを複数持つことができないようなので、ここでは1を指定します。

変数の設定
ECACHE_NODE_COUNT='1'

注釈: Cannot create a Redis cluster with a NumCacheNodes parameter greater than 1.

1.7. セキュリティグループの決定

変数の設定
ARRAY_VPC_SG_IDS="${VPC_SG_ID}" \
        && echo ${ARRAY_VPC_SG_IDS}

2. Redis クラスタの作成

2.1. Redis クラスタの作成

変数の確認
cat << ETX

        ECACHE_CLUSTER_ID:       ${ECACHE_CLUSTER_ID}
        ECACHE_ENGINE_NAME:      ${ECACHE_ENGINE_NAME}
        ECACHE_ENGINE_VERSION:   ${ECACHE_ENGINE_VERSION}
        ECACHE_PARAM_GROUP_NAME: ${ECACHE_PARAM_GROUP_NAME}
        ECACHE_NODE_TYPE:        ${ECACHE_NODE_TYPE}
        ECACHE_NODE_COUNT:       ${ECACHE_NODE_COUNT}
        ARRAY_VPC_SG_IDS:        ${ARRAY_VPC_SG_IDS}

ETX
コマンド
aws elasticache create-cache-cluster \
        --cache-cluster-id ${ECACHE_CLUSTER_ID} \
        --engine ${ECACHE_ENGINE_NAME} \
        --engine-version ${ECACHE_ENGINE_VERSION} \
        --cache-parameter-group-name ${ECACHE_PARAM_GROUP_NAME} \
        --cache-node-type ${ECACHE_NODE_TYPE} \
        --num-cache-nodes ${ECACHE_NODE_COUNT} \
        --security-group-ids ${ARRAY_VPC_SG_IDS}
結果(例)
      {
        "CacheCluster": {
          "Engine": "redis",
          "CacheParameterGroup": {
              "CacheNodeIdsToReboot": [],
              "CacheParameterGroupName": "default.redis2.8",
              "ParameterApplyStatus": "in-sync"
          },
          "CacheClusterId": "handson-20160705",
          "CacheSecurityGroups": [],
          "NumCacheNodes": 1,
          "AutoMinorVersionUpgrade": true,
          "CacheClusterStatus": "creating",
          "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:",
          "CacheSubnetGroupName": "ECACHE_SUBNET_NAME|",
          "EngineVersion": "2.8.24",
          "PendingModifiedValues": {},
          "PreferredMaintenanceWindow": "sun:11:00-sun:12:00",
          "CacheNodeType": "cache.t2.micro"
        }
      }

2.2. ステータスの確認

変数の設定
ECACHE_CLUSTER_STATUS=$( \
        aws elasticache describe-cache-clusters \
          --cache-cluster-id ${ECACHE_CLUSTER_ID} \
          --query 'CacheClusters[].CacheClusterStatus' \
          --output text \
) && echo ${ECACHE_CLUSTER_STATUS}
結果(例)
      creating

5-10分ほどでステータスが'available'になればOKです。

変数の設定
ECACHE_CLUSTER_STATUS=$( \
        aws elasticache describe-cache-clusters \
          --cache-cluster-id ${ECACHE_CLUSTER_ID} \
          --query 'CacheClusters[].CacheClusterStatus' \
          --output text \
) && echo ${ECACHE_CLUSTER_STATUS}
結果(例)
      available

2.3. イベントの確認

変数の設定
MAX_ITEMS='3'
コマンド
aws elasticache describe-events \
        --query "Events[?SourceIdentifier == \`${ECACHE_CLUSTER_ID}\`]" \
        --max-items ${MAX_ITEMS}
結果(例)
      {
        "Events": [
          {
              "Date": "2016-07-04T01:23:45.789",
              "Message": "Added cache node 0001 in availability zone ap-northeast-1a",
              "SourceIdentifier": "handson-20160705",
              "SourceType": "cache-cluster"
          },
          {
              "Date": "2016-07-04T01:23:45.678Z",
              "Message": "This cache cluster does not support persistence (ex: 'appendonly').  Please use a different instance type to enable persistence.",
              "SourceIdentifier": "handson-20160705",
              "SourceType": "cache-cluster"
          },
          {
              "Date": "2016-07-04T01:23:45.678Z",
              "Message": "Cache cluster created",
              "SourceIdentifier": "handson-20160705",
              "SourceType": "cache-cluster"
          }
        ]
      }

3. Redis クラスタの確認

コマンド
aws elasticache describe-cache-clusters \
        --cache-cluster-id ${ECACHE_CLUSTER_ID} \
        --show-cache-node-info
結果(例)
      {
        "CacheClusters": [
          {
              "Engine": "redis",
              "CacheParameterGroup": {
              "CacheNodes": [
                  {
                      "CacheNodeId": "0001",
                      "Endpoint": {
                          "Port": 6379,
                          "Address": "handson-20160705.xxxxxx.0001.usw2cache.amazonaws.com"
                      },
                      "CacheNodeStatus": "available",
                      "ParameterGroupStatus": "in-sync",
                      "CacheNodeCreateTime": "2016-07-04T06:28:40.454Z",
                      "CustomerAvailabilityZone": "ap-northeast-1c"
                  }
              ],
              "CacheParameterGroup": {
                  "CacheNodeIdsToReboot": [],
                  "CacheParameterGroupName": "default.redis2.8",
                  "ParameterApplyStatus": "in-sync"
              },
              "SnapshotRetentionLimit": 0,
              "CacheClusterId": "handson-20160705",
              "CacheSecurityGroups": [],
              "NumCacheNodes": 1,
              "SnapshotWindow": "12:00-13:00",
              "CacheClusterCreateTime": "2016-07-04T01:23:45.678Z",
              "AutoMinorVersionUpgrade": true,
              "CacheClusterStatus": "available",
              "PreferredAvailabilityZone": "ap-northeast-1a",
              "ClientDownloadLandingPage": "https://console.aws.amazon.com/elasticache/home#client-download:",
              "SecurityGroups": [
                  {
                      "Status": "active",
                      "SecurityGroupId": "sg-xxxxxxxx"
                  }
              ],
              "CacheSubnetGroupName": "default",
              "EngineVersion": "2.8.24",
              "PendingModifiedValues": {},
              "PreferredMaintenanceWindow": "sun:11:00-sun:12:00",
              "CacheNodeType": "cache.t2.micro"
          }
        ]
      }

ノードIDの指定

変数の設定
ECACHE_NODE_ID='0001'

エンドポイントの取得

変数の設定
ECACHE_NODE_ENDPOINT=$( \
        aws elasticache describe-cache-clusters \
          --cache-cluster-id ${ECACHE_CLUSTER_ID} \
          --show-cache-node-info \
          --query "CacheClusters[].CacheNodes[?CacheNodeId == \`${ECACHE_NODE_ID}\`].Endpoint.Address" \
          --output text \
) && echo ${ECACHE_NODE_ENDPOINT}
結果(例)
      handson-20160705.xxxxxx.0001.usw2.cache.amazonaws.com

完了

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3