【问题标题】:Referencing env variables from Elastic Beanstalk .ebextensions config files从 Elastic Beanstalk .ebextensions 配置文件中引用环境变量
【发布时间】:2023-03-26 17:19:01
【问题描述】:

是否可以从 .ebextensions 配置文件中引用 PARAM1 / PARAM2 等容器环境属性。如果是这样,怎么做?我试过 $PARAM1 但它似乎是一个空值。

我想在启动时将主机名设置为包含 DEV、QA 或 PROD,我通过 PARAM1 环境变量将它们传递给我的容器。

commands:
  01-set-correct-hostname:
    command: hostname myappname{$PARAM1}.com

【问题讨论】:

  • 为什么仍然没有答案?!?!?下面只告诉你如何使用contaienr_comands 而不是commands

标签: amazon-web-services amazon-elastic-beanstalk


【解决方案1】:

事实证明,您只能在 container_commands 部分执行此操作,而不是 commands 部分。

这行得通:

container_commands:
  01-set-correct-hostname:
    command: "hostname myappname{$PARAM1}.com"

更多详情请见http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#customize-containers-format-container_commands

【讨论】:

  • 该链接没有提及在容器命令中使用环境变量的任何内容。
  • @Nate 阅读 Container_Commands 部分,其中显示“他们还可以访问环境变量,例如您的 AWS 安全凭证。”
  • 如果您的 beanstalk 配置生成多个 ec2 实例,您不会遇到主机名问题吗?
  • 这对普通的commands: 有效吗?我的测试表明不是。
  • 我认为这不再有效,因为这些参数不再是环境变量而是应用程序变量。
【解决方案2】:

这对我有用。我尝试了接受的方法,但没有产生预期的结果(输出中包含大括号)。对上传到 Elastic Beanstalk 时从 .config 文件执行的命令进行故障排除也是一个挑战(或者我只是不知道该去哪里查看)。

AWS 环境:

  • 类型 - Elastic Beanstalk
  • 平台 - 运行 PHP 5.6 的 64 位 Amazon Linux 2015.09 v2.0.4

Elastic Beanstalk 环境属性(配置 -> 软件配置 -> 环境属性):

  • 属性名称 - HELLO_VARIABLE
  • 属性值 - 测试

部署工件的 .ebextensions 文件夹中包含的示例 .config 文件:

container_commands:
  0_test-variable:
    cwd: /tmp
    command: "touch ${HELLO_VARIABLE}_0_.txt"
  1_test-variable:
    cwd: /tmp
    command: "touch {$HELLO_VARIABLE}_1_.txt"
  2_test-variable:
    cwd: /tmp
    command: "touch $HELLO_VARIABLE_2_.txt"

使用 Elastic Beanstalk 部署工件后,EC2 实例中的 /tmp 目录将包含以下文件(注意大括号和 $ 的位置):

  • touch ${HELLO_VARIABLE}_0_.txt 创建 /tmp/test_0_.txt
  • 触摸 {$HELLO_VARIABLE}_1_.txt 创建 /tmp/{test}_1_.txt
  • touch $HELLO_VARIABLE_2_.txt 创建 /tmp/.txt

【讨论】:

  • 在日志文件中看不到命令​​输出的原因是因为 EB 仅包含特定的日志文件。但是,您可以将 cfn-init-cmd.log 添加到包中。我在这里为 Windows 解释它,但你应该能够弄清楚如何为 Linux 更改它。 stackoverflow.com/a/37189606/674488
  • 查看 /var/log/eb-activity.log 进行故障排除
【解决方案3】:

为了使环境变量在命令阶段可用,我将它们解析为 bash 源文件。

000001.envvars.config

...
commands:
  000001_envvars_to_bash_source_file:
    command: |
      # source our elastic beanstalk environment variables
      /opt/elasticbeanstalk/bin/get-config --output YAML environment|perl -ne "/^\w/ or next; s/: /=/; print qq|\$_|" > /var/tmp/envvars
      chmod 400 /var/tmp/envvars
...

然后我使用:-

source /var/tmp/envvars

在后续命令中。

【讨论】:

    【解决方案4】:

    接受的答案已经过时了。

    现在您可以使用 /opt/elasticbeanstalk/support/envvars 文件,该文件已经是一个可供获取的 shell 脚本:

    commands:
      01_update_composer:
        command: |
          . /opt/elasticbeanstalk/support/envvars
          /usr/bin/composer.phar self-update
    
    container_commands:
      01_run_composer:
      command: |
        composer.phar install --no-scripts --no-dev  # already has user-specified env variables
    

    更新:

    经过更深入的调查后发现container_commands: 包含您的环境变量,但commands: 没有。

    【讨论】:

      【解决方案5】:

      本博客详细介绍了如何实现这一目标的各种选项。

      https://www.onica.com/blog/how-to-call-and-export-variables-in-elastic-beanstalk/

      【讨论】:

      • 不链接到提供解决方案的任何具体内容。 #垃圾邮件?
      猜你喜欢
      • 2017-05-06
      • 2021-11-25
      • 2017-07-21
      • 2019-12-21
      • 2019-10-05
      • 2017-08-14
      • 2018-03-26
      • 2015-07-20
      • 2017-05-24
      相关资源
      最近更新 更多