DEV Community

Akbar Nafisa
Akbar Nafisa

Posted on

Setup Auto Revalidation for CloudFront with Every Client App Build

In CloudFront, we can invalidate the cache on demand by using GitHub Actions. This is necessary to ensure that the latest code updates are reflected on our website. You can view the full code here.

Update Identity and Access Management (IAM)

In our IAM, we need to add a new statement for the CloudFront Distribution.

Image description

{
    "Version": "2012-10-17",
    "Statement": [
        ...
        {
            "Action": [
                "cloudfront:CreateInvalidation"
            ],
            "Effect": "Allow",
            "Resource": [
                "arn:aws:cloudfront::000581027806:distribution/E2KRF40UYXVA3O"
            ]
        }
        ...
    ]
}
Enter fullscreen mode Exit fullscreen mode

You can find the CloudFront Distribution here.

Image description

Update Github Action

Let’s add the CloudFront Distribution to the GitHub Secrets with the AWS_CF_DISTRIBUTION variable name. Next, we will add new steps to perform CloudFront Invalidation in the GitHub Action.

Image description

Also, let’s make some changes in our code to verify if the text will be updated.

Image description

Next, let’s create a new branch and push it to the main branch, then wait for the workflow to finish.

Image description

When you observe the CloudFront, you will notice that a new invalidation has been created. Check our website to confirm the changes.

Image description

Image description

Top comments (0)