Java Awssdk 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.
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.
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
);
}
}
Amazon Web Services Tutorials
- Introduction to Amazon Web Services (AWS)
- Introduction to Amazon S3
- Introduction to Amazon Cloudfront and its architecture
- How to reduce Amazon Cloudfront Costs?
- Amazon CloudFront Invalidation
- Introduction to DigitalOcean Spaces
- Create DigitalOcean Spaces Bucket
- Introduction to Amazon ACM
- Java Awssdk S3 S3Client Upload object
- Create AWS accessKeyId/secretAccessKey
- Java Awssdk S3 List objects
- Host a static website on Amazon S3
- Java Awssdk CloudFront Invalidation
- DigitalOcean Spaces Create Access Key
- Java Awssdk Common Credentials Providers
- Java Awssdk ProfileCredentialsProvider
- Java Awssdk Creating and using EnvironmentVariableCredentialsProvider
- Java Awssdk Creating and using SystemPropertyCredentialsProvider
- Java Awssdk S3 Upload object with S3TransferManager
- Java Awssdk S3 S3TransferManager download object
- Java Manipulate DigitalOcean Spaces using S3TransferManager
- Java Create, list and delete S3 Bucket
- Aws Console create IAM User
- Create Amazon S3 Bucket
- Configure custom domain for Amazon S3 static website
- Create a CloudFront distribution for S3 Bucket
- Configure Amazon CloudFront Error Pages
- Amazon S3 Bucket policies
- Amazon AWS Policy Generator - policygen
- Migrate DNS service to Amazon Route 53
- Transfer domain registration to Amazon Route 53
- Request an SSL certificate from Amazon ACM
Show More