[ECS] container enforces having port mappings
Describe the bug Should be able to create ECS container with no port mappings. This error occurs when trying to do so:
Error: Container test hasn't defined any ports. Call addPortMappings().
To Reproduce Create an ecs cluster with a Ec2TaskDefinition that has a container with no port mappings specified.
Expected behavior Should be able to have a container with no port mappings.
Version:
OS: Win10 Programming Language: Typescript CDK Version: 0.28.0
#2264
There are cases a container is behind a reverse proxy so it doesn't need any port mapping.
Hi @kingdun3284, can you also share the task definition code you're trying to run?
Hi @kingdun3284, when trying to reproduce the error it seems to be thrown by a call to one of the getters containerPort. This sort of validation is done only for load balanced services, in which case it would be expected to provide a port mapping. Could you please share the CDK code snippet that you’re using and elaborate on your specific use case with a reverse proxy?
Also wanted to know what network mode you’re using. Did you mean to use none?
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
For me it happens when I try to attach a FireLens to Fargate TD:
const taskDefinition = new ecs.FargateTaskDefinition(this, 'REDUCTED', {
taskRole: iam.Role.fromRoleArn(this, 'REDUCTED', 'arn:aws:iam::REDUCTED'),
memoryLimitMiB: 8192,
cpu: 4096,
})
taskDefinition.addFirelensLogRouter('coralogixLogRouter', {
firelensConfig: {
type: FirelensLogRouterType.FLUENTD
},
image: ecs.ContainerImage.fromRegistry('coralogixrepo/fluentd-coralogix-ecs:latest'),
})
synth results in the following error:
Container coralogixLogRouter hasn't defined any ports. Call addPortMappings().
Hi @arliber , thanks for providing your task definition snippet. I tried reproducing the error with a task definition with Firelens and application containers with no mapped ports but cdk synth did not throw any errors. Please refer to the task definition I used below:
const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
taskRole: iam.Role.fromRoleArn(this, 'newRole', 'arn:aws:iam::REDUCTED),
memoryLimitMiB: 8192,
cpu: 4096,
});
taskDefinition.addFirelensLogRouter('firelensLogRouter', {
firelensConfig: {
type: ecs.FirelensLogRouterType.FLUENTD,
},
image: ecs.ContainerImage.fromRegistry('amazon/aws-for-fluent-bit:latest'),
logging: new ecs.AwsLogDriver({ streamPrefix: 'firelens' }),
memoryReservationMiB: 50,
});
const container = taskDefinition.addContainer('test', {
image: ecs.ContainerImage.fromRegistry('example-image'),
memoryLimitMiB: 256,
logging: ecs.LogDrivers.firelens({}),
});
Could you share more details about your use case?
I would also like to verify that you’re not calling the containerPort or ingressPort getters in your code or if you’re using a load balanced service. As discussed above, these are the two cases we would expect the error to surface and it is required that you provide a port mapping.
Also can you let us know what version of CDK you are using.
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
same error as @arliber for me with @[email protected]
Hi @sebdec, can you share some details of your use case? Are you also getting this error while trying to attach a Firelens log router to the task definition?
I had previously tried to reproduce this error by attaching a FireLens log router (code snippet here) but it wasn't throwing any errors while synthesizing. Is there something else in your configuration that is not being captured here?
I retried today without any problem. Don't know why, maybe the cdk cli was not correctly updated to the new version. I can now attaching log router without defining any port mappings 👍
Thanks for the update @sebdec. I'll leave the issue to be auto-closed to see if the problem still persists for other users.
Hi, I got a same error. This error occurred when adding a service to the ALB target with the LogRouter defined before the app container.
const cluster = new ecs.Cluster(this, 'cluster')
const task = new ecs.FargateTaskDefinition(this, 'task')
task.addFirelensLogRouter('logRouter', {
image: ecs.ContainerImage.fromRegistry("mazon/aws-for-fluent-bit:stable"),
firelensConfig: {
type: ecs.FirelensLogRouterType.FLUENTBIT
}
})
const container = task.addContainer('app', {
image: ecs.ContainerImage.fromRegistry("amazon/amazon-ecs-sample")
})
container.addPortMappings({
containerPort: 80,
protocol: ecs.Protocol.TCP,
})
const service = new ecs.FargateService(this, "service", {
cluster: cluster,
taskDefinition: task,
});
const alb = new elbv2.ApplicationLoadBalancer(this, 'loadBalancer', { vpc: vpc })
const listner = new elbv2.ApplicationListener(this, 'listener', {
loadBalancer: alb,
port: 80
})
listner.addTargets('target', {
port: 80,
targets: [service]
})
If I defined the container with port mapping first, this error did not occur.
Hi @yanyansk , thanks for providing your task definition. When using the service object as target for the Application Load Balancer (ALB) here:
listner.addTargets('target', {
port: 80,
targets: [service]
})
it uses the first essential container (in the error case it is the Firelens log router) as the target. cdk synth will throw this error as the LB target is expected to have an exposed port while the Firelens log router doesn’t have one. When the app container is added before the log router, it becomes the default essential container and as it has an exposed port, no errors are thrown on doing cdk synth.
You’ll either need to add the container with exposed ports first or you can also add the ALB target like so:
listner.addTargets('target', {
port: 80,
targets: [service.loadBalancerTarget({
containerName: 'app',
containerPort: 80,
})]
})
The loadBalancerTarget() function can be used to create a target if you want to load balance to a container other than the first essential container (so the containers can be added in any order) or the first mapped port on the container.
Please - reopen if you still encounter this issue.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.
For future reference, in case anyone else comes across this issue, it was caused by the order that containers are defined in. If you define a container without ports before a container with ports then you get the error. Switch the order in which you call addContainer so the one with ports open is first and it works fine.
Just spent a couple of hours wrestling with the same issue, except I'm using ApplicationLoadBalancedFargateService L3 along with a custom construct that extends FargateTaskDefinition which configures a number of sidecar containers. I cannot switch the order in which I use addContainer() and the service is a property of the L3 construct, so addTargets() didn't work.
I found that this simple one-liner did the trick:
taskDefinition.defaultContainer = containerDefinition
TypeScript example:
const taskDefinition = new FargateTaskDefinition(this, 'TaskDefinition', {})
containerA = taskDefinition.addContainer('ContainerA', { ... })
containerB = taskDefinition.addContainer('ContainerB', { ... })
taskDefinition.defaultContainer = containerB
Could we re-open this? We just ran into the order issue and it seems like there should be a better solution other than having to manage the order ourselves.
Just hit this as well. Reordering the container definition declaration is not a very intuitive solution. It seems odd to me that the port property of the ApplicationTargetGroup isn't the one that defines traffic-port. In my case, the health check went to the first defined container, and I tried to remove the port mappings to make it clear to CDK that the container doesn't need external traffic, which lead me here.