Search code examples
amazon-web-servicesaws-cloudformationamazon-elasticache

AWS CloudFormation: unable to create ElasticCache


I am creating an ElasticCache memcached resource using CloudFomration. But it is failing when I deployed the template.

This is my template

  ElasticCacheSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupDescription: Subnet Group to specify the subnets for elastic cache
      SubnetIds:
        - !Ref DatabaseSubnet1
        - !Ref DatabaseSubnet2
  ElasticCacheSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref Vpc
      GroupDescription: Enable TCP connection on port 3306 for database connection
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '3306'
          ToPort: '3306'
          CidrIp: 0.0.0.0/0
  ElasticCacheCluster:
    Type: AWS::ElastiCache::CacheCluster
    Properties:
      AZMode: cross-az
      CacheNodeType: cache.t2.small
      Engine: memcached
      NumCacheNodes: '3'
      CacheSubnetGroupName: !Ref ElasticCacheSubnetGroup
      VpcSecurityGroupIds:
        - !Ref ElasticCacheSecurityGroup
      PreferredAvailabilityZones:
        - !Select
          - 0
          - Fn::GetAZs: !Ref AWS::Region
        - !Select
          - 1
          - Fn::GetAZs: !Ref AWS::Region

This is the error I got in the log.

 {
            "StackId": "arn:aws:cloudformation:eu-west-1:733553390213:stack/threetierwebapp/c88ea6d0-d029-11ea-9279-02162fdbb6ee", 
            "EventId": "eed78640-d029-11ea-8cd8-02e056ab1688", 
            "ResourceStatus": "ROLLBACK_IN_PROGRESS", 
            "ResourceType": "AWS::CloudFormation::Stack", 
            "Timestamp": "2020-07-27T16:55:10.741Z", 
            "ResourceStatusReason": "The following resource(s) failed to create: [ElasticCacheCluster]. . Rollback requested by user.", 
            "StackName": "threetierwebapp", 
            "PhysicalResourceId": "arn:aws:cloudformation:eu-west-1:733553390213:stack/threetierwebapp/c88ea6d0-d029-11ea-9279-02162fdbb6ee", 
            "LogicalResourceId": "threetierwebapp"
        }, 
        {
            "StackId": "arn:aws:cloudformation:eu-west-1:733553390213:stack/threetierwebapp/c88ea6d0-d029-11ea-9279-02162fdbb6ee", 
            "EventId": "ElasticCacheCluster-CREATE_FAILED-2020-07-27T16:55:09.946Z", 
            "ResourceStatus": "CREATE_FAILED", 
            "ResourceType": "AWS::ElastiCache::CacheCluster", 
            "Timestamp": "2020-07-27T16:55:09.946Z", 
            "ResourceStatusReason": "Cache Subnet Group threetierwebapp-elasticcachesubnetgroup-1hxvajmdjip1i does not exist. (Service: AmazonElastiCache; Status Code: 400; Error Code: CacheSubnetGroupNotFoundFault; Request ID: 790019b8-2ed8-4748-9c38-7c1eec121251)", 
            "StackName": "threetierwebapp", 
            "ResourceProperties": "{\"CacheNodeType\":\"cache.t2.small\",\"CacheSubnetGroupName\":\"threetierwebapp-elasticcachesubnetgroup-1hxvajmdjip1i\",\"VpcSecurityGroupIds\":[\"sg-0603c1f4b76c8afde\"],\"PreferredAvailabilityZones\":[\"eu-west-1a\",\"eu-west-1b\",\"eu-west-1c\"],\"NumCacheNodes\":\"3\",\"Engine\":\"memcached\",\"AZMode\":\"cross-az\"}", 
            "PhysicalResourceId": "", 
            "LogicalResourceId": "ElasticCacheCluster"
        }, 

What is wrong with my template and how can I fix it?


Solution

  • You have assigned a AWS::RDS::DBSubnetGroup for your ElasticCacheSubnetGroup resource.

    Instead swap this to a AWS::ElastiCache::SubnetGroup and remove the DBSubnetGroupDescription property from this.

    This would look like the below

    
      ElasticCacheSubnetGroup:
        Type: AWS::ElastiCache::SubnetGroup
        Properties:
          SubnetIds:
            - !Ref DatabaseSubnet1
            - !Ref DatabaseSubnet2
      ElasticCacheSecurityGroup:
        Type: AWS::EC2::SecurityGroup
        Properties:
          VpcId: !Ref Vpc
          GroupDescription: Enable TCP connection on port 3306 for database connection
          SecurityGroupIngress:
            - IpProtocol: tcp
              FromPort: '3306'
              ToPort: '3306'
              CidrIp: 0.0.0.0/0
      ElasticCacheCluster:
        Type: AWS::ElastiCache::CacheCluster
        Properties:
          AZMode: cross-az
          CacheNodeType: cache.t2.small
          Engine: memcached
          NumCacheNodes: '3'
          CacheSubnetGroupName: !Ref ElasticCacheSubnetGroup
          VpcSecurityGroupIds:
            - !Ref ElasticCacheSecurityGroup
          PreferredAvailabilityZones:
            - !Select
              - 0
              - Fn::GetAZs: !Ref AWS::Region
            - !Select
              - 1
              - Fn::GetAZs: !Ref AWS::Region
            - !Select
              - 2
              - Fn::GetAZs: !Ref AWS::Region