title: How to have AWS ECS automatically map ports of my container to the host machine(EC2)
body: <h2>Context:</h2>
  <p>I am using Circle CI's <code>aws-ecs/deploy-service-update</code> orb to deploy my docker container by pulling the latest image in AWS ECR and deploy it in AWS ECS with AWS EC2 instance. This container is a Machine Learning model that accepts API requests at TCP port 3000(I am using <code>fastAPI</code> for this) and returns the predictions. After I deployed it I couldn't send requests to the public IP of the container instance of the task that deploys the container at port 3000 (This IP is not my EC2 instance's public IP; it only has private IP and public IP is disable).</p>
  <h2>Debugging</h2>
  <ol>
  <li>I checked my security group and made sure that the port 3000 is open to receive requests from all IPs(0.0.0.0), as part of the inbound rule.</li>
  <li>I stopped the task(which automatically will stop the container running in the EC2 instance) with the thought that something may have gone wrong from Circle CI. Then, according to the service configuration(1 desired task) and task definition of AWS ECS, a new task has started(hence the container) automatically. But, I couldn't send requests to this either.</li>
  <li>I SSHed into my EC2 instance to know if the port 3000 is open. This is when is when I learned that ports weren't mapped at all:
  <a href="https://i.stack.imgur.com/rUAzF.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rUAzF.png" alt="enter image description here" /></a><br />
  As you can see, PORTS column is empty for the container and the container has to accept requests at port 3000 from the command.</li>
  </ol>
  <p>And here are the open ports of the EC2 instance:
  <a href="https://i.stack.imgur.com/cvrtl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cvrtl.png" alt="enter image description here" /></a>
  As you can see, port 3000 is not listed here.</p>
  <hr />
  <p>Here is the task with port mappings which deployed the container (to AWS ECS) that you see <code>docker ps</code> screenshot above:
  <a href="https://i.stack.imgur.com/enTjv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/enTjv.png" alt="enter image description here" /></a><br />
  In the task definition, you can see the port mappings I have defined for the container.</p>
  <hr />
  <p>Here is the task running on my EC2 instance with the task-definition shown above and the network mode I am using is 'awsvpc':
  <a href="https://i.stack.imgur.com/0SpUt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0SpUt.png" alt="enter image description here" /></a></p>
  <hr />
  <p>Here's the &quot;Networking&quot; tab of ENI associated with the task, and also the inbound rule of the security group associated with the EC2 instance that the task is running inside, which accepts requests on port 3000 from all IPs.
  <a href="https://i.stack.imgur.com/5V9Tk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/5V9Tk.png" alt="enter image description here" /></a></p>
  <h2>EDIT 1:</h2>
  <p>After I did</p>
  <pre><code>docker run -p 3000:3000 &lt;my-image:my-tag&gt;
  </code></pre>
  <p>inside the EC2 machine(by SSHing from my laptop), I could send API requests and receive proper response to the container to it's public IP, of the cluster of AWS ECS. This means that ports are being mapped only when I run the container manually.</p>
  <p>I had no problems with ports when I used FARGATE, when I updated the service from Circle CI or even when I manually started tasks.</p>
  <p>So, how to automatically map ports when a task is run from AWS ECS service dashboard or from Circle CI? If I run docker container manually, I will not be able to get logs automatically from AWS Cloudwatch and will not be able to stop it from AWS ECS dashboard. Another container by AWS that is running in EC2 instance will take care of those things. It will route the logs to Cloudwatch and accepts stop the existing one and start commands to start a new container with new image stored in AWS ECR, without having to SSH everytime I would want to look at logs or start/stop containers.</p>
  <p>What has gone wrong here, which led to ports not being mapped and How do I fix it and map ports properly, so i will be able to send API requests to my container.</p>