Search code examples
databaseterraformenvironmentamazon-ecscircleci

How to set database config variables for aws ecs in circle ci config.yml?


How are you.

I am deploying go backend api to ecs using docker.

And I am using circle ci for it.

I need to set database config environment variables to run backend api, but I don't know how to set that info in circle ci.

I am initializing aws resource using terraform, do I need to set db config environment variables in terraform? or can I set it on circle ci config.yml?

Thanks


Solution

  • You can define environment variables in the task definition so this will be available for your docker container in ECS.

    resource "aws_ecs_task_definition" "backend-app" {
      family = "backend"
    
      container_definitions = <<EOF
    [
      {
        "portMappings": [
          {
            "hostPort": 80,
            "protocol": "tcp",
            "containerPort": 3000
          }
        ],
        "environment": 
            [
                {
                    "name": "NODE_ENV",
                    "value":"production"
                },
                {
                     "name": "DB_HOST",
                     "value": "HOST_ADDRESS"
                },
                {
                    "name": "DB_PASS",
                    "value": "DB_PASSWORD"
                }
            ],
        "cpu": 1000,
        "memory": 1000,
        "image": "***.dkr.ecr.us-west-2.amazonaws.com/backend:latest",
        "name": "backend",
    
      }
    ]
    EOF
    }