How to remove CDK EFS auto port-mapping in ECS task definition

Viewed 10

I am using CDK to construct EC2 ECS tasks, also I want to mount an EFS volume.

task_definition.add_volume(
        name=volume_name,
        efs_volume_configuration=ecs.EfsVolumeConfiguration(
            file_system_id=file_system.file_system_id,
            transit_encryption='ENABLED',
            authorization_config=ecs.AuthorizationConfig(
                access_point_id=access_point.access_point_id,
                iam="ENABLED"
            )
        )
    )


container_definition.add_mount_points(
        ecs.MountPoint(
            container_path=mount_path,
            source_volume=volume_name,
            read_only=False
        )
    )

CDK adds port mapping for port 2049 for EFS NFS access

  {
      "hostPort": 2049,
      "protocol": "tcp",
      "containerPort": 2049
    },

When I run multiple instances of the same task, this cause a port conflict, so I decided to use dynamic port mapping, I want the port mapping to be like this:

  {
      "hostPort": 0,
      "protocol": "tcp",
      "containerPort": 2049
    },

but, AFAIK there is no way to customize it (please correct me if I am mistaken), I can add new port mapping to the container definition

self.container_definition.add_port_mappings(
        ecs.PortMapping(
            container_port=2049,
            host_port=0,
            protocol=ecs.Protocol.TCP
        )
    )

but this does not solve the issue, as the default one still present, and cause the conflict.

So, the question is, How can I remove the auto-generated (by CDK) port mapping, or disable its generation, or customize it

Thanks

0 Answers
Related