Skip to content

Instantly share code, notes, and snippets.

@adeydas
Created May 26, 2018 16:27
Show Gist options
  • Select an option

  • Save adeydas/3217afbb31fe132e7cebab83ec6e9863 to your computer and use it in GitHub Desktop.

Select an option

Save adeydas/3217afbb31fe132e7cebab83ec6e9863 to your computer and use it in GitHub Desktop.
@Slf4j
public class Invalidate {
private static final String CLOUD_FRONT_DISTRIBUTION_ID = System.getenv("distribution_id");
private static final AmazonCloudFront AMAZON_CLOUD_FRONT_CLIENT
= AmazonCloudFrontClient.builder().withCredentials(new EnvironmentVariableCredentialsProvider())
.withRegion(Regions.US_WEST_2).build();
public void invalidateListOfItems(final List<String> itemsToInvalidate) {
log.info("Starting invalidation for distribution id {}", CLOUD_FRONT_DISTRIBUTION_ID);
final Paths invalidationPaths
= new Paths().withItems(itemsToInvalidate).withQuantity(itemsToInvalidate.size());
final InvalidationBatch invalidationBatch
= new InvalidationBatch(invalidationPaths, UUID.randomUUID().toString());
final CreateInvalidationRequest invalidationRequest
= new CreateInvalidationRequest(CLOUD_FRONT_DISTRIBUTION_ID, invalidationBatch);
try {
CreateInvalidationResult invalidationResult
= AMAZON_CLOUD_FRONT_CLIENT.createInvalidation(invalidationRequest);
log.info("Create invalidation request with id {} with status {}, created on {}"
, invalidationResult.getInvalidation().getId(), invalidationResult.getInvalidation().getStatus(),
invalidationResult.getInvalidation().getCreateTime().toInstant());
} catch (AmazonCloudFrontException acfe) {
if (acfe.getErrorCode().equals("Throttling")) {
// Being throttled, backoff and retry
sleepSomeAndRetry();
} else {
throw acfe;
}
}
}
@SneakyThrows
private void sleepSomeAndRetry() {
Thread.sleep(1000L);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment