Skip to content

How can AWS CodeBuild access Docker images or run yum commands without a NAT Gateway when running inside a VPC?

0

I am running an AWS CodeBuild project inside a VPC, and I do not have a NAT Gateway for cost reasons. This means my CodeBuild ENIs have no outbound internet access, even when placed in a public subnet. I learned that CodeBuild containers never receive public IPs, so the Internet Gateway (IGW) alone is not enough.

Because of the lack of internet connectivity, the following command fails in the install phase of my build:

yum install -y mysql gzip

I now understand that yum always requires outbound internet access, and therefore cannot work inside a NAT-less VPC.

My Goal

I need to run mysqldump inside CodeBuild to dump a MySQL database. Ideally, I want a Docker image that already contains mysql and mysqldump so CodeBuild does not need any internet access and I don't have to run yum install.

My idea was to pull this public image:

public.ecr.aws/codebuild/amazonlinux2-x86_64-standard:3.0

This image already includes the MySQL client tools, so I can run:

command -v mysqldump

without installing anything.

Problem CodeBuild cannot pull public images such as those hosted at:

public.ecr.aws/...

because public ECR requires internet access, and CodeBuild running inside a VPC:

  • does not get a public IP
  • cannot use the IGW
  • cannot reach the internet
  • cannot pull from DockerHub or public ECR

So the image pull fails silently, and CodeBuild falls back to a tiny internal image that does not contain mysqldump, causing the build to fail.

Workaround I am thinking of

Creating a custom Docker image locally that pre-installs MySQL tools:

FROM amazonlinux:2

# Install MySQL client & tools
RUN yum install -y mariadb mysql gzip && yum clean all

# For debugging
RUN mysql --version && mysqldump --version

CMD ["/bin/bash"]

Then

1-Build the image locally

2-Push it to my private Amazon ECR repo

3-Configure CodeBuild to use this private ECR image

4- Add necessary VPC endpoints:

  • com.amazonaws.<region>.ecr.api
  • com.amazonaws.<region>.ecr.dkr

Now CodeBuild can successfully pulls the image without NAT, since private ECR works through VPC endpoints.

Inside the build, I can run:

command -v mysqldump

My Questions

1- Is my understanding correct that CodeBuild cannot pull images from public ECR (public.ecr.aws) when running inside a VPC without NAT Gateway?

2- Is using a custom image in my own private ECR repository the recommended approach for NAT-less VPC environments?

3- Are there any AWS-supported methods to pull public images into CodeBuild without adding a NAT Gateway?

4- Is there a best practice for baking tools (like MySQL client) into a custom CodeBuild image for offline builds?

2 Answers
0

Your understanding of the situation is correct. When AWS CodeBuild runs inside a VPC without a NAT Gateway, it cannot access the public internet, which means it cannot pull images from public repositories like DockerHub or public ECR, nor can it run commands like yum install that require internet access.

To address this limitation, your proposed approach is on the right track:

  1. Yes, CodeBuild cannot pull images from public ECR when running inside a VPC without a NAT Gateway, as it requires internet access.

  2. Using a custom image in your private ECR repository is indeed the recommended approach for NAT-less VPC environments. This allows CodeBuild to access the image through VPC endpoints rather than requiring internet access.

  3. For accessing AWS services without internet access, AWS PrivateLink is the solution. By creating interface VPC endpoints, you can establish private connections between your VPC and supported AWS services. For your specific case, you correctly identified the need for ECR API and ECR DKR endpoints to allow CodeBuild to pull from your private ECR repository.

  4. For best practices when creating custom CodeBuild images:

  • Pre-install all required tools and dependencies in your custom image
  • Keep the image size optimized by cleaning up after installations (as you did with yum clean all)
  • Include version verification commands for debugging (as you did with the MySQL version checks)
  • Consider using multi-stage builds if appropriate to reduce final image size
  • Document the contents and purpose of your custom image

Your solution of creating a custom Docker image with pre-installed MySQL tools, pushing it to a private ECR repository, and configuring the necessary VPC endpoints is the correct approach for running CodeBuild in a VPC without a NAT Gateway.

Remember that when configuring VPC access for CodeBuild, you should only include private subnets. CodeBuild will fail if you provide public subnets because public subnets have a default route to an Internet Gateway.
Sources
Image Builder and AWS PrivateLink interface VPC endpoints - EC2 Image Builder
CodeBuild provisioning custom Amazon VPC support - AWS Proton
Connecting to AWS services from inside your VPC - Amazon Elastic Container Service

answered 9 months ago

EXPERT

reviewed 9 months ago

0

Hello, I'd like to provide additional clarification on your questions.

Question 2: Is using a custom image in my own private ECR repository the recommended approach for NAT-less VPC environments?

Yes, this is the recommended approach. While the original answer mentioned ECR API and ECR DKR endpoints, there's an important additional requirement: ECR image layers are actually stored in S3, so an S3 Gateway endpoint is mandatory. This endpoint is a Gateway type and incurs no additional cost.

Required VPC endpoints are:

  • com.amazonaws.<region>.ecr.api (for ECR API calls)
  • com.amazonaws.<region>.ecr.dkr (for Docker registry API)
  • com.amazonaws.<region>.s3 (for image layer storage, Gateway endpoint, free)

Optionally, you can add com.amazonaws.<region>.logs endpoint for CloudWatch Logs transmission. Additional Considerations: Beyond the original answer's mention of using private subnets only for VPC configuration, security group inbound rules must allow port 443 for VPC endpoint access. Additionally, since CodeBuild creates ENIs across multiple AZs, you need to ensure sufficient IP addresses are available. IAM Permission Setup: The CodeBuild service role requires the following permissions:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ecr:GetAuthorizationToken",
        "ecr:BatchCheckLayerAvailability",
        "ecr:GetDownloadUrlForLayer",
        "ecr:BatchGetImage"
      ],
      "Resource": "*"
    }
  ]
}

Question 3: Are there any AWS-supported methods to pull public images into CodeBuild without adding a NAT Gateway?

There is no direct method, but Pull Through Cache offers an alternative. An important alternative not mentioned in the original answer is using ECR Pull Through Cache rules. This approach automatically syncs public images to your private repository, making them accessible through VPC endpoints.

Creating a Pull Through Cache rule requires internet access only for the first image pull, and subsequent pulls can retrieve cached images from private ECR through VPC endpoints. However, initial setup still requires internet access, so it's not a complete solution, as VPC endpoints currently do not support Amazon ECR Public repositories.

Question 4: Is there a best practice for baking tools (like MySQL client) into a custom CodeBuild image for offline builds?

While the best practices mentioned in the original answer are accurate, here's a specific Dockerfile example with additional recommendations:

Dockerfile Example with Security and Optimization:

FROM amazonlinux:2

# Apply security updates first
RUN yum update -y

# Install MySQL client and required tools
RUN yum install -y mariadb mysql gzip tar && \
    yum clean all && \
    rm -rf /var/cache/yum

# Version verification (for debugging)
RUN mysql --version && mysqldump --version

# Create non-root user (security enhancement)
RUN useradd -m -s /bin/bash builduser
USER builduser

CMD ["/bin/bash"]

Additional Recommendations:

  • Regular vulnerability scanning using ECR image scanning
  • Minimize final image size through multi-stage builds
  • Establish image tagging strategy for clear version management
  • Enhance security by running as non-root user

Implementation Steps

Build and Push Custom Image

* Build image with MySQL tools locally
* Push to private ECR repository

Create VPC Endpoints

* Create ECR API, ECR DKR, and S3 Gateway endpoints
* Allow port 443 inbound in security groups

Configure CodeBuild Project

* Specify private subnets
* Set private ECR image URI
* Grant appropriate IAM permissions

Please let me know if you need clarification on any of these steps or if you have any additional questions.

Best regards, Dayeon Yang

References

answered 9 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.