How can I set the env variable for an instance for a docker container for an elastic beanstalk?

Our flexible single-container board-boom stack application works with load balancing with multiple instances of ec2.

I want to pass the identifier of the ec2 instance of the machine it is running on as an environment variable to the docker container. (I want to avoid using special AWS objects inside the container).

I suppose I need to put something in the .ebextension configuration file, where I take a curl to get the instance data, and then set it to an environment variable that will be passed to the docker container.

Something like this (which does not work, it does not cause an EB error, but env var is not available inside the container):

container_commands:
  set_instance_id:
    command: export EC2_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`

Ideally, I would like to avoid hacking into EB startup scripts because they are undocumented and seem to change without warning.

+2
source share
1 answer

Unfortunately, at the moment you cannot do what you want without cluttering up your application with the help of special AWS functions or changing the EB script / deployment process.

Options # 1: Check for AWS Metadata from Your Application

You can directly curlAWS metadata ( http://169.254.169.254/latest/meta-data/instance-id ) directly from your Docker container.

Run <curl -s code http://169.254.169.254/latest/meta -data / instance-id </code> inside the Docker container for elastic bean stitch

, EC2_INSTANCE_ID ( , ), AWS, . FYI, 169.254.0.0/16 - - . , AWS .

# 2:

Dockerfile ENV. , ENV Docker. Docker , , , Docker.

Injecting Dockerfile . appdeploy/pre .ebextensions:

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/02injectdockerfile.sh":
    mode: "000755"
    content: |
      . /opt/elasticbeanstalk/hooks/common.sh
      EB_CONFIG_APP_CURRENT=$(/opt/elasticbeanstalk/bin/get-config container -k app_deploy_dir)
      cd $EB_CONFIG_APP_CURRENT
      echo "ENV EC2_INSTANCE_ID \"`curl -s http://169.254.169.254/latest/meta-data/instance-id`\"" >> Dockerfile

pre-appdeploy? container_commands?

container_commands . , . container_commands Dockerfile (docker build). Dockerfile, ENV docker build.

: . appdeploy hook:

[ec2-user@ip-172-31-62-137 ~]$ tree /opt/elasticbeanstalk/hooks/appdeploy/
/opt/elasticbeanstalk/hooks/appdeploy/
β”œβ”€β”€ enact
β”‚   β”œβ”€β”€ 00run.sh
β”‚   └── 01flip.sh
β”œβ”€β”€ post
β”‚   └── 01_monitor_pids.sh
└── pre
    β”œβ”€β”€ 00clean_dir.sh
    β”œβ”€β”€ 01unzip.sh
    β”œβ”€β”€ 02docker_db_check.sh
    └── 03build.sh

pre/01unzip.sh docker build pre/03build.sh. , script, ENV script 01unzip.sh 03build.sh. , . , , , Elastic Beanstalk. , "" , .

, - . : docker run enact/00run.sh. EB script.

+6

Source: https://habr.com/ru/post/1607894/


All Articles