--- AWSTemplateFormatVersion: 2010-09-09 Description: Reference Architecture to host Moodle on AWS - Creates ElastiCache cache cluster Metadata: AWS::CloudFormation::Interface: ParameterGroups: - Label: default: Cache Parameters Parameters: - CacheUsageType - CacheEngineType - ElastiCacheNodeType - ElastiCacheClusterName - ElastiCacheSecurityGroup - NumberOfSubnets - Subnet ParameterLabels: CacheEngineType: default: Cache Engine Type CacheUsageType: default: Cache Usage Type ElastiCacheClusterName: default: Cache Cluster Name ElastiCacheNodeType: default: Cache Cluster Node Type ElastiCacheSecurityGroup: default: ElastiCache Security Group NumberOfSubnets: default: Number of subnets Subnet: default: Subnets Parameters: CacheEngineType: AllowedValues: - Redis - Memcached Default: Memcached Description: Indicates whether to use ElastiCache Memcached or Redis. Type: String CacheUsageType: AllowedValues: - application - session Default: session Description: Indicates whether to use ElastiCache for session or application caching. Type: String ElastiCacheClusterName: AllowedPattern: ^([a-zA-Z0-9]*)$ Description: ElastiCache cluster name. Type: String ElastiCacheNodeType: AllowedValues: - cache.t3.micro - cache.t3.small - cache.t3.medium - cache.m5.large - cache.m5.xlarge - cache.m5.2xlarge - cache.m5.4xlarge - cache.m5.12xlarge - cache.m5.24xlarge - cache.m6g.large - cache.m6g.xlarge - cache.m6g.2xlarge - cache.m6g.4xlarge - cache.m6g.8xlarge - cache.m6g.12xlarge - cache.m6g.16xlarge ConstraintDescription: Must be a valid Amazon ElastiCache node type. Default: cache.m6g.large Description: ElastiCache cluster node type. Type: String ElastiCacheSecurityGroup: Description: Select the ElastiCache security group. Type: AWS::EC2::SecurityGroup::Id NumberOfSubnets: AllowedValues: - 1 - 2 - 3 Default: 2 Description: Number of subnets. This must match your selections in the list of subnets below. Type: String Subnet: Description: Select existing subnets. The number selected must match the number of subnets above. Subnets selected must be in separate AZs. Type: List ProjectName: AllowedPattern: ^([a-zA-Z0-9]*)$ Default: App Description: Moodle Project Name Type: String Conditions: UseRedis: !Equals [!Ref CacheEngineType, Redis] UseMemcached: !Equals [!Ref CacheEngineType, Memcached] NumberOfSubnets1: !Equals [ 1, !Ref NumberOfSubnets ] NumberOfSubnets2: !Equals [ 2, !Ref NumberOfSubnets ] NumberOfSubnets3: !Equals [ 3, !Ref NumberOfSubnets ] Subnet0: !Or - !Condition NumberOfSubnets1 - !Condition NumberOfSubnets2 - !Condition NumberOfSubnets3 Subnet1: !Or - !Condition NumberOfSubnets2 - !Condition NumberOfSubnets3 Subnet2: !Condition NumberOfSubnets3 Resources: ElastiCacheClusterRedis: Condition: UseRedis Type: AWS::ElastiCache::ReplicationGroup Properties: ReplicationGroupId: !Ref ElastiCacheClusterName ReplicationGroupDescription: !Ref ElastiCacheClusterName AtRestEncryptionEnabled: true TransitEncryptionEnabled: false AutoMinorVersionUpgrade: true MultiAZEnabled: true CacheNodeType: !Ref ElastiCacheNodeType CacheSubnetGroupName: !Ref ElastiCacheSubnetGroup SecurityGroupIds: - !Ref ElastiCacheSecurityGroup Engine: redis NumCacheClusters: !Ref NumberOfSubnets ElastiCacheClusterMemcached: Condition: UseMemcached Type: AWS::ElastiCache::CacheCluster Properties: AZMode: cross-az CacheNodeType: !Ref ElastiCacheNodeType CacheSubnetGroupName: !Ref ElastiCacheSubnetGroup ClusterName: !Ref ElastiCacheClusterName Engine: memcached NumCacheNodes: !Ref NumberOfSubnets Tags: - Key: Name Value: !Join [ '', [ 'Moodle / ', !Ref 'AWS::StackName' ] ] VpcSecurityGroupIds: - !Ref ElastiCacheSecurityGroup ElastiCacheSubnetGroup: Type: AWS::ElastiCache::SubnetGroup Properties: CacheSubnetGroupName: !Join [ '', [ !Ref ElastiCacheClusterName, SubnetGroup ] ] Description: ElastiCache Subnet Group for Moodle SubnetIds: !If [ NumberOfSubnets1, [ !Select [ 0, !Ref Subnet ] ], !If [ NumberOfSubnets2, [ !Select [ 0, !Ref Subnet ], !Select [ 1, !Ref Subnet ] ], [ !Select [ 0, !Ref Subnet ], !Select [ 1, !Ref Subnet ], !Select [ 2, !Ref Subnet ] ] ] ] ElastiCacheClusterEndpoint: Type: AWS::SSM::Parameter Properties: Name: !Join [ '', [ '/Moodle/',!Ref ProjectName, '/Cache/', !Ref CacheUsageType, '/ElastiCacheClusterEndpoint' ] ] Type: String Value: !If [UseRedis, !Join [ ':', [!GetAtt ElastiCacheClusterRedis.PrimaryEndPoint.Address, !GetAtt ElastiCacheClusterRedis.PrimaryEndPoint.Port]], !Join [ ':', [!GetAtt ElastiCacheClusterMemcached.ConfigurationEndpoint.Address, !GetAtt ElastiCacheClusterMemcached.ConfigurationEndpoint.Port]] ] Description: SSM Parameter for ElastiCache Cluster Endpoint ElastiCacheEngine: Type: AWS::SSM::Parameter Properties: Name: !Join [ '', [ '/Moodle/',!Ref ProjectName, '/Cache/', !Ref CacheUsageType, '/Engine' ] ] Type: String Value: !Ref CacheEngineType Description: SSM Parameter for ElastiCache Engine Type Outputs: ElastiCacheClusterEndpointAddress: Value: !If [UseRedis, !Join [ ':', [!GetAtt ElastiCacheClusterRedis.PrimaryEndPoint.Address, !GetAtt ElastiCacheClusterRedis.PrimaryEndPoint.Port]], !Join [ ':', [!GetAtt ElastiCacheClusterMemcached.ConfigurationEndpoint.Address, !GetAtt ElastiCacheClusterMemcached.ConfigurationEndpoint.Port]] ]