- Newest
- Most votes
- Most comments
Based on your userdata, I suspect this is an windows AMI. The security group is incorrect as you opening port 22 (ssh port), however, that's not causing the error. (Also, make sure the AMI is ECS optimised)
For the error, can you check your check your EC2 security group and make sure it had outbound rule 443 to 0.0.0.0/0. It should use the default rule based on your cloudformation but it can also be overwritten / removed by other automation.
To troubleshoot, login to the EC2. Then run your userdata commands and see if those work or fails.
Go to the instance → Connect with RDP.
Open Services → check that AmazonECS is running.
Open PowerShell → run Get-Content C:\ProgramData\Amazon\ECS\log\ecs-agent.log
Also, you have AWSRegion parameter with multiple regions allowed. Make sure, the related parameters like ECR repository is also in the same region. Confirm that the ECR repository exist and contains the image.
The issue you're experiencing with your Windows EC2 instance not registering to your ECS cluster is likely related to the ECS agent configuration or initialization on your Windows instance. Here are several potential causes and solutions:
-
Windows ECS Agent Configuration: The UserData script in your template might not be correctly configuring the ECS agent. For Windows instances, the PowerShell commands need to ensure the ECS agent is properly configured and started.
-
Windows-specific ECS Configuration: Your UserData script is creating the ecs.config file, but you mentioned there's no ProgramData folder in the C drive. This suggests the ECS agent might not be initializing properly on your Windows instance.
-
Network Connectivity: Even though your subnet has access to the internet, there might be security group rules, network ACLs, or routing issues preventing the EC2 instance from communicating with the ECS service endpoints.
-
Windows ECS-Optimized AMI: Ensure you're using the correct Windows ECS-optimized AMI for your region. The AMIs you mentioned (ami-05b458f59b6df9a7a & ami-01fed81ccabd6c52a) should be verified as the latest Windows ECS-optimized AMIs.
-
IAM Role Permissions: While you've attached the AmazonEC2ContainerServiceforEC2Role policy, there might be additional permissions needed for Windows instances.
Recommended actions:
- Modify your UserData script to include more robust ECS agent initialization:
<powershell>
# Create the ECS config directory if it doesn't exist
New-Item -Path "C:\ProgramData\Amazon\ECS" -ItemType Directory -Force
# Set the cluster name
[System.IO.File]::WriteAllText("C:\ProgramData\Amazon\ECS\ecs.config", "ECS_CLUSTER=${ECSCluster}")
# Restart the ECS service
Restart-Service AmazonECS -Force
</powershell>
-
Check the Windows Event Logs on the instance for any ECS agent errors.
-
Verify network connectivity from the instance to the ECS service endpoints.
-
Consider adding a VPC endpoint for ECS if your instances are in a private subnet.
-
If possible, try using a Linux-based ECS instance first to verify your overall cluster configuration is working correctly.
Sources
ECS cluster starts an EC2 instance, but it still says "Container instances 0" | AWS re:Post
EC2 instance does not show up at ECS container instance | AWS re:Post
EC2 scale from auto scaling group not start ecs agent | AWS re:Post
Relevant content
- asked 3 years ago
- asked a year ago
- asked 3 years ago
- asked 3 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 3 months ago

aws ssm get-parameters --names "/aws/service/ami-windows-latest/Windows_Server-2022-English-Full-ECS_Optimized" --region eu-west-2.ProgramDatadirectory (so no logs) nor it has AmazonECS service (tried running it but got not found error) I wonder why? As ECS Optmized AMI it should have ECS agent.Yeah.. that 22 port is a copy paste issue.