NetworkMode

class aws_cdk.aws_ecs.NetworkMode(*values)

Bases: Enum

The networking mode to use for the containers in the task.

ExampleMetadata:

infused

Example:

# vpc: ec2.Vpc


cluster = ecs.Cluster(self, "Cluster", vpc=vpc)

mi_capacity_provider = ecs.ManagedInstancesCapacityProvider(self, "MICapacityProvider",
    subnets=vpc.private_subnets,
    instance_requirements=ec2.InstanceRequirementsConfig(
        v_cpu_count_min=1,
        memory_min=Size.gibibytes(2)
    )
)

# Optionally configure security group rules using IConnectable interface
mi_capacity_provider.connections.allow_from(ec2.Peer.ipv4(vpc.vpc_cidr_block), ec2.Port.tcp(80))

# Add the capacity provider to the cluster
cluster.add_managed_instances_capacity_provider(mi_capacity_provider)

task_definition = ecs.TaskDefinition(self, "TaskDef",
    memory_mi_b="512",
    cpu="256",
    network_mode=ecs.NetworkMode.AWS_VPC,
    compatibility=ecs.Compatibility.MANAGED_INSTANCES
)

task_definition.add_container("web",
    image=ecs.ContainerImage.from_registry("amazon/amazon-ecs-sample"),
    memory_reservation_mi_b=256
)

ecs.FargateService(self, "FargateService",
    cluster=cluster,
    task_definition=task_definition,
    min_healthy_percent=100,
    capacity_provider_strategies=[ecs.CapacityProviderStrategy(
        capacity_provider=mi_capacity_provider.capacity_provider_name,
        weight=1
    )
    ]
)

Attributes

AWS_VPC

The task is allocated an elastic network interface.

BRIDGE

The task utilizes Docker’s built-in virtual network which runs inside each container instance.

HOST

The task bypasses Docker’s built-in virtual network and maps container ports directly to the EC2 instance’s network interface directly.

In this mode, you can’t run multiple instantiations of the same task on a single container instance when port mappings are used.

NAT

The task utilizes Docker’s built-in virtual network which runs inside each Windows container instance.

NONE

The task’s containers do not have external connectivity and port mappings can’t be specified in the container definition.