{"id":544,"date":"2021-02-08T21:36:51","date_gmt":"2021-02-08T21:36:51","guid":{"rendered":"http:\/\/kevinmichaelcoy.com\/blog\/?p=544"},"modified":"2021-02-08T21:38:55","modified_gmt":"2021-02-08T21:38:55","slug":"devops-invalidating-aws-cloudfront-cache-during-aws-codebuild","status":"publish","type":"post","link":"https:\/\/kevinmichaelcoy.com\/blog\/2021\/02\/08\/devops-invalidating-aws-cloudfront-cache-during-aws-codebuild\/","title":{"rendered":"DevOps: Invalidating AWS CloudFront Cache during AWS CodeBuild"},"content":{"rendered":"\n<p>Let&#8217;s say you are using AWS CloudFront with AWS S3 to host a website. Then, let us assume that your DevOps process updates the website in S3 each time a new change is released. You may experience issues with caching where you do not see your latest changes immediately. One way to mitigate this issue is to invalidate the &#8220;Distribution&#8221; cache for CloudFront. In your <code>buildspec.yml<\/code> you&#8217;ll want a step that looks like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  post_build:\n    commands:\n      # Install new web application code by syncing files (and deleting destination files not in the source)\n      - aws s3 sync dist s3:\/\/${BUCKET_NAME}\/xyz-web\/en\/ --delete\n      # Install jq\n      - apt-get update &amp;&amp; apt-get install -y curl jq\n      # Find the distribution ID for the one with an alias which contains a common token in each environment\n      - export DISTRIBUTION_ID=$(aws cloudfront list-distributions | jq -r '.DistributionList.Items | map(select(.Aliases.Items&#91;]  | contains(\"web-applications\"))) | .&#91;].Id')\n      # Invalidate Cache\n      - echo \"Invalidating CloudFront cache with Distribution ID:\" ${DISTRIBUTION_ID}\n      - aws cloudfront create-invalidation --distribution-id ${DISTRIBUTION_ID} --paths \"\/xyz-web\/en\/*\"<\/code><\/pre>\n\n\n\n<p>In this setup, we are first syncing files from a build in a directory called <code>dist<\/code> to the required S3 bucket (DO NOT use copy if you want to take care of file deletions during builds). Then, we install a popular tool called JQ to make dealing with JSON from AWS APIs easier to deal with. We use this tool to find the &#8220;Distribution ID&#8221; associated with CloudFront that we want to have the cache invalidated for the CDN (you can obviously hardcode this step if you want, but if you have multiple websites this may be a more flexible approach. Lastly, we issue a command to invalidate the cache. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say you are using AWS CloudFront with AWS S3 to host a website. Then, let us assume that your DevOps process updates the website in S3 each time a new change is released. You may experience issues with caching where you do not see your latest changes immediately. One&#8230;<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"https:\/\/kevinmichaelcoy.com\/blog\/2021\/02\/08\/devops-invalidating-aws-cloudfront-cache-during-aws-codebuild\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5,3],"tags":[369,372,373,370,375,374,371],"_links":{"self":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/544"}],"collection":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/comments?post=544"}],"version-history":[{"count":2,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions"}],"predecessor-version":[{"id":547,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/544\/revisions\/547"}],"wp:attachment":[{"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/media?parent=544"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/categories?post=544"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/tags?post=544"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}