Java Awssdk CloudFront Invalidation

  1. Library
  2. CloudFront Invalidation
As you know, Amazon CloudFront is a Content Delivery Network (CDN) that provides fast and secure delivery of static and dynamic web content to users around the world. CloudFront has a feature called cache invalidation, which allows you to remove cached content from the CDN so that new content can be delivered to users.
In the article introducing Amazon CloudFront, I explained the architecture and how CloudFront works. It helps you understand why we have to invalidate CloudFront's cache every time the origin server is updated.
  • Introduction to Amazon Cloudfront
Basically, you can manually invalidate CloudFront caching in the AWS Management Console.
  • Amazon CloudFront Invalidation
In this article, I will show you how to use Java Awssdk CloudFront to invalidate CloudFront's caching, this helps your application have higher automation.
First you need to write a utility class to create a CloudFrontClient object.
public class MyUtils { 

	public static CloudFrontClient createCloudFrontClient(Region region) { 
		// Credentials Provider
		AwsCredentialsProvider provider = ...;

		CloudFrontClient cloudFrontClient = CloudFrontClient.builder() //
				.credentialsProvider(provider) //
				.region(region) //

		return cloudFrontClient;
To create a CloudFrontClient object you need to create an AwsCredentialsProvider object, which provides credentials that allow you to interact with AWS. See the article below to create an AwsCredentialsProvider suitable for your purposes.


<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/cloudfront -->

CloudFront Invalidation

On the AWS Management Console you can find the "Distribution ID" that corresponds to your Content Distribution.
For example, use Java Awssdk CloudFront to invalidate caching on a CloudFront Distribution.
package org.o7planning.java_14203_awssdk_cloudfront;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.o7planning.awssdkcloudfront.MyUtils;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudfront.CloudFrontClient;
import software.amazon.awssdk.services.cloudfront.model.CreateInvalidationRequest;
import software.amazon.awssdk.services.cloudfront.model.CreateInvalidationResponse;
import software.amazon.awssdk.services.cloudfront.model.InvalidationBatch;
import software.amazon.awssdk.services.cloudfront.model.Paths;

public class CloudFrontInvalidationUtils {

	// @invalidationPaths: /thing.txt, /foo/bar/*
	public static void sendInvalidation(Region region, //
			String cloudFrontDistributionId, String... invalidationPaths) {
		CloudFrontClient cloudFrontClient = MyUtils.createCloudFrontClient(region);

		Paths paths = Paths.builder() //
				.items(invalidationPaths) //
				.quantity(invalidationPaths.length) //

		// Unique ID like a date: "20201001090000"
		LocalDateTime now = LocalDateTime.now();
		String referenceUniqueId = now.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
		System.out.println("Invalidation referenceUniqueId: " + referenceUniqueId);
		InvalidationBatch invalidationBatch = InvalidationBatch.builder() //
				.paths(paths) //
				.callerReference(referenceUniqueId) //

		CreateInvalidationRequest createInvalidationRequest = CreateInvalidationRequest.builder()
				.distributionId(cloudFrontDistributionId) //
				.invalidationBatch(invalidationBatch) //

		CreateInvalidationResponse response = cloudFrontClient.createInvalidation(createInvalidationRequest);
		System.out.println("Invalidation Result: " + response);
		System.out.println("Invalidation Done!");
package org.o7planning.java_14203_awssdk_cloudfront;

import software.amazon.awssdk.regions.Region;

public class InvalidationExample1 {

	// EU (Frankfurt) - Germany
	private static final Region myCloudFrontRegion = Region.EU_CENTRAL_1;

	private static final String myDistributionId = "Your-Distribution-ID";

	public static void main(String[] args) {
		String[] invalidationPaths = new String[] { //
				"/*" // All Paths
		CloudFrontInvalidationUtils.sendInvalidation( //
				myCloudFrontRegion, // Distribution Regions
				myDistributionId, // Distribution ID
				invalidationPaths // Invalidation Paths