mydomain
No ADS
No ADS

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.
Basically, you can manually invalidate CloudFront caching in the AWS Management Console.
No ADS
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.
MyUtils.java
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.cloudfront.CloudFrontClient;

public class MyUtils {

	public static CloudFrontClient createCloudFrontClient(Region region) {
		AwsCredentials credentials = AwsBasicCredentials.create("accessKeyId", "secretAccessKey");
		AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(credentials);

		CloudFrontClient cloudFrontClient = CloudFrontClient.builder() //
				.credentialsProvider(credentialsProvider) //
				.region(region) //
				.build();

		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.

Library

<!-- https://mvnrepository.com/artifact/software.amazon.awssdk/cloudfront -->
<dependency>
    <groupId>software.amazon.awssdk</groupId>
    <artifactId>cloudfront</artifactId>
    <version>2.21.14</version>
</dependency> 

CloudFront Invalidation

On the AWS Management Console you can find the "Distribution ID" that corresponds to your Content Distribution.
No ADS
For example, use Java Awssdk CloudFront to invalidate caching on a CloudFront Distribution.
CloudFrontInvalidationUtils.java
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) //
				.build();

		// 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) //
				.build();

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

		CreateInvalidationResponse response = cloudFrontClient.createInvalidation(createInvalidationRequest);
		System.out.println("Invalidation Result: " + response);
		System.out.println("Invalidation Done!");
	}
}
InvalidationExample1.java
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
		);
	} 
}
No ADS
No ADS