How to Install and configure Redis on ElasticBeanstalk

后端 未结 2 1864
感情败类
感情败类 2020-12-28 18:25

How do I install and configure Redis on AWS ElasticBeanstalk? Does anyone know how to write an .ebextension script to accomplish that?

相关标签:
2条回答
  • 2020-12-28 18:50

    AWS Elastic Beanstalk does provide resource configuration via the .ebextensions folder. Essentially you need to tell Elastic Beanstalk what you would like provisioned in addition to your application. For provisioning into a default vpc. You need to

    create an .ebextensions folder

    add an elasticache.config file

    and include the following contents.

    Resources:
      MyCacheSecurityGroup:
        Type: "AWS::EC2::SecurityGroup"
        Properties:
          GroupDescription: "Lock cache down to webserver access only"
          SecurityGroupIngress :
            - IpProtocol : "tcp"
              FromPort :
                Fn::GetOptionSetting:
                  OptionName : "CachePort"
                  DefaultValue: "6379"
              ToPort :
                Fn::GetOptionSetting:
                  OptionName : "CachePort"
                  DefaultValue: "6379"
              SourceSecurityGroupName:
                Ref: "AWSEBSecurityGroup"
      MyElastiCache:
        Type: "AWS::ElastiCache::CacheCluster"
        Properties:
          CacheNodeType:
            Fn::GetOptionSetting:
              OptionName : "CacheNodeType"
              DefaultValue : "cache.t1.micro"
          NumCacheNodes:
            Fn::GetOptionSetting:
              OptionName : "NumCacheNodes"
              DefaultValue : "1"
          Engine:
            Fn::GetOptionSetting:
              OptionName : "Engine"
              DefaultValue : "redis"
          VpcSecurityGroupIds:
            -
              Fn::GetAtt:
                - MyCacheSecurityGroup
                - GroupId
    
    Outputs:
      ElastiCache:
        Description : "ID of ElastiCache Cache Cluster with Redis Engine"
        Value :
          Ref : "MyElastiCache"
    

    Referenced from: "How to add ElasticCache resources to Elastic Beanstalk VPC" http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-environment-resources-elasticache.html

    0 讨论(0)
  • 2020-12-28 19:03

    The accepted answer is great if you are using ElastiCache (like RDS, but for Memcached or Redis). But, if what you are trying to do is tell EB to provision Redis into the EC2 instance in which it spins up your app, you want a different config file, something like this gist:

    packages: 
      yum:
        gcc-c++: [] 
        make: []
    sources:
      /home/ec2-user: http://download.redis.io/releases/redis-2.8.4.tar.gz
    commands:
      redis_build:
        command: make
        cwd: /home/ec2-user/redis-2.8.4
      redis_config_001:
        command: sed -i -e "s/daemonize no/daemonize yes/" redis.conf
        cwd: /home/ec2-user/redis-2.8.4
      redis_config_002:
        command: sed -i -e "s/# maxmemory <bytes>/maxmemory 500MB/" redis.conf
        cwd: /home/ec2-user/redis-2.8.4
      redis_config_003:
        command: sed -i -e "s/# maxmemory-policy volatile-lru/maxmemory-policy allkeys-lru/" redis.conf
        cwd: /home/ec2-user/redis-2.8.4
      redis_server:
        command: src/redis-server redis.conf
        cwd: /home/ec2-user/redis-2.8.4
    

    IMPORTANT: The commands are executed in alphabetical order by name, so if you pick different names than redis_build, redis_config_xxx, redis_server, make sure they are such that they execute in the way you expect.

    Your other option is to containerize your app with Redis using Docker, then deploy your app as some number of Docker containers, instead of whatever language you wrote it in. Doing that for a Flask app is described here.

    You can jam it all into one container and deploy that way, which is easier, but doesn't scale well, or you can use AWS' Elastic Beanstalk multi-container deployments. If you have used docker-compose, you can use this tool to turn a docker-compose.yml into the form AWS wants, Dockerrun.aws.json.

    0 讨论(0)
提交回复
热议问题