File size: 4,128 Bytes
43cd3eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates a Redis cluster with Multi-AZ in a new VPC

Resources:
  # Create a NEW VPC with both Public an Private Subnets
  VPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/16
      EnableDnsHostnames: true
      EnableDnsSupport: true
      InstanceTenancy: default

  # Create the first public subnet in the VPC with a CIDR block of 10.0.1.0/24
  PublicSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: 10.0.1.0/24
      MapPublicIpOnLaunch: true

  # Create the second public subnet in the VPC with a CIDR block of 10.0.2.0/24
  PublicSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs '' ]
      CidrBlock: 10.0.2.0/24
      MapPublicIpOnLaunch: true

  # Create the first private subnet in the VPC with a CIDR block of 10.0.10.0/24
  PrivateSubnet1:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 0, !GetAZs '' ]
      CidrBlock: 10.0.10.0/24
      MapPublicIpOnLaunch: false

  # Create the second private subnet in the VPC with a CIDR block of 10.0.11.0/24
  PrivateSubnet2:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId: !Ref VPC
      AvailabilityZone: !Select [ 1, !GetAZs '' ]
      CidrBlock: 10.0.11.0/24
      MapPublicIpOnLaunch: false

  # Create a Security Group for the Redis cluster allowing Ingress and Egress to "0.0.0.0/0" on port "6379"
  RedisSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Redis Security Group
      VpcId: !Ref VPC
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 6379
          ToPort: 6379
          CidrIp: 0.0.0.0/0
      SecurityGroupEgress:
        - IpProtocol: tcp
          FromPort: 6379
          ToPort: 6379
          CidrIp: 0.0.0.0/0

  # Create a subnet group for the Redis cluster using the 2 private subnets from the VPC
  RedisSubnetGroup:
    Type: AWS::ElastiCache::SubnetGroup
    Properties:
      Description: Subnets available for the Redis cluster
      SubnetIds:
        - !Ref PrivateSubnet1
        - !Ref PrivateSubnet2

  # Create a parameter group for the Redis cluster using Group family "redis6.x"
  RedisParameterGroup:
    Type: AWS::ElastiCache::ParameterGroup
    Properties:
      CacheParameterGroupFamily: redis7
      Description: Parameter group for Redis cluster

  # Create a Redis cluster using the newly created parameter group and subnet group, with a NumCacheClusters "2"
  ElastiCacheCluster:
    Type: AWS::ElastiCache::ReplicationGroup
    Properties:
        AutomaticFailoverEnabled: true
        AutoMinorVersionUpgrade: true
        CacheParameterGroupName: !Ref RedisParameterGroup
        CacheNodeType: cache.t3.micro
        CacheSubnetGroupName: !Ref RedisSubnetGroup
        Engine: 'redis'
        NumCacheClusters: 2
        Port: 6379
        ReplicationGroupDescription: 'Redis cluster'
        SecurityGroupIds:
            - !Ref RedisSecurityGroup

Outputs:
  # Create the output for the DNS address of the primary read-write cache node
  PrimaryAddress:
      Description: 'The DNS address of the primary read-write cache node.'
      Value: !GetAtt ElastiCacheCluster.PrimaryEndPoint.Address

  # Create the output for the port number of the primary read-write cache node of the read-only replica nodes
  PrimaryPort:
      Description: 'The number of the port that the primary read-write cache engine is listening on.'
      Value: !GetAtt ElastiCacheCluster.PrimaryEndPoint.Port

  # Create the output for the DNS addresses of the read-only replica nodes
  SecondaryAddresses:
      Description: 'A string with a list of endpoints for the read-only replicas.'
      Value: !GetAtt ElastiCacheCluster.ReadEndPoint.Addresses

  # Create the output for the port number of the read-only replica nodes
  SecondaryPorts:
      Description: 'A string with a list of ports for the read-only replicas. '
      Value: !GetAtt ElastiCacheCluster.ReadEndPoint.Ports