# NetworkLoadBalancedTaskImageOptions

- Source: [`aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-ecs-patterns/lib/base/network-load-balanced-service-base.ts#L229)
- Type last changed: v2.163.0 · 22 Oct 2024

## Example

TypeScript:

```ts
import { Certificate } from 'aws-cdk-lib/aws-certificatemanager';

declare const cluster: ecs.Cluster;
const certificate = Certificate.fromCertificateArn(this, 'Cert', 'arn:aws:acm:us-east-1:123456:certificate/abcdefg');
const loadBalancedEcsService = new ecsPatterns.NetworkLoadBalancedEc2Service(this, 'Service', {
  cluster,
  memoryLimitMiB: 1024,
  // The default value of listenerPort is 443 if you pass in listenerCertificate
  // It is configured to port 4443 here
  listenerPort: 4443,
  listenerCertificate: certificate,
  taskImageOptions: {
    image: ecs.ContainerImage.fromRegistry('test'),
    // The default value of containerPort is 443 if you pass in listenerCertificate
    // It is configured to port 8443 here
    containerPort: 8443,
    environment: {
      TEST_ENVIRONMENT_VARIABLE1: "test environment variable 1 value",
      TEST_ENVIRONMENT_VARIABLE2: "test environment variable 2 value",
    },
  },
  desiredCount: 2,
});
```

Python:

```python
from aws_cdk.aws_certificatemanager import Certificate

# cluster: ecs.Cluster

certificate = Certificate.from_certificate_arn(self, "Cert", "arn:aws:acm:us-east-1:123456:certificate/abcdefg")
load_balanced_ecs_service = ecs_patterns.NetworkLoadBalancedEc2Service(self, "Service",
    cluster=cluster,
    memory_limit_mi_b=1024,
    # The default value of listenerPort is 443 if you pass in listenerCertificate
    # It is configured to port 4443 here
    listener_port=4443,
    listener_certificate=certificate,
    task_image_options=ecsPatterns.NetworkLoadBalancedTaskImageOptions(
        image=ecs.ContainerImage.from_registry("test"),
        # The default value of containerPort is 443 if you pass in listenerCertificate
        # It is configured to port 8443 here
        container_port=8443,
        environment={
            "TEST_ENVIRONMENT_VARIABLE1": "test environment variable 1 value",
            "TEST_ENVIRONMENT_VARIABLE2": "test environment variable 2 value"
        }
    ),
    desired_count=2
)
```

Java:

```java
import software.amazon.awscdk.services.certificatemanager.Certificate;

Cluster cluster;

ICertificate certificate = Certificate.fromCertificateArn(this, "Cert", "arn:aws:acm:us-east-1:123456:certificate/abcdefg");
NetworkLoadBalancedEc2Service loadBalancedEcsService = NetworkLoadBalancedEc2Service.Builder.create(this, "Service")
        .cluster(cluster)
        .memoryLimitMiB(1024)
        // The default value of listenerPort is 443 if you pass in listenerCertificate
        // It is configured to port 4443 here
        .listenerPort(4443)
        .listenerCertificate(certificate)
        .taskImageOptions(NetworkLoadBalancedTaskImageOptions.builder()
                .image(ContainerImage.fromRegistry("test"))
                // The default value of containerPort is 443 if you pass in listenerCertificate
                // It is configured to port 8443 here
                .containerPort(8443)
                .environment(Map.of(
                        "TEST_ENVIRONMENT_VARIABLE1", "test environment variable 1 value",
                        "TEST_ENVIRONMENT_VARIABLE2", "test environment variable 2 value"))
                .build())
        .desiredCount(2)
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.CertificateManager;

Cluster cluster;

var certificate = Certificate.FromCertificateArn(this, "Cert", "arn:aws:acm:us-east-1:123456:certificate/abcdefg");
var loadBalancedEcsService = new NetworkLoadBalancedEc2Service(this, "Service", new NetworkLoadBalancedEc2ServiceProps {
    Cluster = cluster,
    MemoryLimitMiB = 1024,
    // The default value of listenerPort is 443 if you pass in listenerCertificate
    // It is configured to port 4443 here
    ListenerPort = 4443,
    ListenerCertificate = certificate,
    TaskImageOptions = new NetworkLoadBalancedTaskImageOptions {
        Image = ContainerImage.FromRegistry("test"),
        // The default value of containerPort is 443 if you pass in listenerCertificate
        // It is configured to port 8443 here
        ContainerPort = 8443,
        Environment = new Dictionary<string, string> {
            { "TEST_ENVIRONMENT_VARIABLE1", "test environment variable 1 value" },
            { "TEST_ENVIRONMENT_VARIABLE2", "test environment variable 2 value" }
        }
    },
    DesiredCount = 2
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"

var cluster Cluster

certificate := awscdk.Certificate_FromCertificateArn(this, jsii.String("Cert"), jsii.String("arn:aws:acm:us-east-1:123456:certificate/abcdefg"))
loadBalancedEcsService := ecsPatterns.NewNetworkLoadBalancedEc2Service(this, jsii.String("Service"), &NetworkLoadBalancedEc2ServiceProps{
	Cluster: Cluster,
	MemoryLimitMiB: jsii.Number(1024),
	// The default value of listenerPort is 443 if you pass in listenerCertificate
	// It is configured to port 4443 here
	ListenerPort: jsii.Number(4443),
	ListenerCertificate: certificate,
	TaskImageOptions: &NetworkLoadBalancedTaskImageOptions{
		Image: ecs.ContainerImage_FromRegistry(jsii.String("test")),
		// The default value of containerPort is 443 if you pass in listenerCertificate
		// It is configured to port 8443 here
		ContainerPort: jsii.Number(8443),
		Environment: map[string]*string{
			"TEST_ENVIRONMENT_VARIABLE1": jsii.String("test environment variable 1 value"),
			"TEST_ENVIRONMENT_VARIABLE2": jsii.String("test environment variable 2 value"),
		},
	},
	DesiredCount: jsii.Number(2),
})
```

## Properties

- `containerName`: string = none — The container name value to be specified in the task definition.

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

  **Default:** 80 or 443 with listenerCertificate provided

  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.

  Port mappings that are automatically assigned in this way do not count toward the 100 reserved ports limit of a container instance.

  For more information, see
  [hostPort](https://docs.aws.amazon.com/AmazonECS/latest/APIReference/API_PortMapping.html#ECS-Type-PortMapping-hostPort).

- `dockerLabels`: { [key: string]: string } — A key/value map of labels to add to the container.

  **Default:** No labels.

- `enableLogging`: boolean = true — Flag to indicate whether to enable logging.

- `environment`: { [key: string]: string } — The environment variables to pass to the container.

  **Default:** No environment variables.

- `executionRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The name of the task execution IAM role that grants the Amazon ECS container agent permission to call AWS APIs on your behalf.

  **Default:** No value

- `family`: string — The name of a family that this task definition is registered to.

  **Default:** Automatically generated name.

  A family groups multiple versions of a task definition.

- `image`: [ContainerImage](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.ContainerImage.html) (required) = none — The image used to start a container.

  Image or taskDefinition must be specified, but not both.

- `logDriver`: [LogDriver](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.LogDriver.html) — The log driver to use.

  **Default:** AwsLogDriver if enableLogging is true

- `secrets`: { [key: string]: [Secret](/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.Secret.html) } — The secret to expose to the container as an environment variable.

  **Default:** No secret environment variables.

- `taskRole`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — The name of the task IAM role that grants containers in the task permission to call AWS APIs on your behalf.

  **Default:** A task role is automatically created for you.
