Skip to content

ECS Fargate tasks unable to pull ECR images - VPC endpoint private DNS not resolving correctly

0

I have a VPC using 10.10.0.0/16 which has 3 private subnets with an ECS cluster using fargate instances. Calls to ECS will be internet -> rest api -> vpc link 2 -> alb -> ECS. My problem is that when I deploy a service to ECS it fails with the following error

CannotPullContainerError: The task cannot pull 077513866981.dkr.ecr.eu-west-1.amazonaws.com/simple-service:9edc5ac8 from the registry. There is a connection issue between the task and the registry. Check your task network configuration. : failed to copy: httpReadSeeker: failed open: failed to do request: Get 077513866981.dkr.ecr.eu-west-1.amazonaws.com/simple-service:9edc5ac8: dial tcp 52.92.4.2:443: i/o timeout.

It seems that ECS is trying to pull the image from a public IP but I don't have a NAT or IGW, I want it to use my VPC endpoints. I don't know why the DNS is resolving to public IPs.

I have these VPC endpoints/gateway

com.amazonaws.eu-west-1.s3
com.amazonaws.eu-west-1.ecr.dkr
com.amazonaws.eu-west-1.ecr.api
com.amazonaws.eu-west-1.logs

The VPC endpoints have a permissive policy which allows * for Principal, Action & Resource.

My ecr endpoints have a security group which allows tcp inbound on 443 from 10.10.0.0/16.

My routetable includes the S3 gateway endpoint.

Fargate platform version: LATEST.

The VPC has DNS resolution and DNS hostnames enabled.

DHCP option set is using domain-name: eu-west-1.compute.internal, domain-name-servers: AmazonProvidedDNS.

I can see on my VPC endpoints that they have private dns names (.dkr.ecr.eu-west-1.amazonaws.com,.dkr-ecr.eu-west-1.on.aws),(api.ecr.eu-west-1.amazonaws.com,ecr.eu-west-1.api.aws), Network ACLs are default (allow all)

3 Answers
1
Accepted Answer

Heres your problem. Even though you have allowed access to Private endpoints with 10.10.0.0/16, it still relies on S3 which are Public IPs. S3 in your VPC is using a Prefix routing table.

ECS will still need to connect to a PUBLIC IP but the Route table will route it via the S3 gateway.

There for your Outbound security group on ECS

"IpRanges": [ { "Description": "HTTPS to VPC (for VPC endpoints)", "CidrIp": "10.10.0.0/16" } ],

Needs to ALSO allow outbound access also to the S3 prefix List pl-6da54004 on port 443 or 0.0.0.0/0 depending on how locked down you want this to be

EXPERT

answered 10 months ago

EXPERT

reviewed 10 months ago

  • I added an egress on the security group used by ECS on port 443 to pl-6da54004 as you said and ran a deploy of my simple-service task to ECS. There was no error and the tasks went to a running state. Thank you so much for your help!

1

Based on your error message and configuration details, it appears that your ECS Fargate tasks are unable to pull images from ECR because the private DNS for your VPC endpoints isn't resolving correctly.

For the ECR VPC endpoints to work properly with Fargate tasks, you need to ensure:

  1. Private DNS is enabled for your ECR VPC endpoints. This is critical because it allows the standard ECR domain names to resolve to your VPC endpoint's private IP addresses instead of the public ones. I see you've mentioned that private DNS names are configured, but double-check that the "Enable Private DNS Name" option is actually set to "Yes" for both the ECR API and DKR endpoints.

  2. S3 Gateway endpoint configuration: The error suggests that your task is trying to reach a public IP address (52.92.4.2) instead of using your VPC endpoints. While you mentioned that your route table includes the S3 gateway endpoint, make sure that the route table associated with your private subnets where the Fargate tasks are running has the S3 gateway endpoint properly configured. ECR uses S3 to store image layers, so this endpoint is essential.

  3. Security group configuration: While you've configured your ECR endpoints' security group to allow inbound traffic on port 443 from your VPC CIDR, also ensure that the security group associated with your Fargate tasks allows outbound traffic to the VPC endpoints.

  4. Task network configuration: In your ECS task definition or service configuration, ensure that:

  • You're using the awsvpc network mode
  • You've set assignPublicIp to DISABLED for tasks in private subnets
  • The task is using the correct subnets and security groups

