CloudFormationでElasticache for redisの設定をする
CloudFormationで非クラスターモード(マルチAZ)の構成を試してみた。
プライマリーノード:1、リードレプリカ:2の3ノード構成でプライベートなVPCに配置する想定。
最終的には下記の公式ドキュメントのサンプルオペレーションをCloudFormation対応した感じ。
次のオペレーションでは、3 つのノード、1 つのプライマリ、2 つのレプリカを持つ Redis (クラスターモードが無効) レプリケーショングループ new-group を作成します。
一部抜粋だけどこんな感じになった。
ECacheSecurityGroup: Type: "AWS::EC2::SecurityGroup" Properties: GroupName: !Ref ECacheSecurityGroupName GroupDescription: Security Group for Elasticache Redis SecurityGroupIngress: - IpProtocol: tcp CidrIp: '0.0.0.0/0' FromPort: 6379 ToPort: 6379 - IpProtocol: tcp CidrIpv6: '::/0' FromPort: 6379 ToPort: 6379 Tags: - Key: Name Value: !Ref ECacheSecurityGroupName VpcId: !Ref VpcId ECacheSubnetGroup: Type: "AWS::ElastiCache::SubnetGroup" Properties: CacheSubnetGroupName: !Ref ECacheSubnetGroupName Description: Redis Subnet Group SubnetIds: - !Ref SubnetAId - !Ref SubnetCId ECacheRedis: Type: "AWS::ElastiCache::ReplicationGroup" Properties: AutomaticFailoverEnabled: true AutoMinorVersionUpgrade: true CacheNodeType: !Ref ECacheNodeType CacheParameterGroupName: default.redis3.2 CacheSubnetGroupName: !Ref ECacheSubnetGroup Engine: redis EngineVersion: 3.2.10 NumCacheClusters: 3 ReplicationGroupDescription: 'Elasticache Redis' ReplicationGroupId: !Ref ECacheClusterName SecurityGroupIds: - !Ref ECacheSecurityGroup SnapshotRetentionLimit: 5 SnapshotWindow: 'sun:13:30-sun:14:30'
VPC内に配置したい場合
所属するサブネットグループをAWS::ElastiCache::SubnetGroupで作成して、CacheSubnetGroupNameで指定してあげる。
マルチAZにしたい場合
ドキュメントに記載がある。
注記 2.8.6 より前のバージョンの Redis または T1 キャッシュノードタイプでは、自動フェイルオーバーを有効にすることはできません。 自動フェイルオーバーは、クラスターモードが有効で Redis バージョン 3.2.4 以降を実行している場合のみ、T2 ノードタイプでサポートされます。
つまりT2系の場合はクラスターモードが有効でないとマルチAZにできない。
m3系以上だとクラスターモードが無効でもマルチAZにできる。
キャッシュノード数を2以上にしたい場合
初めはAWS::ElastiCache::CacheClusterでRedisを構築しようとしていたが、ノード数2以上で作成しようとするの下記のようなエラーがでる。
NumCacheNodes should be 1 if engine is redis
キャッシュノード数を2以上にしたい場合は下記のAWS::ElastiCache::ReplicationGroupを使う必要があるらしい。
AWS::ElastiCache::ReplicationGroup - AWS CloudFormation
下記の記事も詳しくてよかった。
クラスターモード有効時
検証用にクラスターモードの場合も構築してみた。
ECacheRedis: Type: "AWS::ElastiCache::ReplicationGroup" Properties: AutomaticFailoverEnabled: true AutoMinorVersionUpgrade: true CacheNodeType: !Ref ECacheNodeType CacheParameterGroupName: default.redis3.2.cluster.on CacheSubnetGroupName: !Ref ECacheSubnetGroup Engine: redis EngineVersion: 3.2.10 NumNodeGroups: 2 NodeGroupConfiguration: - PrimaryAvailabilityZone: ap-northeast-1a ReplicaAvailabilityZones: - ap-northeast-1a - ap-northeast-1c - PrimaryAvailabilityZone: ap-northeast-1c ReplicaAvailabilityZones: - ap-northeast-1a - ap-northeast-1c ReplicasPerNodeGroup: 2 ReplicationGroupDescription: 'Elasticache Redis' ReplicationGroupId: !Ref ECacheClusterName SecurityGroupIds: - !Ref ECacheSecurityGroup SnapshotRetentionLimit: 3 SnapshotWindow: 'sun:13:30-sun:14:30'