AWS SDK for PHP
Developer Guide

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

Amazon CloudFront caches content in worldwide edge locations to speed up distribution of static and dynamic files that you store on your own server, or on an Amazon service like Amazon S3 and Amazon EC2. When users request content from your website, CloudFront serves it from the closest edge location, if the file is cached there. Otherwise CloudFront retrieves a copy of the file, serves it, and then caches it for the next request. Caching content at an edge location reduces the latency of similar user requests in that area.

For each CloudFront distribution that you create, you specify where the content is located and how to distribute it when users make requests. This topic focuses on distributions for static and dynamic files such as HTML, CSS, JSON, and image files. F or information about using CloudFront with video on demand, see On-Demand and Live Streaming Video with CloudFront.

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 CloudFront Distribution

Create a distribution from an Amazon S3 bucket. In the following example, optional parameters are commented out, but default values are displayed. To add customizations to your distribution, uncomment both the value and the parameter inside $distribution.

To create a CloudFront distribution, use the CreateDistribution 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' ]); $originName = 'Name to identify the S3 bucket'; $s3BucketURL = '<bucket-name>.s3.amazonaws.com'; $callerReference = 'unique string'; $comment = 'Created by AWS SDK for PHP'; $cacheBehavior = [ 'AllowedMethods' => [ 'CachedMethods' => [ 'Items' => ['HEAD', 'GET'], 'Quantity' => 2, ], 'Items' => ['HEAD', 'GET'], 'Quantity' => 2, ], 'Compress' => false, 'DefaultTTL' => 0, 'FieldLevelEncryptionId' => '', 'ForwardedValues' => [ 'Cookies' => [ 'Forward' => 'none', ], 'Headers' => [ 'Quantity' => 0, ], 'QueryString' => false, 'QueryStringCacheKeys' => [ 'Quantity' => 0, ], ], 'LambdaFunctionAssociations' => ['Quantity' => 0], 'MaxTTL' => 0, 'MinTTL' => 0, 'SmoothStreaming' => false, 'TargetOriginId' => $originName, 'TrustedSigners' => [ 'Enabled' => false, 'Quantity' => 0, ], 'ViewerProtocolPolicy' => 'allow-all', ]; $enabled = false; $origin = [ 'Items' => [ [ 'DomainName' => $s3BucketURL, 'Id' => $originName, 'OriginPath' => '', 'CustomHeaders' => ['Quantity' => 0], 'S3OriginConfig' => ['OriginAccessIdentity' => ''], ], ], 'Quantity' => 1, ]; /* * $cache = [ * 'Quantity' => 0, * ]; * $rootObject = '<string>'; * $alias = [ * 'Quantity' => 0, * ]; * $customError = [ * 'Quantity' => 0, * ]; * $httpVersion = 'http1.1'; * $IPV6 = false; * $logging = [ * 'Bucket' => '', * 'Enabled' => false, * 'IncludeCookies' => false, * 'Prefix' => '', * ]; * $priceClass = 'PriceClass_100'; * $restrictions = [ * 'GeoRestriction' => [ * 'Quantity' => 0, * 'RestrictionType' => 'none', * ], * ]; * $viewerCert = [ * 'CertificateSource' => 'cloudfront', * 'CloudFrontDefaultCertificate' => true, * 'MinimumProtocolVersion' => 'TLSv1', * ]; * $webACLid = ''; */ $distribution = [ 'CallerReference' => $callerReference, 'Comment' => $comment, 'DefaultCacheBehavior' => $cacheBehavior, 'Enabled' => $enabled, 'Origins' => $origin, //'CacheBehaviors' => $cache, //'DefaultRootObject' => $rootObject, //'Aliases' => $alias, //'CustomErrorResponses' => $customError, //'HttpVersion' => $httpVersion, //'IsIPV6Enabled' => $IPV6, //'Logging' => $logging, //'PriceClass' => $priceClass, //'Restrictions' => $restrictions, //'ViewerCertificate' => $viewerCert, //'WebACLId' => $webACLid, ]; try { $result = $client->createDistribution([ 'DistributionConfig' => $distribution, //REQUIRED ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Retrieve a CloudFront Distribution

