question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[ECS] container enforces having port mappings

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:6
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
upparekhcommented, Jun 15, 2021

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.

3reactions
alexbaileyukcommented, Aug 29, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

PortMapping - Amazon Elastic Container Service
Port mappings allow containers to access ports on the host container instance to send or receive traffic. Port mappings are specified as part...
Read more >
Task definition parameters - Amazon ECS - 亚马逊云科技
Port mappings allow containers to access ports on the host container instance to send or receive traffic. For task definitions that use the...
Read more >
port mapping for AWS ECS - Stack Overflow
All we need is container port. Host port will be randomly assigned and when we scale containers we have no issues. – Balu...
Read more >
Use Port Forwarding to Access Applications in a Cluster
Before you begin You need to have a Kubernetes cluster, and the kubectl ... --template='{{(index (index .spec.containers 0).ports 0).
Read more >
Deploy to Amazon Elastic Container Service - GitLab Docs
Add a port mapping. Set 80 to Host Port and 5000 to Container port. ... Select Create. Make sure that the ECS cluster...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found