🌊

18.ElastiCacheをCloudFormationで動かしてみる試み

2024/08/19に公開

0. やりたいこと

  • ElastiCache(オンデマンドノード)をCloudFormationで立てたい
    • サーバーレスとは対称にオンデマンドノードと呼ぶそう

1. 必要設定項目の確認

マネジメントコンソールを確認

作成方法:簡単な作成

  • 選べる3つの設定の差分については以下のような構成
設定 本番稼動用 開発/テスト デモ
ノードのタイプ cache.r7g.large cache.r7g.xlarge cache.t4g.micro
シャード 3 1 1
レプリカ 2 1 0
マルチAZ 有効 有効 無効
転送中の暗号化 有効 有効 有効
自動バックアップ 有効 有効 無効
バックアップ保持期間 1 1 -

作成方法:クラスターキャッシュ

クラスターモードモード

  • クラスターモード有効の場合
    • シャード数が増やせる
    • この時、パラメーターグループは「default.redis7.cluster.on」
  • クラスターモード無効の場合
    • シャード数を増やすことはできない。
    • そのため、コンソール画面上でもシャードの数値の値はなく、あくまでもレプリカ数しか設定できない
    • なので、原理的には1シャードnレプリカ(nは0~5の値)
      • この時、1シャードには1つのプライマリノードは付いている
    • この時、パラメーターグループは「default.redis7」

大前提用語

名前 概要 備考
クラスター 1つ以上のシャードを持っている レプリケーショングループって言ったりもする
シャード 1つ以上のノードを持っている(1プライマリノードは必ず保持) ノードグループって言ったりもする
ノード 構成要素の最小単位 -

ノード2つの役割種類

名前 概要 備考
プライマリノード 読み/書きができる 1シャードには1プライマリノードは保持
レプリカノード 読みができる -

2. 作ってみる

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticache-replicationgroup.html

$ aws cloudformation create-stack \
  --profile <my-profile> \
  --region ap-northeast-1 \
  --stack-name takenoko \
  --template-body file://takenoko.yml \
  --capabilities CAPABILITY_IAM

構成

項目
クラスターモード 有効
ノードタイプ cache.t4g.micro
シャード 1
レプリカ 1
takemono.yaml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'ElastiCache Redis Cluster with CloudFormation'

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: ElastiCacheVPC
        - Key: Stack
          Value: takenoko

  Subnet1:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Select [0, !GetAZs ]
      CidrBlock: 10.0.1.0/24
      VpcId: !Ref VPC
      Tags:
        - Key: Stack
          Value: takenoko

  Subnet2:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Select [1, !GetAZs ]
      CidrBlock: 10.0.2.0/24
      VpcId: !Ref VPC
      Tags:
        - Key: Stack
          Value: takenoko

  SubnetGroup:
    Type: AWS::ElastiCache::SubnetGroup
    Properties:
      Description: Subnet group for ElastiCache
      SubnetIds:
        - !Ref Subnet1
        - !Ref Subnet2
      Tags:
        - Key: Stack
          Value: takenoko

  RedisCluster:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
      AutomaticFailoverEnabled: true
      CacheNodeType: cache.t4g.micro
      CacheParameterGroupName: default.redis7.cluster.on
      CacheSubnetGroupName: !Ref SubnetGroup
      Engine: redis
      EngineVersion: '7.1'
      MultiAZEnabled: true
      NumNodeGroups: 1
      Port: 6379
      ReplicasPerNodeGroup: 1
      ReplicationGroupDescription: ElastiCache Redis cluster
      ReplicationGroupId: takenoko
      Tags:
        - Key: Stack
          Value: takenoko

Outputs:
  RedisClusterAddress:
    Description: The address of the Redis cluster
    Value: !GetAtt RedisCluster.ConfigurationEndPoint.Address

作成に14分程

確認

aws elasticache describe-cache-clusters \
  --profile <my-profile> \
  --region ap-northeast-1 \
  --output yaml
