Reputation: 1020
I have a Tomcat + Redis app running on Elastic Beanstalk. In my code, I create a Redis client using
pool = new JedisPool(new JedisPoolConfig(), "localhost");
Of course, in production, I can't use "localhost" I've read through Customizing the Beanstalk Environment and about getting autodiscovery to work with your code, but that seems to only apply to Memcached.
Has anyone gotten a Redis ElastiCache cluster configured with Beanstalk. Where do you get the DNS address to use instead of "localhost"?
Upvotes: 0
Views: 2207
Reputation: 3968
I'm afraid that you can't get the AWS ElastiCache endpoint easily (e.g: environment variable).
From AWS documentation on Deploying an Express Application with Clustering to Elastic Beanstalk, we can follow the NodeJS + ElastiCache Memcache solution. Create the two following .ebextensions
(read more here) scripts in your root project:
.ebextenstions/elasticache-iam-with-script.config
##
# This sample adds an ElastiCache cluster to the environment.
# It creates an IAM user with the permisisons required to discover the elasticache nodes.
# It provides a cfn-hup responder if the cache changes to rewrite the file
# It writes a file out to: /var/www/html/nodelist
# containing the cache nodes on startup and when the cache changes (through a cfn/eb update)
#
# Customers would generally not edit this file.
# Instead, they would have another file sitting in the same directory (or anywhere) with
# an option-settings section such as the following (all of these are showing the default value)
#
# option-settings:
# "aws:elasticbeanstalk:customoption" :
# CacheNodeType : cache.m1.small
# NumCacheNodes : 1
# Engine : memcached
# NodeListPath : /var/www/html/nodelist
#
# Issues:
# Requires ElastiCache CLI latest URL to point to version 1.7
##
Resources:
MyElastiCache:
Type: AWS::ElastiCache::CacheCluster
Properties:
CacheNodeType:
Fn::GetOptionSetting:
OptionName : CacheNodeType
DefaultValue: cache.m1.small
NumCacheNodes:
Fn::GetOptionSetting:
OptionName : NumCacheNodes
DefaultValue: 1
Engine:
Fn::GetOptionSetting:
OptionName : Engine
DefaultValue: memcached
CacheSecurityGroupNames:
- Ref: MyCacheSecurityGroup
MyCacheSecurityGroup:
Type: AWS::ElastiCache::SecurityGroup
Properties:
Description: "Lock cache down to webserver access only"
MyCacheSecurityGroupIngress:
Type: AWS::ElastiCache::SecurityGroupIngress
Properties:
CacheSecurityGroupName:
Ref: MyCacheSecurityGroup
EC2SecurityGroupName:
Ref: AWSEBSecurityGroup
AWSEBAutoScalingGroup :
Metadata :
ElastiCacheConfig :
CacheName :
Ref : MyElastiCache
CacheSize :
Fn::GetOptionSetting:
OptionName : NumCacheNodes
DefaultValue: 1
WebServerUser :
Type : AWS::IAM::User
Properties :
Path : "/"
Policies:
-
PolicyName: root
PolicyDocument :
Statement :
-
Effect : Allow
Action :
- cloudformation:DescribeStackResource
- cloudformation:ListStackResources
- elasticache:DescribeCacheClusters
Resource : "*"
WebServerKeys :
Type : AWS::IAM::AccessKey
Properties :
UserName :
Ref: WebServerUser
Outputs:
WebsiteURL:
Description: sample output only here to show inline string function parsing
Value: |
http://`{ "Fn::GetAtt" : [ "AWSEBLoadBalancer", "DNSName" ] }`
MyElastiCacheName:
Description: Name of the elasticache
Value:
Ref : MyElastiCache
NumCacheNodes:
Description: Number of cache nodes in MyElastiCache
Value:
Fn::GetOptionSetting:
OptionName : NumCacheNodes
DefaultValue: 1
files:
"/etc/cfn/cfn-credentials" :
content : |
AWSAccessKeyId=`{ "Ref" : "WebServerKeys" }`
AWSSecretKey=`{ "Fn::GetAtt" : ["WebServerKeys", "SecretAccessKey"] }`
mode : "000400"
owner : root
group : root
"/etc/cfn/get-cache-nodes" :
content : |
# Define environment variables for command line tools
export AWS_ELASTICACHE_HOME="/home/ec2-user/elasticache/$(ls /home/ec2-user/elasticache/)"
export AWS_CLOUDFORMATION_HOME=/opt/aws/apitools/cfn
export PATH=$AWS_CLOUDFORMATION_HOME/bin:$AWS_ELASTICACHE_HOME/bin:$PATH
export AWS_CREDENTIAL_FILE=/etc/cfn/cfn-credentials
export JAVA_HOME=/usr/lib/jvm/jre
# Grab the Cache node names and configure the PHP page
cfn-list-stack-resources `{ "Ref" : "AWS::StackName" }` --region `{ "Ref" : "AWS::Region" }` | grep MyElastiCache | awk '{print $3}' | xargs -I {} elasticache-describe-cache-clusters {} --region `{ "Ref" : "AWS::Region" }` --show-cache-node-info | grep CACHENODE | awk '{print $4 ":" $5}' > `{ "Fn::GetOptionSetting" : { "OptionName" : "NodeListPath", "DefaultValue" : "/var/www/html/nodelist" } }`
mode : "000500"
owner : root
group : root
"/etc/cfn/hooks.d/cfn-cache-change.conf" :
"content": |
[cfn-cache-size-change]
triggers=post.update
path=Resources.AWSEBAutoScalingGroup.Metadata.ElastiCacheConfig
action=/etc/cfn/get-cache-nodes
runas=root
sources :
"/home/ec2-user/elasticache" : "https://s3.amazonaws.com/elasticache-downloads/AmazonElastiCacheCli-latest.zip"
commands:
make-elasticache-executable:
command: chmod -R ugo+x /home/ec2-user/elasticache/*/bin/*
packages :
"yum" :
"aws-apitools-cfn" : []
container_commands:
initial_cache_nodes:
command: /etc/cfn/get-cache-nodes
.ebextensions/elasticache-settings.config
option_settings:
"aws:elasticbeanstalk:customoption" :
CacheNodeType : cache.t2.micro
NumCacheNodes : 1
Engine : redis
NodeListPath : /var/nodelist
The first script (elasticache-iam-with-script.config) is used to create an ElastiCache resource which is attached to your Elastic Beanstalk environment.
The second script (elasticache-settings.config) is the custom configuration file for the first script. You may change the configuration as your need.
The first script will generate a file in /var/nodelist. The content of file looks like:
aws-my-xxxxxxxxxxxxx.xxxxxx.0001.use1.cache.amazonaws.com:us-east-1b
It's an endpoint and AZ pair which is separated by an colon (<endpoint>:<AZ>
). Your Java may parse /var/nodelist and take the endpoint
into the JedisPool
.
Upvotes: 3