- Newest
- Most votes
- Most comments
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:
-
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.
-
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.
-
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.
-
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.
-
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:
-
Use Provisioned Concurrency: This keeps a set of initialized execution environments ready, which can help reduce cold start times.
-
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.
-
Use Caching: Implement caching strategies in CodeBuild to speed up subsequent builds by reusing dependencies and artifacts from previous builds.
-
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.
-
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
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:
- Diagnose Delays Using CloudWatch Logs Insights.
- Switch to a smaller compute type (e.g.,
BUILD_GENERAL1_SMALL) for higher fleet availability. - Schedule builds during off-peak hours using CloudWatch Events.
- Use pre-warmed environments (e.g., Docker, ECR, and EC2) to eliminate cold starts.
- Streamline configurations like secrets retrieval, IAM roles, and container setup.
Step-by-Step Guide:
- 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 forWaitingevents to locate delays:
Analyze entries such asfields @timestamp, @message | filter @message like /Waiting for/ | sort @timestamp descWaiting for agent pingorProvisioning resourcesto identify the root cause.
- Switch to Smaller Compute Types
Smaller instance types, such asBUILD_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
- 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"] }
-
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
Dockerfilefor 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.
-
- 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:
- AWS CodeBuild Phases Documentation
- CloudWatch Logs Insights Query Reference
- Managing Secrets in AWS CodeBuild
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
Relevant content
asked 4 years ago
asked 4 years ago
asked 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