---
AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  toCluster:
    Type: String
    Default: 'yes'
Conditions:
  isCluster: !Equals [!Ref 'toCluster', 'yes']
Resources:
  MyParameterGroup:
    Type: AWS::ElastiCache::ParameterGroup
    Properties:
      Description: "MyNewParameterGroup"
      CacheParameterGroupFamily: redis3.2
      Properties:
        cas_disabled: "1"
        cluster-enabled: !If [isCluster, 'no', 'yes']
  MyClusterParameterGroup:
    Type: AWS::ElastiCache::ParameterGroup
    Properties:
      Description: "MyNewParameterGroup"
      CacheParameterGroupFamily: redis3.2
      Properties:
        cas_disabled: "1"
        cluster-enabled: 'yes'
  MyNonClusterParameterGroup:
    Type: AWS::ElastiCache::ParameterGroup
    Properties:
      Description: "MyNewParameterGroup"
      CacheParameterGroupFamily: "memcached1.4"
      Properties:
        cas_disabled: "1"
        cluster-enabled: 'no'
  BasicReplicationGroup:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
      AutomaticFailoverEnabled: !If [isCluster, false, true]
      AutoMinorVersionUpgrade: true
      CacheParameterGroupName: !If [isCluster, !Ref MyClusterParameterGroup, !Ref MyNonClusterParameterGroup]
      CacheNodeType: cache.r3.large
      CacheSubnetGroupName: subnetgroup
      Engine: redis
      EngineVersion: '3.2'
      NumNodeGroups: 2
      ReplicasPerNodeGroup: 3
      Port: 6379
      PreferredMaintenanceWindow: sun:05:00-sun:09:00
      ReplicationGroupDescription: A sample replication group
      SecurityGroupIds:
      - sg-12345abc
      SnapshotRetentionLimit: 5
      SnapshotWindow: 10:00-12:00
  SecondReplicationGroup:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
      AutomaticFailoverEnabled: !If [isCluster, true, false]
      AutoMinorVersionUpgrade: true
      CacheParameterGroupName: !If [isCluster, !Ref MyNonClusterParameterGroup, !Ref MyClusterParameterGroup]
      CacheNodeType: cache.r3.large
      CacheSubnetGroupName: subnetgroup
      Engine: redis
      EngineVersion: '3.2'
      NumCacheClusters: 1
      Port: 6379
      PreferredMaintenanceWindow: sun:05:00-sun:09:00
      ReplicationGroupDescription: A sample replication group
      SecurityGroupIds:
      - sg-12345abc
      SnapshotRetentionLimit: 5
      SnapshotWindow: 10:00-12:00
  # Error when no cache or node groups specified
  ThirdReplicationGroup:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
      AutomaticFailoverEnabled: true
      AutoMinorVersionUpgrade: true
      CacheParameterGroupName: !Ref MyClusterParameterGroup
      CacheNodeType: cache.r3.large
      CacheSubnetGroupName: subnetgroup
      Engine: redis
      EngineVersion: '3.2'
      Port: 6379
      PreferredMaintenanceWindow: sun:05:00-sun:09:00
      ReplicationGroupDescription: A sample replication group
      SecurityGroupIds:
      - sg-12345abc
      SnapshotRetentionLimit: 5
      SnapshotWindow: 10:00-12:00
  FourtReplicationGroup:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
      AutomaticFailoverEnabled: true
      AutoMinorVersionUpgrade: true
      CacheParameterGroupName: !Ref MyClusterParameterGroup
      CacheNodeType: cache.r3.large
      CacheSubnetGroupName: subnetgroup
      NumCacheClusters: 1  # Failure without scenario
      Engine: redis
      EngineVersion: '3.2'
      Port: 6379
      PreferredMaintenanceWindow: sun:05:00-sun:09:00
      ReplicationGroupDescription: A sample replication group
      SecurityGroupIds:
      - sg-12345abc
      SnapshotRetentionLimit: 5
      SnapshotWindow: 10:00-12:00
