Skip to content

Commit

Permalink
Merged in issues/GUISE-185 (pull request #150)
Browse files Browse the repository at this point in the history
GUISE-185: Automatically invalidate an existing CloudFront distribution.
  • Loading branch information
garretwilson committed Dec 23, 2022
2 parents e548c43 + e300f51 commit a2afd3d
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions mummy/src/main/java/io/guise/mummy/deploy/aws/CloudFront.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import java.io.IOException;
import java.net.URI;
import java.time.LocalDate;
import java.time.*;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
Expand Down Expand Up @@ -54,6 +54,9 @@

/**
* Sets up a <a href="https://aws.amazon.com/cloudfront/">CloudFront</a> distribution for the site.
* <p>
* If the distribution already exists, an invalidation request will be sent for all the files in the distribution, essentially "refreshing" the distribution.
* </p>
* @implSpec This implementation requires an {@link S3Website} deployment to be specified in the configuration before this deployment. The S3 website bucket and
* aliases (which may or may not have been originally determined from the site domain and aliases) will be used as the certificate domain name and
* alternative names, respectively. If there is an existing certificate indicating the primary S3 website bucket, it will be used. This implementation
Expand All @@ -67,6 +70,12 @@
*/
public class CloudFront implements ContentDeliveryTarget, Clogged {

/**
* The invalidation path to invalidate all the files in a distribution.
* @see <a href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Invalidation.html">Invalidating files</a>
*/
private static final String INVALIDATION_PATH_ALL = "/*";

/**
* The region to use with ACM to work with CloudFront.
* @see <a href="https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cnames-and-https-requirements.html#https-requirements-aws-region">AWS
Expand Down Expand Up @@ -318,7 +327,7 @@ public Optional<URI> deploy(final MummyContext context, final Artifact rootArtif
final String distributionId;
final DomainName distributionDomainName;
if(existingDistributionSummaries.isEmpty()) {
logger.info("Creating distribution for S3 bucket `{}`.", s3Bucket);
logger.info("Creating CloudFront distribution for S3 bucket `{}`.", s3Bucket);
final StringBuilder commentBuilder = new StringBuilder(); //CloudFront comments are limited to a little over 120 characters in length
commentBuilder.append("Created by ").append(context.getMummifierIdentification()); //i18n
commentBuilder.append(" on ").append(LocalDate.now()); //TODO i18n
Expand Down Expand Up @@ -352,6 +361,19 @@ public Optional<URI> deploy(final MummyContext context, final Artifact rootArtif
distributionId = distributionSummary.id();
distributionDomainName = DomainName.of(distributionSummary.domainName());
//TODO ensure that the existing distribution truly has the correct origin, i.e. to the S3 bucket

//Invalidate all the files in the existing distribution, so that any changes will take place immediately
//rather than waiting for catch expiration (up to 24 hours by default). A wildcard invalidation costs
//no more than a single path invalidation; the only drawback seems to be a smaller number of allowed
//concurrent invalidations with wildcards, which probably won't often be of consequence for site deployments.
final String invalidationBatchCallerReference = Instant.now().toString();
try {
final CreateInvalidationResponse response = cloudFrontClient.createInvalidation(request -> request.distributionId(distributionId).invalidationBatch(
batch -> batch.callerReference(invalidationBatchCallerReference).paths(paths -> paths.items(INVALIDATION_PATH_ALL).quantity(1))));
logger.info("Refreshed CloudFront distribution `{}`; invalidation ID `{}`.", distributionId, response.invalidation().id());
} catch(final TooManyInvalidationsInProgressException tooManyInvalidationsInProgressException) {
logger.warn("Unable to invalidate existing distribution `{}`; too many invalidations are alrady in progress.", distributionId);
}
}

//add an alias record to the new distribution if we have a Route 53 DNS
Expand Down

0 comments on commit a2afd3d

Please sign in to comment.