2
votes

Invalid 'containerPort' setting for container 'prerenderContainer'. (Service: AmazonECS; Status Code: 400; Error Code: ClientException; Request ID: ac922961-8445-4810-a839-03bb031938a2)

Error above ^ code snippet below....

const taskDefinition = new ecs.Ec2TaskDefinition(this, 'TaskDef',{
  networkMode: "bridge",

prerenderContainer.addPortMappings('ports',{
  containerPort: 3000,
  hostPort: 80,
});

const listener = applb.addListener('appFleet', {
   targetGroupName: 'prerenderFleet-TGroup',
   port: 80
});

const prerenderFleetTGroup = listener.addTargets('ECS', {
port: 80,
targets:[service.loadBalancerTarget({
containerName: 'prerenderContainer',
containerPort: 3000,
})], 

I've tried bridge and awsvpc for the network mode with no luck. I thought this might be because of the specific port range I have chosen so I also tried "40391" but no luck.

Is it a timeout issue?

Any help is appreciated.

1

1 Answers

3
votes

First of all I would recommend leaving the "hostPort" setting out. This allows ECS to choose a random host port dynamically, and then also leave the port our of the "addTargets" call. The random port will automatically be mapped in the target port as well. With the current configuration you can only place a single container per host, as all containers will try to map to port 80 on the host, and there is only one port 80 on the host.

Alternatively you can enable AWSVPC networking mode and then also turn on "ENI trunking" This will give each container on the host its own Elastic Network Interface with its own IP address, and its own port 80.

In terms of this exact error message I'd need to see a little more info on the settings generated from CDK by these calls. Try running "cdk synth" and then grab the JSON or YAML generated for the task definition and that should provide more info about why the "containerPort" setting is invalid.

However, I'm also confident that if you switch to a dynamic port or use AWSVPC mode it will also solve this problem, and you will have a better networking configuration as well