2018-09-14 17:57:46 -07:00
< ? php
2020-04-15 14:25:50 -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.creates3distribution.complete]
// snippet-start:[cloudfront.php.creates3distribution.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.creates3distribution.import]
2024-02-05 10:49:20 -07:00
2020-04-15 14:25:50 -07:00
/* ////////////////////////////////////////////////////////////////////////////
* Purpose: Creates a distribution in Amazon CloudFront.
2018-09-14 17:57:46 -07:00
*
2020-04-15 14:25:50 -07:00
* Inputs:
2020-04-17 16:11:47 -07:00
* - $cloudFrontClient: An initialized AWS SDK for PHP SDK client
* for CloudFront.
2020-04-15 14:25:50 -07:00
* - $distribution: A collection of settings for the distribution to
* be created.
*
* Returns: Information about the distribution that was created;
* otherwise, the error message.
* ///////////////////////////////////////////////////////////////////////// */
2024-02-05 10:49:20 -07:00
2020-04-15 14:25:50 -07:00
// snippet-start:[cloudfront.php.creates3distribution.main]
function createS3Distribution ( $cloudFrontClient , $distribution )
{
try {
$result = $cloudFrontClient -> createDistribution ([
'DistributionConfig' => $distribution
]);
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
$message = '' ;
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
if ( isset ( $result [ 'Distribution' ][ 'Id' ])) {
$message = 'Distribution created with the ID of ' .
$result [ 'Distribution' ][ 'Id' ];
}
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
$message .= ' and an effective URI of ' .
$result [ '@metadata' ][ 'effectiveUri' ] . '.' ;
2024-02-05 10:49:20 -07:00
2020-04-27 16:17:12 -07:00
return $message ;
2020-04-15 14:25:50 -07:00
} catch ( AwsException $e ) {
return 'Error: ' . $e [ 'message' ];
2024-02-05 10:49:20 -07:00
}
2020-04-15 14:25:50 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-15 14:25:50 -07:00
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
],
2018-09-21 18:59:28 -07:00
'Items' => [ 'HEAD' , 'GET' ],
2020-04-15 14:25:50 -07:00
'Quantity' => 2
2018-09-14 17:57:46 -07:00
],
2020-04-15 14:25:50 -07:00
'Compress' => false ,
'DefaultTTL' => 0 ,
'FieldLevelEncryptionId' => '' ,
'ForwardedValues' => [
'Cookies' => [
'Forward' => 'none'
],
'Headers' => [
'Quantity' => 0
],
'QueryString' => false ,
'QueryStringCacheKeys' => [
'Quantity' => 0
2020-04-23 16:08:15 -07:00
]
2018-09-14 17:57:46 -07:00
],
2020-04-15 14:25:50 -07:00
'LambdaFunctionAssociations' => [ 'Quantity' => 0 ],
'MaxTTL' => 0 ,
'MinTTL' => 0 ,
'SmoothStreaming' => false ,
'TargetOriginId' => $originName ,
'TrustedSigners' => [
'Enabled' => false ,
'Quantity' => 0
2018-09-14 17:57:46 -07:00
],
2020-04-15 14:25:50 -07:00
'ViewerProtocolPolicy' => 'allow-all'
];
$enabled = false ;
$origin = [
'Items' => [
[
'DomainName' => $s3BucketURL ,
'Id' => $originName ,
'OriginPath' => '' ,
'CustomHeaders' => [ 'Quantity' => 0 ],
'S3OriginConfig' => [ 'OriginAccessIdentity' => '' ]
]
2018-09-14 17:57:46 -07:00
],
2020-04-15 14:25:50 -07:00
'Quantity' => 1
];
$distribution = [
'CallerReference' => $callerReference ,
'Comment' => $comment ,
'DefaultCacheBehavior' => $defaultCacheBehavior ,
'Enabled' => $enabled ,
'Origins' => $origin
];
2024-02-05 10:49:20 -07:00
2020-04-15 14:25:50 -07:00
$cloudFrontClient = new Aws\CloudFront\CloudFrontClient ([
'profile' => 'default' ,
'version' => '2018-06-18' ,
'region' => 'us-east-1'
2018-09-14 17:57:46 -07:00
]);
2024-02-05 10:49:20 -07:00
2020-04-15 14:25:50 -07:00
echo createS3Distribution ( $cloudFrontClient , $distribution );
2018-10-30 20:53:20 -07:00
}
2024-02-05 10:49:20 -07:00
2020-04-15 14:25:50 -07:00
// Uncomment the following line to run this code in an AWS account.
// createsTheS3Distribution();
2019-01-18 00:02:57 -08:00
// snippet-end:[cloudfront.php.creates3distribution.main]
2020-04-15 14:25:50 -07:00
// snippet-end:[cloudfront.php.creates3distribution.complete]
// snippet-sourceauthor:[pccornel (AWS)]