Nik*_*Nik 10 java environment-variables amazon-web-services amazon-elastic-beanstalk ebextensions
我有一个spring-boot应用程序,我需要指定石墨服务器和端口(发送指标).要做到这一点,我必须安装和配置statsd.我这样做是使用该ebextensions文件.
commands:
01_nodejs_install:
command: sudo yum -y install nodejs npm --enablerepo=epel
ignoreErrors: true
02_mkdir_statsd:
command: mkdir /home/ec2-user/statsd
ignoreErrors: true
03_fetch_statsd:
command: git clone https://github.com/etsy/statsd.git /home/ec2-user/statsd
ignoreErrors: true
04_change_example_config:
command: "cat exampleConfig.js | sed 's/2003/<graphite-port>/g' | sed 's/graphite.example.com/<my-graphite-server>/g' > config.js"
cwd: /home/ec2-user/statsd
05_run_statsd:
command: setsid node stats.js config.js >/dev/null 2>&1 < /dev/null &
cwd: /home/ec2-user/statsd
Run Code Online (Sandbox Code Playgroud)
这种配置的问题是我可以在这里为所有环境仅指定1个石墨服务器.
所以我决定将命令04和05移入container_commands.我想定义叫做环境变量的ENV_NAME 使用魔豆控制台/ UI,并将其设置为dev,qa或prod根据环境.然后我可以使用test选项container_commands仅基于此特定环境运行04和05命令ENV_NAME.
所以我的问题是 - 我如何使用AWS控制台来定义环境变量?我尝试使用Benastalk控制台来定义我的变量,如此处的文档中所述, 但它不起作用.我还发现(请参阅5个upvotes的答案)此方法仅设置JVM属性而不设置ENV变量.
我无法定义环境变量使用ebextensions因为那时我会遇到同样的问题 - 不能为不同的envs定义不同的env变量:)
所以我需要帮助:
ENV_NAME使用beanstalk UI 设置环境变量.要么
ENV_NAME系统属性的方法来container_commands根据值来判断是否运行命令ENV_NAME.如果您知道为不同环境指定不同Graphite服务器的更简单/更好的方法,请随时参与.
Nik*_*Nik 21
我解决这个问题的方法是分别定义ENV_NAMEas dev和proddev和prod环境,并使用以下ebextensions配置.
commands:
01_nodejs_install:
command: sudo yum -y install nodejs npm --enablerepo=epel
ignoreErrors: true
02_mkdir_statsd:
command: mkdir /home/ec2-user/statsd
ignoreErrors: true
03_fetch_statsd:
command: git clone https://github.com/etsy/statsd.git /home/ec2-user/statsd
ignoreErrors: true
container_commands:
04a_container_change_example_config:
command: "cat exampleConfig.js | sed 's/2003/<graphite-dev-port>/g' | sed 's/graphite.example.com/<graphite-dev-host>/g' > config.js"
cwd: /home/ec2-user/statsd
test: '[ "${ENV_NAME}" == "dev" ]'
04b_container_change_example_config:
command: "cat exampleConfig.js | sed 's/2003/<graphite-prod-port>/g' | sed 's/graphite.example.com/<graphite-prod-host>/g' > config.js"
cwd: /home/ec2-user/statsd
test: '[ "${ENV_NAME}" == "prod" ]'
05_run_statsd:
command: setsid node stats.js config.js >/dev/null 2>&1 < /dev/null &
cwd: /home/ec2-user/statsd
Run Code Online (Sandbox Code Playgroud)
使用test我可以调节我已经在beanstalk环境中定义container command的ENV_NAME属性的执行.
除了@Nik的答案:
除了手动添加环境变量外ENV_NAME,您还可以获取实际的环境名称并将其ENV_NAME自动存储在其中。这是通过option_settings在您的ebextensions配置文件中使用来实现的。
例如:
option_settings:
aws:elasticbeanstalk:application:environment:
ENV_NAME: '`{ "Ref" : "AWSEBEnvironmentName" }`' # assign the actual env name to ENV_NAME
container_commands:
0100_execute_only_in_dev:
command: echo "this is the development environment" # this will turn up in ebactivity.log
test: '[[ $ENV_NAME = "dev" ]]'
Run Code Online (Sandbox Code Playgroud)
附带说明,对于像我这样不熟悉shell脚本的人来说:测试表达式中的空格很重要(示例)。
| 归档时间: |
|
| 查看次数: |
4297 次 |
| 最近记录: |