Search code examples
docker-composegoogle-cloud-functionsaws-cloudformationinfrastructure-as-code

Adding environment variable to Cloudformation ECS + Fargate deployment


Our images have environment variables that ought to be defined during docker run, any idea how to add this variables into the cloudformation file. We currently have something like:

Task:
Type: AWS::ECS::TaskDefinition
Properties:
  Family: testenv
  Cpu: 256
  Memory: 512
  NetworkMode: 
  RequiresCompatibilities:
    - FARGATE
  ExecutionRoleArn: !ImportValue ECSTaskExecutionRole
  ContainerDefinitions:
    - Name: bonalds
      Image: gcr.io/zonalds-21/id-me:latest // image comes from gcr
      Cpu: 256
      Memory: 512
      PortMappings:
        - ContainerPort: 4567
          Protocol: tcp
      LogConfiguration:
        LogDriver: 
        Options:
          awslogs-group: 'zonalds'
          awslogs-region: !Ref AWS::Region
          awslogs-stream-prefix: 'routme'

I can't seem to find any info in the AWS documentation, what would be the best way to add the environment variable?


Solution

  • Your container definition can hold environment variables.

    ContainerDefinitions:
        - Name: bonalds
          Image: gcr.io/zonalds-21/id-me:latest // image comes from gcr
          Cpu: 256
          Environment:
            - Name: Test
              Value: 'test'
          Memory: 512
          PortMappings:
            - ContainerPort: 4567
              Protocol: tcp
          LogConfiguration:
            LogDriver: 
            Options:
              awslogs-group: 'zonalds'
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: 'routme'
    

    More information in the doc