CacheClusters:
  - ARN: arn:aws:elasticache:ap-northeast-1:<account-id>:cluster:takenoko-0001-001
    AtRestEncryptionEnabled: false
    AuthTokenEnabled: false
    AutoMinorVersionUpgrade: true
    CacheClusterId: takenoko-0001-001
    CacheClusterStatus: creating
    CacheNodeType: cache.t4g.micro
    CacheParameterGroup:
      CacheNodeIdsToReboot: []
      CacheParameterGroupName: default.redis7.cluster.on
      ParameterApplyStatus: in-sync
    CacheSecurityGroups: []
    CacheSubnetGroupName: takenoko-subnetgroup-geeztiuirlsa
    ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
    Engine: redis
    EngineVersion: 7.1.0
    IpDiscovery: ipv4
    LogDeliveryConfigurations: []
    NetworkType: ipv4
    NumCacheNodes: 1
    PendingModifiedValues: {}
    PreferredAvailabilityZone: ap-northeast-1a
    PreferredMaintenanceWindow: wed:13:00-wed:14:00
    ReplicationGroupId: takenoko
    ReplicationGroupLogDeliveryEnabled: false
    SnapshotRetentionLimit: 0
    SnapshotWindow: 20:00-21:00
    TransitEncryptionEnabled: false

  - ARN: arn:aws:elasticache:ap-northeast-1:<account-id>:cluster:takenoko-0001-002
    AtRestEncryptionEnabled: false
    AuthTokenEnabled: false
    AutoMinorVersionUpgrade: true
    CacheClusterId: takenoko-0001-002
    CacheClusterStatus: creating
    CacheNodeType: cache.t4g.micro
    CacheParameterGroup:
      CacheNodeIdsToReboot: []
      CacheParameterGroupName: default.redis7.cluster.on
      ParameterApplyStatus: in-sync
    CacheSecurityGroups: []
    CacheSubnetGroupName: takenoko-subnetgroup-geeztiuirlsa
    ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
    Engine: redis
    EngineVersion: 7.1.0
    IpDiscovery: ipv4
    LogDeliveryConfigurations: []
    NetworkType: ipv4
    NumCacheNodes: 1
    PendingModifiedValues: {}
    PreferredAvailabilityZone: ap-northeast-1c
    PreferredMaintenanceWindow: wed:13:00-wed:14:00
    ReplicationGroupId: takenoko
    ReplicationGroupLogDeliveryEnabled: false
    SnapshotRetentionLimit: 0
    SnapshotWindow: 20:00-21:00
    TransitEncryptionEnabled: false

後片付け

$ aws cloudformation delete-stack \
  --profile <my-profile> \
  --region ap-northeast-1 \
  --stack-name takenoko

削除10分程度

3. 料金

https://calculator.aws/#/createCalculator/ElastiCache

  • 基本的にはノード数とノードのタイプで決まりそう
    • ノード数
      • 1シャードに1Primaryノードが付属
      • レプリカノードを追加することで、これで2ノード
      • なので、calculatorでは2ノードで入力
    • ノードタイプ
      • cache.t4g.micro

1時間放置してみる

  • 東京リージョン
  • 0.025 USD * 2インスタンス = $0.05
    • 為替を150円/$として 0.05 x 150 = 7.5円

結果

  • $0.05
  • 合致

4. おまけ1(クラスターモード無効で作ってみる)

構成

項目
クラスターモード 無効
ノードタイプ cache.t4g.micro
シャード 1
レプリカ 0
$ aws cloudformation create-stack \
  --profile <my-profile> \
  --region us-east-2 \
  --stack-name shiitake \
  --template-body file://shiitake.yml \
  --capabilities CAPABILITY_IAM
shiitake.yml
AWSTemplateFormatVersion: '2010-09-09'
Description: 'ElastiCache Redis Cluster with CloudFormation'

Resources:
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: ElastiCacheVPC
        - Key: Stack
          Value: shiitake

  Subnet1:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Select [0, !GetAZs ]
      CidrBlock: 10.0.1.0/24
      VpcId: !Ref VPC
      Tags:
        - Key: Stack
          Value: shiitake

  Subnet2:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: !Select [1, !GetAZs ]
      CidrBlock: 10.0.2.0/24
      VpcId: !Ref VPC
      Tags:
        - Key: Stack
          Value: shiitake

  SubnetGroup:
    Type: AWS::ElastiCache::SubnetGroup
    Properties:
      Description: Subnet group for ElastiCache
      SubnetIds:
        - !Ref Subnet1
        - !Ref Subnet2
      Tags:
        - Key: Stack
          Value: shiitake

  RedisCluster:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
      AutomaticFailoverEnabled: false
      CacheNodeType: cache.t4g.micro
      CacheParameterGroupName: default.redis7
      CacheSubnetGroupName: !Ref SubnetGroup
      Engine: redis
      EngineVersion: '7.1'
      MultiAZEnabled: false
      NumNodeGroups: 1
      Port: 6379
      ReplicasPerNodeGroup: 0
      ReplicationGroupDescription: ElastiCache Redis cluster
      ReplicationGroupId: shiitake
      Tags:
        - Key: Stack
          Value: shiitake