To retrieve the status and details of a specified CloudFront distribution, use the GetDistribution 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->getDistribution([ 'Id' => $distributionId, //REQUIRED ]); print("<p>The Distribution " . $result['Distribution']['Id'] . " is currently " . $result['Distribution']['Status'] . "</p>"); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

List CloudFront Distributions

Get a list of existing CloudFront distributions in the specified AWS Region from your current account using the ListDistributions 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' ]); try { $result = $client->listDistributions([ ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Update a CloudFront Distribution

Updating a CloudFront distribution is similar to creating a distribution. However, when you update a distribution, more fields are required and all values must be included. To make changes to an existing distribution, we recommend that you first retrieve the existing distribution, and update the values you want to change in the $distribution array.

To update a specified CloudFront distribution, use the UpdateDistribution 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' ]); $id = 'E1A2B3C4D5E'; try { $result = $client->getDistribution([ 'Id' => $id, ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } $currentConfig = $result["Distribution"]["DistributionConfig"]; $ETag = $result["ETag"]; $distribution = [ 'CallerReference' => $currentConfig["CallerReference"], // REQUIRED 'Comment' => $currentConfig["Comment"], // REQUIRED 'DefaultCacheBehavior' => $currentConfig["DefaultCacheBehavior"], // REQUIRED 'DefaultRootObject' => $currentConfig["DefaultRootObject"], 'Enabled' => $currentConfig["Enabled"], // REQUIRED 'Origins' => $currentConfig["Origins"], // REQUIRED 'Aliases' => $currentConfig["Aliases"], 'CustomErrorResponses' => $currentConfig["CustomErrorResponses"], 'HttpVersion' => $currentConfig["HttpVersion"], 'CacheBehaviors' => $currentConfig["CacheBehaviors"], 'Logging' => $currentConfig["Logging"], 'PriceClass' => $currentConfig["PriceClass"], 'Restrictions' => $currentConfig["Restrictions"], 'ViewerCertificate' => $currentConfig["ViewerCertificate"], 'WebACLId' => $currentConfig["WebACLId"], ]; try { $result = $client->updateDistribution([ 'DistributionConfig' => $distribution, 'Id' => $id, 'IfMatch' => $ETag ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Disable a CloudFront Distribution

To deactivate or remove a distribution, change its status from deployed to disabled.

To disable the specified CloudFront distribution, use the DisableDistribution 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' ]); $id = 'E1A2B3C4D5E'; try { $result = $client->getDistribution([ 'Id' => $id, ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } $enabled = false; $currentConfig = $result["Distribution"]["DistributionConfig"]; $ETag = $result["ETag"]; $distribution = [ 'CacheBehaviors' => $currentConfig["CacheBehaviors"], //REQUIRED 'CallerReference' => $currentConfig["CallerReference"], // REQUIRED 'Comment' => $currentConfig["Comment"], // REQUIRED 'DefaultCacheBehavior' => $currentConfig["DefaultCacheBehavior"], // REQUIRED 'DefaultRootObject' => $currentConfig["DefaultRootObject"], 'Enabled' => $enabled, // REQUIRED 'Origins' => $currentConfig["Origins"], // REQUIRED 'Aliases' => $currentConfig["Aliases"], 'CustomErrorResponses' => $currentConfig["CustomErrorResponses"], 'HttpVersion' => $currentConfig["HttpVersion"], 'IsIPV6Enabled' => $currentConfig["IsIPV6Enabled"], 'Logging' => $currentConfig["Logging"], 'PriceClass' => $currentConfig["PriceClass"], 'Restrictions' => $currentConfig["Restrictions"], 'ViewerCertificate' => $currentConfig["ViewerCertificate"], 'WebACLId' => $currentConfig["WebACLId"], ]; //var_dump($distribution); try { $result = $client->updateDistribution([ 'DistributionConfig' => $distribution, 'Id' => $id, 'IfMatch' => $ETag ]); print("<p>For The Distribution " . $result['Distribution']['Id'] . " enabled is set to " . $result['Distribution']['DistributionConfig']['Enabled'] . "</p>"); var_dump($result['Distribution']['DistributionConfig']['Enabled']); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Delete a CloudFront Distribution

Once a distribution is in a disabled status, you can delete the distribution.

To remove a specified CloudFront distribution, use the DeleteDistribution 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' ]); $id = 'E1A2B3C4D5E'; try { $result = $client->getDistribution([ 'Id' => $id, ]); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } $ETag = $result["ETag"]; try { $result = $client->deleteDistribution([ 'Id' => $id, 'IfMatch' => $ETag ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }