无法在ApplicationLoadBalancedFargateService中为容器设置环境变量

时间:2020-02-06 20:11:55

标签: python-3.x aws-cdk

(CDK 1.18.0和Python 3.6)

task_role = iam.Role(
    self,
    id=f"...",
    assumed_by=iam.ServicePrincipal("ecs-tasks.amazonaws.com"),
    managed_policies=[...]
)

repo = get_repo(self)

task_def = ecs.FargateTaskDefinition(
    self,
    "...",
    memory_limit_mib=30720,
    cpu=4096,
    task_role=task_role,
    execution_role=self.ecs_execution_role,
)

cont = task_def.add_container(
    "...",
    image=ecs.ContainerImage.from_ecr_repository(repo),
    logging=ecs.LogDrivers.aws_logs(stream_prefix=f"Logging"),
    command=["bash", "start.sh"],
    environment={"NAME1": 'VALUE1', "NAME2": 'VALUE2'} # what would I have to put here?
)

cont.add_port_mappings(ecs.PortMapping(container_port=8080))
fg = ecsp.ApplicationLoadBalancedFargateService(
    self,
    "...",
    task_definition=task_def,
    assign_public_ip=True,
)

我想将NAME1 = VALUE1和NAME2 = VALUE2传递给容器。

我尝试了各种方式来表达环境变量。但是没有一个有效。我在这里做错了什么吗?

除此特定问题外,服务还可以部署和运行。

1 个答案:

答案 0 :(得分:2)

您遵循的方法似乎适用于最新版本(1.23.0)。但是我在发行说明中找不到任何提示,为什么可能会更改。您可以更新到最新版本吗?

        task_def.add_container("container", environment={"a": "b", "c": "d"},  image=aws_ecs.ContainerImage.from_registry(name="TestImage"), memory_limit_mib=512)
  newtask1C300F30:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Environment:
            - Name: a
              Value: b
            - Name: c
              Value: d
          Essential: true
          Image: TestImage
          Memory: 512
          Name: container