CreateDistributionS3.php - AWS Code Sample

CreateDistributionS3.php

CreateDistributionS3.php demonstrates how to create an Amazon CloudFront distribution for an Amazon S3 Bucket.

<?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: Creates a distribution in Amazon CloudFront. * * Inputs: * - $cloudFrontClient: An initialized AWS SDK for PHP SDK client * for CloudFront. * - $distribution: A collection of settings for the distribution to * be created. * * Returns: Information about the distribution that was created; * otherwise, the error message. * ///////////////////////////////////////////////////////////////////////// */ function createS3Distribution($cloudFrontClient, $distribution) { try { $result = $cloudFrontClient->createDistribution([ 'DistributionConfig' => $distribution ]); $message = ''; if (isset($result['Distribution']['Id'])) { $message = 'Distribution created with the ID of ' . $result['Distribution']['Id']; } $message .= ' and an effective URI of ' . $result['@metadata']['effectiveUri'] . '.'; return $message; } catch (AwsException $e) { return 'Error: ' . $e['message']; } } function createsTheS3Distribution() { $originName = 'my-unique-origin-name'; $s3BucketURL = 'my-bucket-name.s3.amazonaws.com'; $callerReference = 'my-unique-caller-reference'; $comment = 'my-comment-about-this-distribution'; $defaultCacheBehavior = [ '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 ]; $distribution = [ 'CallerReference' => $callerReference, 'Comment' => $comment, 'DefaultCacheBehavior' => $defaultCacheBehavior, 'Enabled' => $enabled, 'Origins' => $origin ]; $cloudFrontClient = new Aws\CloudFront\CloudFrontClient([ 'profile' => 'default', 'version' => '2018-06-18', 'region' => 'us-east-1' ]); echo createS3Distribution($cloudFrontClient, $distribution); } // Uncomment the following line to run this code in an AWS account. // createsTheS3Distribution();

Sample Details

Service: cloudfront

Last tested: 2020-04-13

Author: pccornel (AWS)

Type: full-example