AWS Elastic Beanstalk:如何在ebextensions中使用环境变量? [英] AWS Elastic Beanstalk: How to use environment variables in ebextensions?

查看:148
本文介绍了AWS Elastic Beanstalk:如何在ebextensions中使用环境变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试将特定于环境的应用程序配置文件存储在s3中. 文件存储在不同的子目录中,这些子目录以环境命名,并且环境也作为文件名的一部分.

We are trying to store environment specific application configuration files in s3. The files are stored in different subdirectories which are named after the environment and also have the environment as part of the file name.

示例是

dev/application-dev.properties
stg/application-stg.properties
prd/application-prd.properties

Elastic Beanstalk环境的名称为 dev,stg,prd ,或者我也有一个在Elastic Beanstalk中定义的名为ENVIRONMENT的环境变量,可以为 dev,stg或prd .

The Elastic Beanstalk environments are named dev, stg, prd and alternatively I also have an environment variable defined in Elastic Beanstalk named ENVIRONMENT which can be dev, stg or prd.

我现在的问题是,从.ebextensions中的配置文件下载配置文件时,如何引用环境名称或环境变量?

My question now is, how do I reference the environment name or ENVIRONMENT variable when downloading the configuration file from a config file in .ebextensions?

我尝试在.ebextensions/myapp.config中使用{"Ref": "AWSEBEnvironmentName" }引用,但是在部署时出现语法错误.

I tried using a {"Ref": "AWSEBEnvironmentName" } reference in .ebextensions/myapp.config but get a syntax error when deploying.

.ebextensions/myapp.config的内容为:

The content of .ebextensions/myapp.config is:

files:
  /config/application-`{"Ref": "AWSEBEnvironmentName" }`.properties:
    mode: "000666"
    owner: webapp
    group: webapp
    source: https://s3.amazonaws.com/com.mycompany.mybucket/`{"Ref": "AWSEBEnvironmentName" }`/application-`{"Ref": "AWSEBEnvironmentName" }`.properties 
    authentication: S3Access

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Access:
          type: S3
          roleName: aws-elasticbeanstalk-ec2-role
          buckets: com.mycompany.api.config

我得到的错误是:

The configuration file .ebextensions/myapp.config in application version
manualtest-18 contains invalid YAML or JSON. YAML exception: Invalid Yaml: 
mapping values are not allowed here in "<reader>", line 6, column 85: 
... .config/stg/application-`{"Ref": "AWSEBEnvironmentName" }`.prop ... ^ , 
JSON exception: Invalid JSON: Unexpected character (f) at position 0.. 
Update the configuration file.

在AWS Elastic Beanstalk中的.ebextensions配置文件中引用环境变量的正确方法是什么?

What is the correct way of referencing an environment variable in a .ebextensions config file in AWS Elastic Beanstalk?

推荐答案

我一直努力工作,直到发现Sub功能在ebextensions中似乎不可用:

I struggled to get this working, until I discovered that the Sub function doesn't appear to be available in ebextensions: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/ebextensions-functions.html

这意味着您需要回退到Fn::JoinRef,至少直到对ebextensions引入了对Sub的支持.似乎files属性需要固定路径(在这种情况下我不能使用Fn::Join).

This means that you need to fall back to Fn::Join and Ref, at least until support for Sub is introduced to ebextensions. It also seems that the files attribute requires a fixed path (and I couldn't use Fn::Join in this context).

我对此的总体解决方案如下:

My overall solution to this was as follows:

Resources:
  AWSEBAutoScalingGroup:
    Metadata:
      AWS::CloudFormation::Authentication:
        S3Auth:
          type: S3
          buckets: arn:aws:s3:::elasticbeanstalk-xxx
          roleName: aws-elasticbeanstalk-ec2-role

files:
  "/tmp/application.properties" :
    mode: "000644"
    owner: root
    group: root
    source: { "Fn::Join" : ["", ["https://s3-xxx.amazonaws.com/elasticbeanstalk-xxx/path/to/application-", { "Ref" : "AWSEBEnvironmentName" }, ".properties" ]]}
    authentication: S3Auth

container_commands:
  01-apply-configuration:
    command: mkdir -p config && mv /tmp/application.properties config

这将在已部署的应用程序实例旁边的config目录中生成一个application.properties文件(没有环境名称限定符).

This will result in an application.properties file (without the environment name qualifier) in a config directory next to the deployed application instance.

如果要使用这种方法将环境名称保留为文件名的一部分,则需要调整将文件移动的命令,以使用另一个Fn::Join表达式来控制文件名.

If you want to keep the name of the environment as part of the file name using this approach, you will need to adjust the command that moves the file to use another Fn::Join expression to control the filename.

这篇关于AWS Elastic Beanstalk:如何在ebextensions中使用环境变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