GetInvalidation.php - AWS Code Sample

GetInvalidation.php

GetInvalidation.php demonstrates how to retrieve information about an invalidation issued for 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 an invalidation for an * Amazon CloudFront distribution. * * Prerequisites: An existing Amazon CloudFront distribution and a * corresponding invalidation. * * Inputs: * - $cloudFrontClient: An initialized AWS SDK for PHP SDK client * for CloudFront. * - $distributionId: The distribution's ID. * - $invalidationId: The invalidation ID. * * Returns: Information about the invalidation; otherwise, * the error message. * ///////////////////////////////////////////////////////////////////////// */ function getInvalidation($cloudFrontClient, $distributionId, $invalidationId) { try { $result = $cloudFrontClient->getInvalidation([ 'DistributionId' => $distributionId, 'Id' => $invalidationId, ]); $message = ''; if (isset($result['Invalidation']['Status'])) { $message = 'The status for the invalidation with the ID of ' . $result['Invalidation']['Id'] . ' is ' . $result['Invalidation']['Status']; } if (isset($result['@metadata']['effectiveUri'])) { $message .= ', and the effective URI is ' . $result['@metadata']['effectiveUri'] . '.'; } else { $message = 'Error: Could not get information about ' . 'the invalidation. The invalidation\'s status ' . 'was not available.'; } return $message; } catch (AwsException $e) { return 'Error: ' . $e->getAwsErrorMessage(); } } function getsAnInvalidation() { $distributionId = 'E1BTGP2EXAMPLE'; $invalidationId = 'I1CDEZZEXAMPLE'; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); echo getInvalidation($cloudFrontClient, $distributionId, $invalidationId); } // Uncomment the following line to run this code in an AWS account. // getsAnInvalidation();

Sample Details

Service: cloudfront

Last tested: 2020-04-24

Author: pccornel (AWS)

Type: full-example