Skip to content

Instantly share code, notes, and snippets.

@joshbeard
Created April 27, 2022 15:22
Show Gist options
  • Save joshbeard/5de66179be2c3d6b2ab49ceafb390844 to your computer and use it in GitHub Desktop.
Save joshbeard/5de66179be2c3d6b2ab49ceafb390844 to your computer and use it in GitHub Desktop.
GitLab CloudFront Cache Invalidation example
# ------------------------------------------------------------------------------
# == CloudFront Cache Invalidation
# ------------------------------------------------------------------------------
.cf_invalidate:
image:
name: amazon/aws-cli:latest
entrypoint: [""]
stage: CloudFront
when: manual
tags:
- prod
script:
# Get the CloudFront distribution ID based on the host name (CLOUDFRONT_HOST)
- "cfid=$(aws cloudfront list-distributions --query \"DistributionList.Items[].{id: Id,cf_domain: DomainName, aliases: Aliases.Items[0]}[?contains(aliases, '${CLOUDFRONT_HOST}')] | [0].id\" --output text)"
# Invalidate the cache
- aws cloudfront create-invalidation --distribution-id "$cfid" --paths "/*"
only:
variables:
- $CI_COMMIT_REF_PROTECTED == "true"
refs:
- release
cache_invalidate:beta:
extends: .cf_invalidate
variables:
CLOUDFRONT_HOST: beta.foo.com
cache_invalidate:prod:
extends: .cf_invalidate
variables:
CLOUDFRONT_HOST: foo.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment