Sub topic 8
The S3 backend
This chapter assumes you can run docker buildx build, know what cache-from and cache-to do, and have a docker-container builder. It also assumes you know the basics of S3 buckets and IAM roles. The S3 backend stores BuildKit cache in a bucket you own. It suits teams that already run on AWS or that need more room than the GitHub Actions cache allows.
How the S3 backend stores cache
The s3 cache exporter writes two kinds of objects into one bucket:
- Blobs, which hold the layer data. They are stored under a
blobs/prefix. - Manifests, which list the layers for one cache. They are stored under a
manifests/prefix and named after thenameoption.
On import, BuildKit reads the manifest named by name, then fetches only the blobs it needs. Because blobs are addressed by digest, caches with different names can share the same blob objects in one bucket.
The exporter needs the docker-container driver. The default docker driver does not support it, so create the builder first (see the builder chapter).
The key options
| Option | Meaning |
|---|---|
| `region` | AWS region of the bucket. |
| `bucket` | Bucket name. |
| `name` | Name of the cache manifest. Use one name per app or branch. |
| `mode` | `min` or `max`, as covered earlier. |
| `prefix` | Optional key prefix inside the bucket. |
If you omit credentials, BuildKit uses the standard AWS credential chain from the environment. That includes the variables set by aws-actions/configure-aws-credentials.
A GitHub Actions workflow with OIDC
Long-lived access keys stored as secrets are a risk. A better setup lets the job assume an IAM role through GitHub OIDC (OpenID Connect), so no keys are stored.
name: build-s3-cache
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v4
- uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::123456789012:role/build-cache-writer
aws-region: us-east-1
- uses: docker/setup-buildx-action@v3
- uses: docker/build-push-action@v6
with:
context: .
push: false
tags: myapp:ci
cache-from: type=s3,region=us-east-1,bucket=my-build-cache,name=myapp
cache-to: type=s3,region=us-east-1,bucket=my-build-cache,name=myapp,mode=maxThe id-token: write permission lets the job request an OIDC token. The role's trust policy must allow your repository to assume it. Setup-buildx-action creates a docker-container builder, so the exporter is available.
Least-privilege IAM
The role needs only to read and write objects and list the bucket. Scope it to one bucket.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::my-build-cache/*"
},
{
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-build-cache"
}
]
}Object actions apply to bucket/*, while s3:ListBucket applies to the bucket itself. Mixing these up is a common cause of access errors.
Common errors
- The builder rejects
type=s3. You are probably using the defaultdockerdriver. Switch to a docker-container builder. AccessDeniedon write or read. Check that the policy hasGetObjectandPutObjectonbucket/*andListBucketon the bucket. Also confirm that the role's trust policy matches the repository and branch.- A wrong region. The
regionoption must match the bucket's region. - Cold builds every time. Confirm the
nameis identical incache-fromandcache-to. Then run with--progress=plainand look forCACHEDsteps. - A growing bucket. S3 does not delete old blobs for you. Add a lifecycle rule that expires objects after a set number of days.
Trade-offs
S3 has no practical size cap of the kind the GitHub Actions cache has, and it works from any CI system that can reach AWS. The cost is extra setup (bucket, role, policy) and per-request and storage charges. Transfer is fastest when the runner is in the same region as the bucket.
Exercise
A build using type=s3 fails with AccessDenied on s3:ListBucket, although the policy allows s3:GetObject and s3:PutObject on arn:aws:s3:::my-build-cache/*. What is wrong, and how do you fix it?
Show answer
The ListBucket action applies to the bucket resource, not to objects. Add a statement that allows s3:ListBucket on arn:aws:s3:::my-build-cache (without /*).
Exercise
Why should fork pull requests not run a build with cache-to pointing at your shared S3 bucket?
Show answer
A fork can change the build and write poisoned layers into the cache. A later trusted build that imports that cache could then use those layers. Give untrusted builds read-only access, or no access.