Skip to content

Commit

Permalink
fix(codebuild): workspace -or-create + provider cache via S3
Browse files Browse the repository at this point in the history
- tf workspace select -or-create: creates workspace in codebuild backend
  on first run instead of failing with exit 1
- TF_PLUGIN_CACHE_DIR: warm from inf-tfstate-229685449397/tf-provider-cache/
  before tf init, save back in post_build; shaves ~30s off provider install
  on warm builds; || true so a cold cache is not fatal
  • Loading branch information
Your Name committed Mar 17, 2026
1 parent 65108ba commit 11e2322
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion codebuild/buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ version: 0.2
env:
variables:
TF_VERSION: "1.9.8"
# Provider plugin cache -- warm from S3 before tf init, saved back after build
TF_PLUGIN_CACHE_DIR: "/root/.terraform.d/plugin-cache"
PROVIDER_CACHE_S3: "s3://inf-tfstate-229685449397/tf-provider-cache"
PROVIDER_CACHE_S3_REGION: "us-gov-east-1"

phases:

Expand All @@ -35,6 +39,9 @@ phases:
- chmod +x /usr/local/bin/tf
- tf --version
- pip3 install --quiet requests
- echo "Warming provider cache from S3..."
- mkdir -p "$TF_PLUGIN_CACHE_DIR"
- aws s3 sync "${PROVIDER_CACHE_S3}/" "$TF_PLUGIN_CACHE_DIR/" --region "$PROVIDER_CACHE_S3_REGION" --quiet || true

pre_build:
on-failure: ABORT
Expand All @@ -49,7 +56,7 @@ phases:
echo "No workspace-specific backend config found; using backend.tf"
tf init -input=false
fi
- tf workspace select "$TF_WORKSPACE"
- tf workspace select -or-create "$TF_WORKSPACE"
- echo "Active workspace $(tf workspace show)"

build:
Expand Down Expand Up @@ -89,6 +96,8 @@ phases:
post_build:
commands:
- echo "Saving provider cache to S3..."
- aws s3 sync "$TF_PLUGIN_CACHE_DIR/" "${PROVIDER_CACHE_S3}/" --region "$PROVIDER_CACHE_S3_REGION" --quiet || true
- |
echo "Build completed at $(date -u '+%Y-%m-%dT%H:%M:%SZ')"
- |
Expand Down

0 comments on commit 11e2322

Please sign in to comment.