Skip to content

CodeBuild takes too long in 'Waiting for DOWNLOAD_SOURCE"

1

I'm experiencing a consistent 6-minute delay during the 'Waiting for DOWNLOAD_SOURCE' phase in AWS CodeBuild, before the actual source download begins. I have no idea what Codepipeline does during this long wait.

e.g. [Container] 2025/01/13 21:00:25.924087 Running on CodeBuild On-demand [Container] 2025/01/13 21:00:25.924102 Waiting for agent ping [Container] 2025/01/13 21:00:26.025191 Waiting for DOWNLOAD_SOURCE [Container] 2025/01/13 21:06:12.681501 Phase is DOWNLOAD_SOURCE [Container] 2025/01/13 21:06:15.157657 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED

What are common causes for delays in this initialization phase? Are there any recommended configurations/common practices to reduce this warm-up time, or is there a way to pre-warm the build environment since we have scheduled builds?

Any insights on optimizing this initial phase would be greatly appreciated.

asked 2 years ago905 views

2 Answers
0

The delay you're experiencing in the 'Waiting for DOWNLOAD_SOURCE' phase of AWS CodeBuild can be caused by several factors. Here are some common causes and potential solutions:

  1. Resource Availability: CodeBuild uses an on-demand fleet that scales based on incoming customer volume. If capacity is not immediately available, your build may be queued, causing delays. Unfortunately, there's no direct way to reduce this queuing time as it depends on resource availability in the region.

  2. Git Clone Depth: To speed up the DOWNLOAD_SOURCE phase, you can set the Git clone depth to a low number. This causes CodeBuild to download fewer versions of your repository, potentially reducing the download time.

  3. Source Size: If your source code repository is large, it may take longer to download. Consider optimizing your repository size by removing unnecessary files or using .gitignore to exclude large files.

  4. Network Latency: If your source repository is hosted far from the CodeBuild region, it may cause delays. Consider using a CodeBuild project in a region closer to your source repository.

  5. Compute Power: Ensure you're using an appropriately sized compute type for your build. A larger instance type might initialize faster.

To optimize this initial phase:

  1. Use Provisioned Concurrency: This keeps a set of initialized execution environments ready, which can help reduce cold start times.

  2. Optimize Source Download: Set a low Git clone depth and consider using a reference and commit ID when specifying your source version. This allows CodeBuild to download only the specified branch, making the DOWNLOAD_SOURCE phase faster.

  3. Use Caching: Implement caching strategies in CodeBuild to speed up subsequent builds by reusing dependencies and artifacts from previous builds.

  4. Monitor and Analyze: Use AWS CloudWatch to monitor your build metrics and analyze where the most time is being spent. This can help you identify specific areas for optimization.

  5. Consider Region: If possible, run your CodeBuild projects in a region close to your source repository to minimize network latency.

Remember, while these strategies can help optimize the build process, some delay in the initialization phase is normal and may not be entirely eliminated.

Sources
Codebuild service usage optimization | AWS re:Post
Build phase - Development and Test on Amazon Web Services
Source version sample with AWS CodeBuild - AWS CodeBuild

answered 2 years ago

  • it's not DOWNLOAD_SOURCE taking the time but the waiting before it downloading usually only takes several seconds according to the log [Container] 2025/01/13 21:06:12.681501 Phase is DOWNLOAD_SOURCE [Container] 2025/01/13 21:06:15.157657 Phase complete: DOWNLOAD_SOURCE State: SUCCEEDED

0

Greeting

Hi Tia,
Thanks for sharing the details and clarifying your experience! I see you’re dealing with a consistent six-minute delay during the Waiting for DOWNLOAD_SOURCE phase in AWS CodeBuild. Since you’ve noted that the actual download is quick, the issue clearly lies in the setup process that happens before downloading your source. Let’s dig into what’s causing this and how we can optimize it for a smoother pipeline experience! 😊


Clarifying the Issue

The logs you’ve provided point to delays happening during the Waiting for DOWNLOAD_SOURCE phase. This part of the process is when CodeBuild sets up your environment before fetching your source code. Tasks in this stage can include provisioning resources, initializing a build container, validating the agent, retrieving secrets, and preparing configurations.

A consistent six-minute delay indicates a bottleneck in one or more of these pre-build tasks, rather than a one-off resource availability issue. Since your builds are scheduled, optimizing this phase can save significant time in your CI/CD workflows and improve efficiency.


Why This Matters

Lengthy delays in pre-build initialization slow down the entire pipeline, especially in high-frequency or scheduled builds. Addressing these bottlenecks helps reduce wasteful idle time and ensures faster feedback loops for developers. Optimizing this phase also lowers the total build cost by minimizing unnecessary runtime in CodeBuild.


Key Terms

  • Pre-Build Initialization: Tasks that prepare the build environment, such as provisioning resources, setting up containers, and retrieving configurations.
  • Fleet Capacity: The availability of compute resources in CodeBuild’s on-demand pool.
  • Secrets Retrieval: Fetching sensitive data (e.g., credentials) from AWS services like Secrets Manager.
  • CloudWatch Logs Insights: A tool for querying and analyzing logs to diagnose delays.

