Skip to content

Developing With Environment Variables

David Killmon edited this page Mar 20, 2020 · 15 revisions

What are Environment Variables?

Environment variables are variables that are available to your app, based on the environment they're running in. Your application can reference them without having to define them. Environment variables are useful for when you want to pass in data to your app that's specific to a particular environment (your test database name versus your production database name, for example).

Accessing environment variables is usually simply based on the language you're using. Here are some examples of getting an environment variable called DATABASE_NAME in a few different languages.

Go

dbName := os.Getenv("DATABASE_NAME")

Javascript

var dbName = process.env.DATABASE_NAME;

Python

database_name = os.getenv('DATABASE_NAME')

What are the Default Environment Variables?

By default, the ECS CLI v2 passes in some default environment variables for your app to use.

  • ECS_CLI_PROJECT_NAME - this is the name of the project this application is running in.
  • ECS_CLI_ENVIRONMENT_NAME - this is the name of the environment the application is running in (test vs prod, for example)
  • ECS_CLI_APP_NAME - this is the name of the current application.
  • ECS_CLI_LB_DNS - this is the DNS name of the Load Balancer (if it exists) such as kudos-Publi-MC2WNHAIOAVS-588300247.us-west-2.elb.amazonaws.com. One note, if you're using a custom domain name, this value will still be the Load Balancer's DNS name.
  • ECS_APP_DISCOVERY_ENDPOINT - this is the endpoint to add after an app name to talk to another app in your environment via service discovery. The value is {project name}.local. For more information about service discovery checkout our service discovery guide.

How do I add my own Environment Variables?

Adding your own environment variable is easy. You can add them directly to your manifest in the variables section. The following snippet will pass a environment variable called LOG_LEVEL to your application, with the value set to debug.

# in ecs-project/{app name}/manifest.yml 
variables:                    
  LOG_LEVEL: debug

You can also pass in a specific value for an environment variable based on the environment. We'll follow the same example as above, by setting the log level, but overwriting the value to be info in our production environment. Changes to your manifest take effect when you deploy them, so changing them locally is safe.

# in ecs-project/{app name}/manifest.yml 
variables:                    
  LOG_LEVEL: debug

environments:
  production:
    variables:
      LOG_LEVEL: info

Here's a quick guide showing you how to add environment variables to your app by editing the manifest 👇

Editing the manifest to add env vars

How do I know the name of my DynamoDB table, S3 bucket, RDS database, etc?

When using the ECS CLI to provision additional AWS resources, such as DynamoDB tables, S3 buckets, Databases, etc, any output values will be passed in as environment variables to your app. For more information, check out the additional resources guide.

Clone this wiki locally