如何通过Elasticbeanstalk配置文件(使用Docker)访问环境变量?

时间:2014-11-04 17:48:29

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

例如,如果我希望挂载由环境变量定义的特定卷。

3 个答案:

答案 0 :(得分:2)

我最终使用了以下代码:

---
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/02my_setup.sh":
    owner: root
    group: root
    mode: "000755"
    content: |
      #!/bin/bash

      set -e

      . /opt/elasticbeanstalk/hooks/common.sh

      EB_CONFIG_APP_CURRENT=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
      EB_SUPPORT_FILES_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k support_files_dir)

      # load env vars
      eval $($EB_SUPPORT_FILES_DIR/generate_env | sed 's/$/;/')

答案 1 :(得分:1)

如果需要从Elastic Beanstalk配置文件中访问环境变量,可以执行以下操作:

commands:

  01-vars:
    command: |
      $(/opt/elasticbeanstalk/containerfiles/support/generate_env | grep PARAM -v | sed 's/^/export/') && # do stuff 

这假设您不需要默认的PARAM环境变量。

答案 2 :(得分:1)

您可以在bash脚本中使用/opt/elasticbeanstalk/bin/get-config environment

示例:

# .ebextensions/efs_mount.config

commands:
01_mount:
    command: "/tmp/mount-efs.sh"

files:
"/tmp/mount-efs.sh":
    mode: "000755"
    content : |
        #!/bin/bash

        EFS_REGION=$(/opt/elasticbeanstalk/bin/get-config environment | jq -r '.EFS_REGION')
        EFS_MOUNT_DIR=$(/opt/elasticbeanstalk/bin/get-config environment | jq -r '.EFS_MOUNT_DIR')
        EFS_VOLUME_ID=$(/opt/elasticbeanstalk/bin/get-config environment | jq -r '.EFS_VOLUME_ID')