恐怕您无法轻松获取 AWS ElastiCache 端点(例如:环境变量)。
从Deploying an Express Application with Clustering to Elastic Beanstalk 上的 AWS 文档中,我们可以遵循 NodeJS + ElastiCache Memcache 解决方案。在您的根项目中创建以下两个.ebextensions(阅读更多here)脚本:
.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
第一个脚本 (elasticache-iam-with-script.config) 用于创建附加到 Elastic Beanstalk 环境的 ElastiCache 资源。
第二个脚本 (elasticache-settings.config) 是第一个脚本的自定义配置文件。您可以根据需要更改配置。
第一个脚本将在 /var/nodelist 中生成一个文件。文件内容如下:
aws-my-xxxxxxxxxxxxx.xxxxxx.0001.use1.cache.amazonaws.com:us-east-1b
这是一个端点和 AZ 对,用冒号 (<endpoint>:<AZ>) 分隔。您的 Java 可能会解析 /var/nodelist 并将endpoint 放入JedisPool。