ListInvalidations.php - AWS Code Sample

ListInvalidations.php

ListInvalidations.php demonstrates how to list invalidation batches in an Amazon CloudFront distribution.

<?php /* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0 */ require 'vendor/autoload.php'; use Aws\CloudFront\CloudFrontClient; use Aws\Exception\AwsException; /* //////////////////////////////////////////////////////////////////////////// * Purpose: Gets information about Amazon CloudFront distribution * invalidations. * * Prerequisites: At least one existing Amazon CloudFront invalidation for the * specified distribution. * * Inputs: * - $cloudFrontClient: An initialized AWS SDK for PHP SDK client * for CloudFront. * - $distributionId: The ID of the distribution to get invalidation * information about. * * Returns: Information about existing distribution invalidations; otherwise, * the error message. * ///////////////////////////////////////////////////////////////////////// */ function listInvalidations($cloudFrontClient, $distributionId) { try { $result = $cloudFrontClient->listInvalidations([ 'DistributionId' => $distributionId ]); return $result; } catch (AwsException $e) { exit('Error: ' . $e->getAwsErrorMessage()); } } function listTheInvalidations() { $distributionId = 'E1WICG1EXAMPLE'; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); $invalidations = listInvalidations($cloudFrontClient, $distributionId); if (isset($invalidations['InvalidationList'])) { if ($invalidations['InvalidationList']['Quantity'] > 0) { foreach ($invalidations['InvalidationList']['Items'] as $invalidation) { echo 'The invalidation with the ID of ' . $invalidation['Id'] . ' has the status of ' . $invalidation['Status'] . '.' . "\n"; } } else { echo 'Could not find any invalidations for the specified distribution.'; } } else { echo 'Error: Could not get invalidation information. Could not get ' . 'information about the specified distribution.'; } } // Uncomment the following line to run this code in an AWS account. // listTheInvalidations();

Sample Details

Service: cloudfront

Last tested: 2020-04-27

Author: pccornel (AWS)

Type: full-example