10分程度

差分

  • レプリカ0だとマルチAZの有効化、自動フェイルオーバーができなくなるので明記
- AutomaticFailoverEnabled: true
+ AutomaticFailoverEnabled: false
...
- CacheParameterGroupName: default.redis7.cluster.on
+ CacheParameterGroupName: default.redis7
...
- MultiAZEnabled: true
+ MultiAZEnabled: false
...
- ReplicasPerNodeGroup: 1
+ ReplicasPerNodeGroup: 0

確認

$ aws elasticache describe-cache-clusters \
  --profile <my-profile> \
  --region us-east-2 \
  --output yaml
CacheClusters:
- ARN: arn:aws:elasticache:us-east-2:<account-id>:cluster:shiitake-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-19T08:24:55.140000+00:00'
  CacheClusterId: shiitake-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: shiitake-subnetgroup-czlb8zr8omi7
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-2a
  PreferredMaintenanceWindow: mon:05:30-mon:06:30
  ReplicationGroupId: shiitake
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 02:30-03:30
  TransitEncryptionEnabled: false

CLIからレプリカノードを0から1に増やしてみる

$ aws elasticache increase-replica-count \
  --profile <my-profile> \
  --region us-east-2 \
  --replication-group-id shiitake \
  --new-replica-count 1 \
  --apply-immediately

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elasticache/increase-replica-count.html

primaryのmodifyは1分程
replicaの作成10分程

確認

$ aws elasticache describe-cache-clusters \
  --profile <my-profile> \
  --region us-east-2 \
  --output yaml

別日に新しくstackを作り直しているので、細かい部分に差あり

CacheClusters:
- ARN: arn:aws:elasticache:us-east-2:<account-id>:cluster:shiitake-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T02:05:49.090000+00:00'
  CacheClusterId: shiitake-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: shiitake-subnetgroup-b8aykns2sudz
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-2b
  PreferredMaintenanceWindow: wed:23:30-thu:00:30
  ReplicationGroupId: shiitake
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 04:30-05:30
  TransitEncryptionEnabled: false
- ARN: arn:aws:elasticache:us-east-2:<account-id>:cluster:shiitake-002
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T02:22:17.884000+00:00'
  CacheClusterId: shiitake-002
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: shiitake-subnetgroup-b8aykns2sudz
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-2a
  PreferredMaintenanceWindow: wed:23:30-thu:00:30
  ReplicationGroupId: shiitake
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 04:30-05:30
  TransitEncryptionEnabled: false

増やしたレプリカノードを1から0に減らしてみる

$ aws elasticache decrease-replica-count \
  --profile <my-profile> \
  --region us-east-2 \
  --replication-group-id shiitake \
  --new-replica-count 0 \
  --apply-immediately

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elasticache/decrease-replica-count.html

確認

$ aws elasticache describe-cache-clusters \
  --profile <my-profile> \
  --region us-east-2 \
  --output yaml

primaryのmodifyは1分程
replicaの削除10分程

CacheClusters:
- ARN: arn:aws:elasticache:us-east-2:<account-id>:cluster:shiitake-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T02:05:49.090000+00:00'
  CacheClusterId: shiitake-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: shiitake-subnetgroup-b8aykns2sudz
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-2b
  PreferredMaintenanceWindow: wed:23:30-thu:00:30
  ReplicationGroupId: shiitake
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 04:30-05:30
  TransitEncryptionEnabled: false

templateファイルの変更からupdate-stackしようと試みた時

結果

  • ReplicationGroupIdを変えないといけなさそう
    • ReplicationGroupIdを変えると各種エンドポイントが変更になる
    • replicaのノード数の変更はcloudformationではなさそう

以下手順

  • ReplicasPerNodeGroupを0 → 1
- ReplicasPerNodeGroup: 0
+ ReplicasPerNodeGroup: 1
$ aws cloudformation update-stack \
  --profile <my-profile> \
  --region us-east-2 \
  --stack-name shiitake \
  --template-body file://shiitake.yml
CloudFormation cannot update a stack when a custom-named resource requires replacing. Rename shiitake and update the stack again.

https://repost.aws/ja/knowledge-center/cloudformation-custom-name

4. おまけ2(クラスターモードonでシャード数を追加してみる)

  • 一度削除したので再度作成
