AWS SDK for PHP
Developer Guide

Managing Amazon CloudFront Invalidations Using the CloudFront API and the AWS SDK for PHP Version 3

Amazon CloudFront caches copies of static and dynamic files in worldwide edge locations. To remove or update a file on all edge locations, create an invalidation for each file or for a group of files.

Each calendar month, your first 1,000 invalidations are free. To learn more about removing content from a CloudFront edge location, see Invalidating Files.

The following examples show how to:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

For more information about using Amazon CloudFront, see the Amazon CloudFront Developer Guide.

Create a Distribution Invalidation

Create a CloudFront distribution invalidation by specifying the path location for the files you need to remove. This example invalidates all files in the distribution, but you can identify specific files under Items.

To create a CloudFront distribution invalidation, use the CreateInvalidation operation.

Imports

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

Sample Code

// Create a CloudFront Client $client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $id = 'E1A2B3C4D5E'; $callerReference = 'STRING'; try { $result = $client->createInvalidation([ 'DistributionId' => $id, 'InvalidationBatch' => [ 'CallerReference' => $callerReference, 'Paths' => [ 'Items' => ['/*'], 'Quantity' => 1, ], ] ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Get a Distribution Invalidation

To retrieve the status and details about a CloudFront distribution invalidation, use the GetInvalidation operation.

Imports

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $distributionId = 'E1A2B3C4D5E'; $invalidationID = 'I1A2B3C4D5E6F7G'; try { $result = $client->getInvalidation([ 'DistributionId' => $distributionId, 'Id' => $invalidationID, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

List Distribution Invalidations

To list all current CloudFront distribution invalidations, use the ListInvalidations operation.

Imports

require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException;

Sample Code

$client = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-2' ]); $distributionId = 'E1A2B3C4D5E'; try { $result = $client->listInvalidations([ 'DistributionId' => $distributionId, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }