aws-cdk-lib.aws_ecs.PortMapping

interface PortMapping

LanguageType name
.NETAmazon.CDK.AWS.ECS.PortMapping
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awsecs#PortMapping
Javasoftware.amazon.awscdk.services.ecs.PortMapping
Pythonaws_cdk.aws_ecs.PortMapping
TypeScript (source)aws-cdk-lib » aws_ecs » PortMapping

Obtainable from ContainerDefinition.findPortMapping(), ContainerDefinition.findPortMappingByName(), TaskDefinition.findPortMappingByName()

Port mappings allow containers to access ports on the host container instance to send or receive traffic.

Example

declare const taskDefinition: ecs.TaskDefinition;
declare const cluster: ecs.Cluster;

// Add a container to the task definition
const specificContainer = taskDefinition.addContainer('Container', {
  image: ecs.ContainerImage.fromRegistry('/aws/aws-example-app'),
  memoryLimitMiB: 2048,
});

// Add a port mapping
specificContainer.addPortMappings({
  containerPort: 7600,
  protocol: ecs.Protocol.TCP,
});

new ecs.Ec2Service(this, 'Service', {
  cluster,
  taskDefinition,
  cloudMapOptions: {
    // Create SRV records - useful for bridge networking
    dnsRecordType: cloudmap.DnsRecordType.SRV,
    // Targets port TCP port 7600 `specificContainer`
    container: specificContainer,
    containerPort: 7600,
  },
});

Properties

NameTypeDescription
containerPortnumberThe port number on the container that is bound to the user-specified or automatically assigned host port.
appProtocol?AppProtocolThe protocol used by Service Connect.
hostPort?numberThe port number on the container instance to reserve for your container.
name?stringThe name to give the port mapping.
protocol?ProtocolThe protocol used for the port mapping.

containerPort

Type: number

The port number on the container that is bound to the user-specified or automatically assigned host port.

If you are using containers in a task with the awsvpc or host network mode, exposed ports should be specified using containerPort. If you are using containers in a task with the bridge network mode and you specify a container port and not a host port, your container automatically receives a host port in the ephemeral port range.

For more information, see hostPort. Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance.


appProtocol?

Type: AppProtocol (optional, default: no app protocol)

The protocol used by Service Connect.

Valid values are AppProtocol.http, AppProtocol.http2, and AppProtocol.grpc. The protocol determines what telemetry will be shown in the ECS Console for Service Connect services using this port mapping.

This field may only be set when the task definition uses Bridge or Awsvpc network modes.


hostPort?

Type: number (optional)

The port number on the container instance to reserve for your container.

If you are using containers in a task with the awsvpc or host network mode, the hostPort can either be left blank or set to the same value as the containerPort.

If you are using containers in a task with the bridge network mode, you can specify a non-reserved host port for your container port mapping, or you can omit the hostPort (or set it to 0) while specifying a containerPort and your container automatically receives a port in the ephemeral port range for your container instance operating system and Docker version.


name?

Type: string (optional, default: no port mapping name)

The name to give the port mapping.

Name is required in order to use the port mapping with ECS Service Connect. This field may only be set when the task definition uses Bridge or Awsvpc network modes.


protocol?

Type: Protocol (optional, default: TCP)

The protocol used for the port mapping.

Valid values are Protocol.TCP and Protocol.UDP.