Symptom
I created an Amazon Elastic Container Service (Amazon ECS) cluster in a private subnet through the Amazon ECS console using the cluster creation wizard. However, my container instances aren't registered to the ECS cluster. How can I resolve this issue?
an unusual behavior I got to see while launching an ECS Cluster in a private subnet through the ECS Console. The instance launched in this cluster was still having a public IP instead of residing in the private subnet.
Cause
If you created a cluster with the Amazon ECS console using the cluster creation wizard in a private subnet, your container instances are launched with a public IP address. As result, the container instances can't register to the ECS cluster, because they can't communicate with the ECS endpoint.
when an ECS Cluster is launched it creates a CloudFormation stack to create a AutoScaling Group and AutoScaling LaunchConfiguration, which by default has the following property set: associatePublicIpAddress. This forces your Instance to have a Public IP associated on launch.
To reconfigure the environment to launch container instances without a public IP address, complete the following steps.
Solutions
There are two ways of doing this —
1. Create an empty cluster using the ECS Console and then manually create Auto Scaling Group and Launch Configuration to attach with the empty cluster.
But it may seem a lengthy approach to some people(like me!) for the cluster creation. No worries, there is a simpler way of doing the same with the use of few commands. Here’s the second alternative-
2. Use the ecs-cli to create the cluster which provides an option —no-associate-public-ip-address
with ecs-cli up
command. If you haven’t used the ecs-cli before then you can get stared in three simple steps-
1. Open the AWS CloudFormation console.
2. In the Stack Name column, select the stack that includes the cluster that you previously created.
3. Choose the Template tab.
4. Copy the template, and then paste it into a code editor.
5. In the AWS::AutoScaling::LaunchConfiguration section of the template, change the AssociatePublicIpAddress property to false.
...
6. On the Configure Instance Details page, complete the following steps:
...
e. Set the Auto-assign Public IP field depending on whether you want your instance to be accessible from the public internet. If your instance should be accessible from the internet, verify that the Auto-assign Public IP field is set to Enable. If not, set this field to Disable.
VPC Endpoints
Do not forget to create VPC endpoints to ECR, S3, ECS, and Cloudwatch for ECS task awslogs log-driver (if you use it).
Avoid NAT if possible
NAT can cost you a lot just having them without using it as AWS charges you per hour plus per GB usage. If having one in each AZ for high-availability, it costs multiple times.