$ aws cloudformation create-stack \
  --profile <my-profile> \
  --region us-east-1 \
  --stack-name takenoko \
  --template-body file://takenoko.yml \
  --capabilities CAPABILITY_IAM
  • before
CacheClusters:
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0001-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T03:40:42.727000+00:00'
  CacheClusterId: takenoko-0001-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1a
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0001-002
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T03:40:42.727000+00:00'
  CacheClusterId: takenoko-0001-002
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1b
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false

シャード追加

aws elasticache modify-replication-group-shard-configuration \
  --profile <my-profile> \
  --region us-east-1 \
  --replication-group-id takenoko \
  --node-group-count 2 \
  --apply-immediately

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elasticache/modify-replication-group-shard-configuration.html

  • after
    • takenoko-0002 が追加
CacheClusters:
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0001-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T03:40:42.727000+00:00'
  CacheClusterId: takenoko-0001-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1a
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0001-002
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T03:40:42.727000+00:00'
  CacheClusterId: takenoko-0001-002
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1b
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0002-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T04:08:11.253000+00:00'
  CacheClusterId: takenoko-0002-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1a
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0002-002
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T04:08:11.339000+00:00'
  CacheClusterId: takenoko-0002-002
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1b
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false

シャード数を2から1に削減

aws elasticache modify-replication-group-shard-configuration \
  --profile <my-profile> \
  --region us-east-1 \
  --replication-group-id takenoko \
  --node-group-count 1 \
  --apply-immediately \
  --node-groups-to-remove 0002
  • takenoko-0002 が削除
CacheClusters:
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0001-001
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T03:40:42.727000+00:00'
  CacheClusterId: takenoko-0001-001
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1a
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00
  TransitEncryptionEnabled: false
- ARN: arn:aws:elasticache:us-east-1:<account-id>:cluster:takenoko-0001-002
  AtRestEncryptionEnabled: false
  AuthTokenEnabled: false
  AutoMinorVersionUpgrade: true
  CacheClusterCreateTime: '2024-08-25T03:40:42.727000+00:00'
  CacheClusterId: takenoko-0001-002
  CacheClusterStatus: available
  CacheNodeType: cache.t4g.micro
  CacheParameterGroup:
    CacheNodeIdsToReboot: []
    CacheParameterGroupName: default.redis7.cluster.on
    ParameterApplyStatus: in-sync
  CacheSecurityGroups: []
  CacheSubnetGroupName: takenoko-subnetgroup-uy0nkpsw7oge
  ClientDownloadLandingPage: 'https://console.aws.amazon.com/elasticache/home#client-download:'
  Engine: redis
  EngineVersion: 7.1.0
  IpDiscovery: ipv4
  LogDeliveryConfigurations: []
  NetworkType: ipv4
  NumCacheNodes: 1
  PendingModifiedValues: {}
  PreferredAvailabilityZone: us-east-1b
  PreferredMaintenanceWindow: sat:03:00-sat:04:00
  ReplicationGroupId: takenoko
  ReplicationGroupLogDeliveryEnabled: false
  SnapshotRetentionLimit: 0
  SnapshotWindow: 07:00-08:00

5. 参考

https://aws.amazon.com/jp/elasticache/pricing/?nc=sn&loc=5

https://docs.aws.amazon.com/ja_jp/AmazonElastiCache/latest/red-ug/designing-elasticache-cluster.html

https://docs.aws.amazon.com/ja_jp/AmazonElastiCache/latest/red-ug/CacheNodes.NodeGroups.html

https://zenn.dev/fdnsy/articles/727864f43d9e67

ReplicaがPrimaryに昇格するには多少のダウンタイムが発生します。
複数のシャードでPrimaryノードを持つことで、障害時も別シャードのPrimaryを参照することができます。

https://dev.classmethod.jp/articles/elasticache-cluster-mode/

シャードが1つの場合、AZ障害が起こってからノード昇格が完了するまではプライマリノードが一時的に0個になってしまうため、その間は書き込み処理が行えなくなります。

書き込み処理が少しでも止まってしまうと困るという場合は、クラスターモードを有効化してシャードを複数個作成し、プライマリノードを別々のAZに配置する事を推奨します。

そのようにする事で、1個のプライマリノードにAZ障害が起きても別のプライマリノードを使用できるため、プライマリノードのダウンタイムを最小限に抑える事が可能です。

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/elasticache/index.html

Discussion