2018-09-14 17:57:46 -07:00
< ? php
2020-04-21 16:46:08 -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.deletedistribution.complete]
// snippet-start:[cloudfront.php.deletedistribution.import]
2018-09-14 17:57:46 -07:00
require 'vendor/autoload.php' ;
2024-02-05 10:49:20 -07:00
2018-09-14 17:57:46 -07:00
use Aws\Exception\AwsException ;
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.deletedistribution.import]
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Deletes an Amazon CloudFront distribution.
*
* Prerequisites: An existing Amazon CloudFront distribution. The
* distribution must be disabled first.
*
* Inputs:
* - $cloudFrontClient: An initialized AWS SDK for PHP SDK client
* for CloudFront.
* - $distributionId: The distribution's ID.
* - $eTag: The ETag header value for the distribution. This value comes from
* the companion getDistributionETag function.
2018-09-14 17:57:46 -07:00
*
2020-04-21 16:46:08 -07:00
* Returns: Information about the deletion request; otherwise,
* the error message.
* ///////////////////////////////////////////////////////////////////////// */
2024-02-05 10:49:20 -07:00
2019-01-18 00:02:57 -08:00
// snippet-start:[cloudfront.php.deletedistribution.main]
2020-04-21 16:46:08 -07:00
function deleteDistribution ( $cloudFrontClient , $distributionId , $eTag )
{
try {
$result = $cloudFrontClient -> deleteDistribution ([
'Id' => $distributionId ,
'IfMatch' => $eTag
]);
return 'The distribution at the following effective URI has ' .
'been deleted: ' . $result [ '@metadata' ][ 'effectiveUri' ];
} catch ( AwsException $e ) {
return 'Error: ' . $e -> getAwsErrorMessage ();
2024-02-05 10:49:20 -07:00
}
2020-04-21 16:46:08 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
function getDistributionETag ( $cloudFrontClient , $distributionId )
{
try {
$result = $cloudFrontClient -> getDistribution ([
'Id' => $distributionId ,
]);
2024-02-05 10:49:20 -07:00
2020-04-23 16:08:15 -07:00
if ( isset ( $result [ 'ETag' ])) {
2020-04-27 16:17:12 -07:00
return [
'ETag' => $result [ 'ETag' ],
'effectiveUri' => $result [ '@metadata' ][ 'effectiveUri' ]
];
2020-04-23 16:08:15 -07:00
} else {
2020-04-27 16:17:12 -07:00
return [
'Error' => 'Error: Cannot find distribution ETag header value.' ,
'effectiveUri' => $result [ '@metadata' ][ 'effectiveUri' ]
];
2020-04-23 16:08:15 -07:00
}
2020-04-21 16:46:08 -07:00
} catch ( AwsException $e ) {
2020-04-27 16:17:12 -07:00
return [
'Error' => 'Error: ' . $e -> getAwsErrorMessage ()
];
2024-02-05 10:49:20 -07:00
}
2020-04-21 16:46:08 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
function deleteADistribution ()
{
$distributionId = 'E17G7YNEXAMPLE' ;
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
$cloudFrontClient = new Aws\CloudFront\CloudFrontClient ([
'profile' => 'default' ,
'version' => '2018-06-18' ,
'region' => 'us-east-1'
2018-09-21 18:59:28 -07:00
]);
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
// To delete 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-04-27 16:17:12 -07:00
if ( array_key_exists ( 'Error' , $eTag )) {
exit ( $eTag [ 'Error' ]);
2020-04-21 16:46:08 -07:00
} else {
2020-04-27 16:17:12 -07:00
echo deleteDistribution (
$cloudFrontClient ,
$distributionId ,
$eTag [ 'ETag' ]
);
2020-04-21 16:46:08 -07:00
}
2018-10-30 20:53:20 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-21 16:46:08 -07:00
// Uncomment the following line to run this code in an AWS account.
// deleteADistribution();
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.deletedistribution.main]
// snippet-end:[cloudfront.php.deletedistribution.complete]
2020-04-21 16:46:08 -07:00
// snippet-sourceauthor:[pccornel (AWS)]