briancaffey
briancaffey

Reputation: 2559

How to use the same set of environment variables between multiple ECS service definitions in CloudFormation

I have a Django application that is running in ECS and everything is setup using CloudFormation. I need to use the same set of environment variables in container definitions for several different services/tasks. Currently I am repeating the environment variables for the different containers definitions (Django webserver, multiple celery workers, beat and channels services, and container definitions in tasks for migrations and collectstatic).

Would it be possible for me to define the environment variables in one file, and then reference these environment variables in each container definition as a cross stack reference?

Here is the repo that I am working on that contains the CloudFormation and project code: https://gitlab.com/verbose-equals-true/django-postgres-vue-gitlab-ecs

The services for my CloudFormation stack are defined in this folder: https://gitlab.com/verbose-equals-true/django-postgres-vue-gitlab-ecs/tree/develop/cloudformation/services

Upvotes: 1

Views: 1523

Answers (1)

Adiii
Adiii

Reputation: 59946

The best and more secure option is AWS Systems Manager Parameter Store where you can share common environment variable betweens services and applications.

Parameter Store, part of EC2 Systems Manager, provides a centralized, encrypted store to manage your configuration data, whether plaintext data (such as database strings) or secrets (such as passwords). Parameters can be easily referenced with Systems Manager capabilities, such as Run Command, State Manager, and Automation. In addition, because Parameter Store is available through the AWS CLI, APIs, and SDKs, you can easily reference parameters across AWS services such as AWS Lambda and Amazon ECS.

So you can refer the common variable in your task defintion.

he following is a snippet of a task definition showing the format when referencing an Secrets Manager secret.

{
  "containerDefinitions": [{
    "secrets": [{
      "name": "environment_variable_name",
      "valueFrom": "arn:aws:secretsmanager:region:aws_account_id:secret:secret_name-AbCdEf"
    }]
  }]
}

enter image description here specifying-sensitive-data

Upvotes: 2

Related Questions