Could we get an example of a Redis Elasticache addon? #2011
-
| Hi I wanted to make a caching layer for my backend (private) service using elasticache + redis. Could we get an example of a simple addon yaml file that creates a 50mb redis elasticache instance that respects the default copilot backend service private network setup? All other settings such as replication can be assumed to be whatever you want, I just want to see how the most barebones elastic cache redis addon file looks like 😄 | 
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
| Hi @DennisSSDev ! Sorry this is a bit late but here is a working template for using a private elasticache cluster! Addon template for RedisParameters:
  App:
    Type: String
    Description: Your application's name.
  Env:
    Type: String
    Description: The environment name your service, job, or workflow is being deployed to.
  Name:
    Type: String
    Description: The name of the service, job, or workflow being deployed.
Resources:
  # Subnet group to control where the Redis gets placed
  RedisSubnetGroup:
    Type: AWS::ElastiCache::SubnetGroup
    Properties:
      Description: Group of subnets to place Redis into
      SubnetIds: !Split [ ',', { 'Fn::ImportValue': !Sub '${App}-${Env}-PrivateSubnets' } ]
  
  # Security group to add the Redis cluster to the VPC,
  # and to allow the Fargate containers to talk to Redis on port 6379
  RedisSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: "Redis Security Group"
      VpcId: { 'Fn::ImportValue': !Sub '${App}-${Env}-VpcId' }
  
  # Enable ingress from other ECS services created within the environment.
  RedisIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      Description: Ingress from Fargate containers
      GroupId: !Ref 'RedisSecurityGroup'
      IpProtocol: tcp
      FromPort: 6379
      ToPort: 6379
      SourceSecurityGroupId: { 'Fn::ImportValue': !Sub '${App}-${Env}-EnvironmentSecurityGroup' }
  # The cluster itself.
  Redis:
    Type: AWS::ElastiCache::CacheCluster
    Properties:
      Engine: redis
      CacheNodeType: cache.m4.large
      NumCacheNodes: 1
      CacheSubnetGroupName: !Ref 'RedisSubnetGroup'
      VpcSecurityGroupIds:
        - !GetAtt 'RedisSecurityGroup.GroupId'
  # Redis endpoint stored in SSM so that other services can retrieve the endpoint.
  RedisEndpointAddressParam:
    Type: AWS::SSM::Parameter
    Properties:
      Name: !Sub '/${App}/${Env}/${Name}/redis'   # Other services can retrieve the endpoint from this path.
      Type: String
      Value: !GetAtt 'Redis.RedisEndpoint.Address'
Outputs:
  RedisEndpoint:
    Description: The endpoint of the redis cluster
    Value: !GetAtt 'Redis.RedisEndpoint.Address'This will create an elasticache cluster that's available to all your Copilot services in your VPC. If you want other services to access this cluster you can inject the endpoint in the manifest like this: secrets:
  REDIS_ENDPOINT: /{app}/{env}/{main service name}/redisHope this helps! | 
Beta Was this translation helpful? Give feedback.
-
| I tried to run this example and am running into the following error when the service stack tries to update (the add on stack is successfully setting up): Any ideas? | 
Beta Was this translation helpful? Give feedback.
Hi @DennisSSDev !
Sorry this is a bit late but here is a working template for using a private elasticache cluster!
https://gist.github.com/efekarakus/fe694581c88e03eedb38f17b8ddc68a7
Addon template for Redis