The Solution (Our Recipe)

Steps at a Glance:

  1. Diagnose Delays Using CloudWatch Logs Insights.
  2. Switch to a smaller compute type (e.g., BUILD_GENERAL1_SMALL) for higher fleet availability.
  3. Schedule builds during off-peak hours using CloudWatch Events.
  4. Use pre-warmed environments (e.g., Docker, ECR, and EC2) to eliminate cold starts.
  5. Streamline configurations like secrets retrieval, IAM roles, and container setup.

Step-by-Step Guide:

  1. Diagnose Delays Using CloudWatch Logs Insights
    Use CloudWatch Logs to pinpoint where time is spent during the pre-build phase. A simple query can filter for Waiting events to locate delays:
    fields @timestamp, @message
    | filter @message like /Waiting for/
    | sort @timestamp desc
    Analyze entries such as Waiting for agent ping or Provisioning resources to identify the root cause.

  1. Switch to Smaller Compute Types
    Smaller instance types, such as BUILD_GENERAL1_SMALL, tend to have higher availability in the CodeBuild fleet, reducing queuing time. Update your project environment in the AWS Management Console or via YAML configuration:
    environment:
      computeType: BUILD_GENERAL1_SMALL

  1. Schedule builds during off-peak hours
    Avoid scheduling builds during peak hours by leveraging CloudWatch Events to trigger pipelines at times of low resource demand. Example event rule:
    {
        "source": "aws.events",
        "detail-type": "Scheduled Event",
        "time": "2025-01-14T02:00:00Z",
        "resources": ["arn:aws:events:region:account_id:rule/rule_name"]
    }

  1. Pre-Warm Environments
    AWS CodeBuild doesn’t natively support pre-warmed environments, but you can create a similar setup using pre-configured EC2 instances and Docker containers. This approach avoids the cold start delays in CodeBuild’s environment initialization.

    Example Workflow:

    • Step 1: Pre-create a Docker Container with Dependencies
      Build a Docker container that includes all the tools, dependencies, and configurations your builds require.

      Example Dockerfile for a Python-based build environment:

      FROM amazonlinux:latest
      RUN yum install -y git python3 pip && \
          pip install awscli boto3
      WORKDIR /build
      COPY . .
      ENTRYPOINT ["/bin/bash"]

      Build the Docker image:

      docker build -t my-build-env:latest .
    • Step 2: Store the Container in Amazon ECR
      Push your pre-built Docker image to Amazon Elastic Container Registry (ECR), enabling easy access and reusability:

      docker tag my-build-env:latest <account_id>.dkr.ecr.<region>.amazonaws.com/my-build-env:latest
      aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
      docker push <account_id>.dkr.ecr.<region>.amazonaws.com/my-build-env:latest
    • Step 3: Deploy the Container in a Pre-Warmed Environment
      Use an EC2 instance as a pre-warmed environment to run your builds. Pull the Docker image from ECR on instance startup:

      docker pull <account_id>.dkr.ecr.<region>.amazonaws.com/my-build-env:latest
      docker run -d --name build-container <account_id>.dkr.ecr.<region>.amazonaws.com/my-build-env:latest
    • Step 4: Trigger Builds Using Automation
      Automate build triggers on the EC2 instance using CodePipeline custom actions or AWS Systems Manager (SSM) Run Command. Example SSM command:

      aws ssm send-command \
          --document-name "AWS-RunShellScript" \
          --targets "Key=instanceIds,Values=<ec2_instance_id>" \
          --parameters 'commands=["docker exec build-container ./build-script.sh"]'
    • Step 5: Monitor and Scale
      Use CloudWatch and Auto Scaling to monitor and dynamically adjust resources, ensuring that your pre-warmed environment remains cost-effective and responsive to demand.


  1. Streamline configurations like secrets retrieval, IAM roles, and container setup
    • Minimize secrets retrieval time by caching credentials in a secure location or pre-loading them into the container.
    • Use scoped IAM roles with only the necessary permissions to avoid policy evaluation delays.
    • Example IAM policy:
      {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Effect": "Allow",
                  "Action": [
                      "s3:GetObject",
                      "secretsmanager:GetSecretValue"
                  ],
                  "Resource": "arn:aws:secretsmanager:region:account_id:secret/*"
              }
          ]
      }

Closing Thoughts

Tia, optimizing the pre-build initialization phase can significantly reduce delays and improve overall pipeline performance. While CodeBuild initialization inherently takes some time, focusing on fleet capacity, scheduling, pre-warmed environments, and configuration efficiency can bring this down to a minimal level.

For more detailed guidance, check out these AWS resources:


Farewell

I hope this refined approach helps resolve the delays and makes your builds run faster! Let me know if there’s anything else I can assist with. Happy building! 🚀😊


Cheers,

Aaron 😊

answered 2 years 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.