The error message specifically shows a timeout when trying to connect to a public IP address, which indicates that the DNS resolution isn't using your VPC endpoints. When private DNS is properly enabled for the ECR endpoints, requests to the ECR domain names should resolve to private IP addresses within your VPC.

For Fargate tasks using platform version 1.4.0 or later, you need both the ECR API and DKR endpoints, as well as the S3 gateway endpoint. If you're using an earlier platform version, you only need the DKR endpoint and S3 gateway endpoint.
Sources
Cannot get ECS to pull Private ECR images when using Fargate | AWS re:Post
Amazon ECR interface VPC endpoints (AWS PrivateLink) - Amazon ECR
Verifying Amazon ECS stopped task connectivity - Amazon Elastic Container Service
Amazon ECS task definition differences for Fargate - Amazon Elastic Container Service

answered 10 months ago

EXPERT

reviewed 10 months ago

0

I don't think any of those 4 will fix the issue

  1. Private DNS is enabled on the VPC endpoints ~ $ aws ec2 describe-vpc-endpoints --region eu-west-1 --filters "Name=vpc-endpoint-type,Values=Interface" --query 'VpcEndpoints[].[VpcEndpointId, ServiceName, PrivateDnsEnabled, State]' --output table

| DescribeVpcEndpoints | +-------------------------+------------------------------------------+-------+------------+

| vpce-058f437457f735fca | com.amazonaws.eu-west-1.logs | True | available |

| vpce-00458645ebfd95f47 | com.amazonaws.eu-west-1.secretsmanager | True | available |

| vpce-0a1a79d27c0534697 | com.amazonaws.eu-west-1.ecr.api | True | available |

| vpce-0031ef8adaa01fdec | com.amazonaws.eu-west-1.ecr.dkr | True | available |

+-------------------------+------------------------------------------+-------+------------+

  1. The s3 gateway is in the route table ~ $ aws ec2 describe-route-tables --region eu-west-1 --filters "Name=route.gateway-id,Values=vpce-082c32858a305d98a" --query 'RouteTables[].{RouteTableId:RouteTableId, Subnets:Associations[].SubnetId, Routes:Routes[?GatewayId==vpce-082c32858a305d98a]}' --output json
[
    {
        "RouteTableId": "rtb-0bd7409a4e4c43b87",
        "Subnets": [
            "subnet-012faacbb45d62b5f",
            "subnet-087d239fa31b1aae3",
            "subnet-0e646065f66b862a9"
        ],
        "Routes": [
            {
                "DestinationPrefixListId": "pl-6da54004",
                "GatewayId": "vpce-082c32858a305d98a",
                "Origin": "CreateRoute",
                "State": "active"
            }
        ]
    }
]
  1. The security group for the fargate tasks has egress to 10.10.0.0/16 on 443 $ aws ec2 describe-security-groups --region eu-west-1 --group-ids sg-088cc307ad709e86b --query 'SecurityGroups[0].IpPermissionsEgress' --output json
[
    {
        "IpProtocol": "tcp",
        "FromPort": 53,
        "ToPort": 53,
        "UserIdGroupPairs": [],
        "IpRanges": [
            {
                "Description": "DNS queries to VPC resolver (TCP)",
                "CidrIp": "10.10.0.0/16"
            }
        ],
        "Ipv6Ranges": [],
        "PrefixListIds": []
    },
    {
        "IpProtocol": "udp",
        "FromPort": 53,
        "ToPort": 53,
        "UserIdGroupPairs": [],
        "IpRanges": [
            {
                "Description": "DNS queries to VPC resolver",
                "CidrIp": "10.10.0.0/16"
            }
        ],
        "Ipv6Ranges": [],
        "PrefixListIds": []
    },
    {
        "IpProtocol": "tcp",
        "FromPort": 443,
        "ToPort": 443,
        "UserIdGroupPairs": [],
        "IpRanges": [
            {
                "Description": "HTTPS to VPC (for VPC endpoints)",
                "CidrIp": "10.10.0.0/16"
            }
        ],
        "Ipv6Ranges": [],
        "PrefixListIds": []
    }
]

from task definition "networkMode": "awsvpc",

~ $ aws ecs describe-services --region eu-west-1 --cluster cluster-dev --services simple-service-dev --query 'services[0].networkConfiguration.awsvpcConfiguration.assignPublicIp' --output text DISABLED

The service is using the same subnets as the routetable above and it is using the security group which has egress for port 443 on tcp and 53 on tcp & udp to 10.10.0.0/16

answered 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.