2018-12-24 17:48:50 -08:00
< ? php
2020-05-19 16:07:59 -07:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-start:[cloudfront.php.updatedistribution.complete]
// snippet-start:[cloudfront.php.updatedistribution.import]
2018-12-24 17:48:50 -08:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2018-12-24 17:48:50 -08:00
use Aws\CloudFront\CloudFrontClient ;
use Aws\Exception\AwsException ;
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.updatedistribution.import]
2024-02-05 10:49:20 -07:00
2020-05-19 16:07:59 -07:00
/* ////////////////////////////////////////////////////////////////////////////
2020-05-22 14:15:19 -07:00
* Purpose: Updates information about an Amazon CloudFront distribution.
2020-05-19 16:07:59 -07:00
*
2020-05-22 14:15:19 -07:00
* Prerequisites: An existing CloudFront distribution.
2020-05-19 16:07:59 -07:00
*
* Inputs:
2020-05-22 14:15:19 -07:00
* - $cloudFrontClient: An initialized CloudFront client.
* - $distributionId: The ID of the distribution to update information about.
* - $distributionConfig: A collection of settings for the distribution.
* This value comes from the companion getDistributionConfig function.
* - $eTag: The ETag header value for the distribution. This value comes from
* the companion getDistributionETag function.
2020-05-19 16:07:59 -07:00
*
2020-05-22 14:15:19 -07:00
* Returns: Information about the updated distribution; otherwise,
2020-05-19 16:07:59 -07:00
* the error message.
* ///////////////////////////////////////////////////////////////////////// */
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-start:[cloudfront.php.updatedistribution.main]
2020-05-22 14:15:19 -07:00
function updateDistribution (
$cloudFrontClient ,
$distributionId ,
$distributionConfig ,
$eTag
2020-05-19 16:07:59 -07:00
) {
2020-05-22 14:15:19 -07:00
try {
$result = $cloudFrontClient -> updateDistribution ([
'DistributionConfig' => $distributionConfig ,
'Id' => $distributionId ,
'IfMatch' => $eTag
]);
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
return 'The distribution with the following effective URI has ' .
'been updated: ' . $result [ '@metadata' ][ 'effectiveUri' ];
} catch ( AwsException $e ) {
return 'Error: ' . $e -> getAwsErrorMessage ();
2020-05-19 16:07:59 -07:00
}
2024-02-05 10:49:20 -07:00
}
2020-05-22 14:15:19 -07:00
function getDistributionConfig ( $cloudFrontClient , $distributionId )
2020-05-19 16:07:59 -07:00
{
2020-05-22 14:15:19 -07:00
try {
$result = $cloudFrontClient -> getDistribution ([
'Id' => $distributionId ,
]);
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
if ( isset ( $result [ 'Distribution' ][ 'DistributionConfig' ])) {
return [
'DistributionConfig' => $result [ 'Distribution' ][ 'DistributionConfig' ],
'effectiveUri' => $result [ '@metadata' ][ 'effectiveUri' ]
];
} else {
return [
'Error' => 'Error: Cannot find distribution configuration details.' ,
'effectiveUri' => $result [ '@metadata' ][ 'effectiveUri' ]
];
}
} catch ( AwsException $e ) {
return [
'Error' => 'Error: ' . $e -> getAwsErrorMessage ()
];
}
2020-05-19 16:07:59 -07:00
}
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
function getDistributionETag ( $cloudFrontClient , $distributionId )
{
try {
$result = $cloudFrontClient -> getDistribution ([
'Id' => $distributionId ,
]);
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
if ( isset ( $result [ 'ETag' ])) {
return [
'ETag' => $result [ 'ETag' ],
'effectiveUri' => $result [ '@metadata' ][ 'effectiveUri' ]
];
} else {
return [
'Error' => 'Error: Cannot find distribution ETag header value.' ,
'effectiveUri' => $result [ '@metadata' ][ 'effectiveUri' ]
];
}
} catch ( AwsException $e ) {
return [
'Error' => 'Error: ' . $e -> getAwsErrorMessage ()
];
2024-02-05 10:49:20 -07:00
}
2020-05-22 14:15:19 -07:00
}
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
function updateADistribution ()
{
// $distributionId = 'E1BTGP2EXAMPLE';
$distributionId = 'E1X3BKQ569KEMH' ;
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
$cloudFrontClient = new CloudFrontClient ([
'profile' => 'default' ,
'version' => '2018-06-18' ,
'region' => 'us-east-1'
2018-12-24 17:48:50 -08:00
]);
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
// To change a distribution, you must first get the distribution's
// ETag header value.
$eTag = getDistributionETag ( $cloudFrontClient , $distributionId );
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
if ( array_key_exists ( 'Error' , $eTag )) {
exit ( $eTag [ 'Error' ]);
}
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
// To change a distribution, you must also first get information about
// the distribution's current configuration. Then you must use that
// information to build a new configuration.
$currentConfig = getDistributionConfig ( $cloudFrontClient , $distributionId );
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
if ( array_key_exists ( 'Error' , $currentConfig )) {
exit ( $currentConfig [ 'Error' ]);
}
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
// To change a distribution's configuration, you can set the
// distribution's related configuration value as part of a change request,
// for example:
// 'Enabled' => true
// Some configuration values are required to be specified as part of a change
// request, even if you don't plan to change their values. For ones you
// don't want to change but are required to be specified, you can just reuse
// their current values, as follows.
$distributionConfig = [
'CallerReference' => $currentConfig [ 'DistributionConfig' ][ " CallerReference " ],
'Comment' => $currentConfig [ 'DistributionConfig' ][ " Comment " ],
'DefaultCacheBehavior' => $currentConfig [ 'DistributionConfig' ][ " DefaultCacheBehavior " ],
'DefaultRootObject' => $currentConfig [ 'DistributionConfig' ][ " DefaultRootObject " ],
'Enabled' => $currentConfig [ 'DistributionConfig' ][ " Enabled " ],
'Origins' => $currentConfig [ 'DistributionConfig' ][ " Origins " ],
'Aliases' => $currentConfig [ 'DistributionConfig' ][ " Aliases " ],
'CustomErrorResponses' => $currentConfig [ 'DistributionConfig' ][ " CustomErrorResponses " ],
'HttpVersion' => $currentConfig [ 'DistributionConfig' ][ " HttpVersion " ],
'CacheBehaviors' => $currentConfig [ 'DistributionConfig' ][ " CacheBehaviors " ],
'Logging' => $currentConfig [ 'DistributionConfig' ][ " Logging " ],
'PriceClass' => $currentConfig [ 'DistributionConfig' ][ " PriceClass " ],
'Restrictions' => $currentConfig [ 'DistributionConfig' ][ " Restrictions " ],
'ViewerCertificate' => $currentConfig [ 'DistributionConfig' ][ " ViewerCertificate " ],
'WebACLId' => $currentConfig [ 'DistributionConfig' ][ " WebACLId " ]
];
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
echo updateDistribution (
$cloudFrontClient ,
$distributionId ,
$distributionConfig ,
$eTag [ 'ETag' ]
);
2018-12-24 17:48:50 -08:00
}
2024-02-05 10:49:20 -07:00
2020-05-22 14:15:19 -07:00
// Uncomment the following line to run this code in an AWS account.
// updateADistribution();
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.updatedistribution.main]
// snippet-end:[cloudfront.php.updatedistribution.complete]
2020-05-19 16:07:59 -07:00
// snippet-sourceauthor:[pccornel (AWS)]