Search code examples
dockershellenvironment-variablesamazon-ecsaws-fargate

How do I get environment variables in a shell script running on an ECS FARGATE container?


I have an ECS Fargate instance (node-alpine base) running with some environment variables on it (unique to that instance).

Environment Variables on container

I can access them in a python script I run here:

 // python_script.py
    name1 = os.environ['REACT_APP_TEAM_ONE_NAME']
    name2 = os.environ['REACT_APP_TEAM_TWO_NAME']

But this shell script in the same directory does not find them:

#!/bin/sh

TEAM_ONE_NAME=printenv REACT_APP_TEAM_ONE_NAME
TEAM_TWO_NAME=printenv REACT_APP_TEAM_TWO_NAME

Output from shell script: REACT_APP_TEAM_ONE_NAME: not found

I'm not sure if this is an ECS issue, a shell script issue, or something else as I haven't worked with these technologies very much. Thanks for any help.


Solution

  • To access an environment variable in a shell script, put a dollar sign $ before its name, and generally make sure it's inside a double-quoted string:

    TEAM_ONE_NAME="$REACT_APP_TEAM_ONE_NAME"
    

    Shell variables and environment variables are very closely related and you may find it easier to just directly use the environment variable in your script.

    The syntax you have attempts to run REACT_APP_TEAM_ONE_NAME as a command (that string, not the value of the corresponding environment variable), setting an environment variable TEAM_ONE_NAME to the value printenv (again, a literal string, not the result of a command) only for the scope of that command execution.