[ECS] [request]: Dynamic containerPort when using Host networking
- Dominant language
- Shell
- Stars
- 5.4k
- Forks
- 334
- PR merge metrics
- No merged PRs in 30d
Description
**Tell us about your request**
The ability to use dynamically assigned HostPort for tasks using Host-networking.
**Which service(s) is this request for?**
ECS / EC2
**Tell us about the problem you're trying to solve. What are you trying to do, and why is it hard?**
For some service which have high throughput and/or many network packets flowing in/out of the container, one can use Host networking to reduce latency on network I/O operations for the container.
A limitations with the current feature set in ECS, is that a single ECS service is bound to one HostPort. One task from the same service can only run on one EC2 instance since two different tasks uses the same HostPost.
I want to be able to run tasks under a service with dynamically assigned HostPort, rather than having to specify one.
The container can read for the Metadata API to figure out what port is it assigned, something like:
```
if [[ ! -z "$ECS_CONTAINER_METADATA_URI" ]]; then
HOST_PORT=$(curl --silent --connect-timeout 5 $ECS_CONTAINER_METADATA_URI | jq '.Ports[].HostPort')
for file in /etc/nginx/conf.d/default.conf; do
sed -i -e '
s/80/'"${HOST_PORT:-80}"'/;
' $file
done
fi
```
**Are you currently working around this issue?**
Yes, using multiple services with the same configuration and task definition (minus the HostPort setting)
Others have made their own version of the ECS agent, forcing all deployments to use host-networking: https://github.com/williamoneill/amazon-ecs-agent/commit/753418d1b247a78a18aa999a45157419fe169e2d https://medium.com/quiq-blog/fix-slow-aws-ecs-network-performance-49988ae13676
**Additional context**
This is somewhat already possible with services running under bridge networking, but then information provided by the Metadata API is not relevant for the task since the EC2 host will NAT the traffic for their
**Attachments**
If you think you might have additional information that you'd like to include via an attachment, please do - we'll take a look. (Remember to remove any personally-identifiable information.)
Contributor guide
Research direction
Start by reviewing the ECS/EC2 host-networking and service port-assignment behavior described in the request, along with the container Metadata API referenced there. Define how a service could receive dynamically assigned HostPorts while preserving task discovery and confirming the behavior with multiple tasks on one EC2 instance. Done means host-networked tasks from one service can use distinct dynamic HostPorts and discover their assigned port.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws
- Domain
- cloud
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100