この記事で解決できる悩み

こんな悩みを解決できる記事を書きました!
僕は現役フリーランスエンジニア(歴9年)で、資格は13個保有しています。
結論、PHP(Laravel)でCloudFrontのキャッシュを削除する方法は超簡単です。


本記事を読めば、AWS SDKを使用してPHP(Laravel)でCloudFrontのキャッシュを削除する方法について理解できるので早速見ていきましょう!



すぐ読み終わるので、ぜひ最後まで読んでくださいね♪
目次
【前提】環境
- ライブラリ「aws-sdk」がインストールされていること。
PHP(Laravel)でCloudFrontのキャッシュを削除する方法
では早速、PHP(Laravel)でCloudFrontのキャッシュを削除する方法を解説します。
<?php
namespace App\Services\Aws;
use Aws\CloudFront\CloudFrontClient;
/**
* CloudFrontサービス
*/
class CloudFrontService
{
private $client;
public function __construct()
{
$this->client = new CloudFrontClient([
'version' => '*** version ***',
'region' => '*** region ***',
'credentials' => [
'key' => '*** key ***',
'secret' => '*** secret ***',
],
]);
}
/**
* 無効化バッチを作成する
*
* @link https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-cloudfront-2019-03-26.html#createinvalidation
*/
public function createInvalidation()
{
$this->client->createInvalidation([
'DistributionId' => '*** DistributionId ***',
'InvalidationBatch' => [
'Paths' => [
'Quantity' => count(['/*']),
'Items' => ['/*'],
],
'CallerReference' => time(),
],
]);
}
}
↑がPHP(Laravel)でCloudFrontのキャッシュを削除するソースコードです。



CloudFrontのキャッシュを削除するにはcreateInvalidationを使用します。
まとめ
今回は、AWS SDKを使用してPHP(Laravel)でCloudFrontのキャッシュを削除する方法について解説しました。
以下が本記事のまとめになります。
- おすすめ本
Laravelの勉強なら「PHPフレームワーク Laravel入門 第2版



最後までお読みいただき、ありがとうございました!
- クソおすすめ本



海外のエンジニアがどういった思考で働いているかが理解できます。
海外に行く気はないけど海外エンジニアの動向が気になる雑魚エンジニアにおすすめです(本当におすすめな本しか紹介しないのでご安心を)。