Integrate AWS SDK for PHP
- To manage CloudFront using PHP, you first need the AWS SDK for PHP. Install it via Composer:
composer require aws/aws-sdk-php
Initialize the AWS SDK
- Initialize the SDK with your AWS credentials and region. Create a new client for CloudFront after initialization.
require 'vendor/autoload.php';
use Aws\CloudFront\CloudFrontClient;
$sdk = new Aws\Sdk([
'region' => 'your-region', // e.g., 'us-west-2'
'version' => 'latest',
'credentials' => [
'key' => 'your-access-key-id',
'secret' => 'your-secret-access-key',
]
]);
$cloudFrontClient = $sdk->createCloudFront();
Create a Distribution
- To create a distribution, prepare a distribution configuration and use the
CreateDistribution
method.
$config = [
'CallerReference' => 'unique-string',
'Origins' => [
'Quantity' => 1,
'Items' => [
[
'Id' => 'origin1',
'DomainName' => 'example.com',
'OriginPath' => '',
'CustomHeaders' => [
'Quantity' => 0,
]
],
],
],
'DefaultCacheBehavior' => [
'TargetOriginId' => 'origin1',
'ViewerProtocolPolicy' => 'redirect-to-https',
'AllowedMethods' => [
'Quantity' => 2,
'Items' => ['HEAD', 'GET'],
'CachedMethods' => [
'Quantity' => 2,
'Items' => ['HEAD', 'GET'],
],
],
'ForwardedValues' => [
'QueryString' => false,
'Cookies' => ['Forward' => 'none'],
],
'MinTTL' => 0,
],
'Enabled' => true,
];
$result = $cloudFrontClient->createDistribution([
'DistributionConfig' => $config,
]);
List Distributions
- Retrieve a list of your CloudFront distributions to manage or query their statuses.
$result = $cloudFrontClient->listDistributions();
if ($result['DistributionList']['Quantity'] > 0) {
foreach ($result['DistributionList']['Items'] as $distribution) {
echo $distribution['Id'] . "\n";
}
} else {
echo "No distributions found.\n";
}
Invalidate Cache
- To ensure your CDN delivers updated content, you can invalidate cache files.
$result = $cloudFrontClient->createInvalidation([
'DistributionId' => 'your-distribution-id',
'InvalidationBatch' => [
'Paths' => [
'Quantity' => 1,
'Items' => ['/path-to-invalidate/*'],
],
'CallerReference' => 'unique-invalidation-reference',
],
]);
Update a Distribution
- To modify existing distributions, retrieve the configuration, make changes, and then update it.
// First, get the current configuration
$currentConfig = $cloudFrontClient->getDistributionConfig([
'Id' => 'your-distribution-id'
]);
$config = $currentConfig['DistributionConfig'];
// Make any necessary changes to $config here. For example, enable logging.
$config['Logging'] = [
'Enabled' => true,
'Bucket' => 'my-log-bucket.s3.amazonaws.com',
'Prefix' => 'my-prefix/',
'IncludeCookies' => false,
];
// Update the distribution with the new configuration
$result = $cloudFrontClient->updateDistribution([
'Id' => 'your-distribution-id',
'IfMatch' => $currentConfig['ETag'],
'DistributionConfig' => $config,
]);
Conclusion
- By leveraging the AWS SDK for PHP, you can efficiently manage CDN operations using Amazon CloudFront. The above examples illustrate how to initialize, create, update, and interact with distributions through the CloudFront API for various management tasks.