Skip to content

How To Improve Codebuild S3 Caching?

0

I'm running a bazel build inside AWS Codebuild. I'm using S3 cache for caching the output artifact.

cache:
  paths:
    - '/tmp/s3-cache/**/*' 

/tmp/s3-cache/ is being used as a disk cache by the bazel process running in the container.

The issue here is that the s3 object uploaded with each Codebuild run is getting a lot bigger with each passing build(40-50 Gb) and subsequent codebuild runs are becoming slower (defeating the purpose of cache) I've inspected the s3 object and it contains multiple app tarballs(artifact) that are produced incrementally along with intermediate outputs. I had to delete this object from S3 to stabilize the build speeds.

Looking for some recommendations/suggestions for effective caching and avoid caching unecessary stuff. Thank you!

2 Answers
0

To improve CodeBuild S3 caching for your Bazel build and avoid caching unnecessary files, consider the following recommendations:

  1. Be more specific with cache paths: Instead of caching the entire '/tmp/s3-cache' directory, try to identify and cache only the essential Bazel-specific directories. For example:
cache:
  paths:
    - '/tmp/s3-cache/bazel-cache/**/*'
    - '/tmp/s3-cache/bazel-repository-cache/**/*'
  1. Use Bazel's built-in caching mechanisms: Bazel has its own caching system. Configure Bazel to use a specific cache directory within your CodeBuild environment, and then cache only that directory in CodeBuild.

  2. Implement cache cleanup: Before uploading the cache to S3, add a step in your buildspec to remove unnecessary files or old artifacts. You can use commands like find or rm to delete files based on age or size.

  3. Use cache prefix: Implement a cache prefix in your CodeBuild project settings. This allows you to organize your cache better and potentially implement a rotation strategy.

  4. Implement cache versioning: Use environment variables or timestamps to version your cache. This way, you can easily invalidate old caches when needed.

  5. Optimize Bazel build: Review your Bazel build configuration to ensure you're not generating unnecessary intermediate outputs or artifacts.

  6. Use local caching: If your builds are running frequently, consider using CodeBuild's local caching instead of S3. Local caching can be faster for intermediate to large build artifacts.

  7. Implement a cache rotation strategy: Periodically clean up old caches in S3 using a separate process or AWS Lambda function.

By implementing these strategies, you should be able to maintain a more efficient and stable cache size, leading to improved build speeds over time. Remember to monitor your cache size and build times regularly to ensure the caching strategy remains effective.
Sources
Cache builds to improve performance - AWS CodeBuild
Create a build project in AWS CodeBuild - AWS CodeBuild
Change build project settings in AWS CodeBuild - AWS CodeBuild

answered a year ago
0

Hello,

The above explanation is correct, and you can use this as a mechanism to ensure faster build times. For any further support on this, please reach out to AWS Premium Support.

Thanks and have a nice day !

AWS
SUPPORT ENGINEER
answered a year 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.