Lambda(PHP)をbref localコマンドでローカル実行する
前提
| 項目 | バージョン |
|---|---|
| OS | Ubuntu 20.04.4 LTS |
| PHP | PHP 7.4.3-4ubuntu2.17 (cli) (built: Jan 10 2023 15:37:44) ( NTS ) |
Composerインストール済みであること
$ sudo apt update $ sudo apt install -y php-curl $ sudo apt install -y php7.4-xml $ sudo apt install -y composer
aws sdkインストール
composerでaws sdkをインストールします。
$ composer require bref/bref $ vendor/bin/bref init What kind of lambda do you want to create? (you will be able to add more functions later by editing `serverless.yml`) [Web application]: [0] Web application [1] Event-driven function > 1 $ composer require aws/aws-sdk-php
index.php
index.php作成します。シークレットマネージャにアクセスしてログ出力しています。
<?php
require __DIR__ . '/vendor/autoload.php';
use Aws\SecretsManager\SecretsManagerClient;
use Aws\Exception\AwsException;
return function ($event) {
$client = new SecretsManagerClient([
'version' => '2017-10-17',
'region' => 'ap-northeast-1',
]);
try {
$result = $client->getSecretValue([
'SecretId' => 'xxx' // シークレット名
]);
} catch (AwsException $e) {
$error = $e->getAwsErrorCode();
var_dump($error);
exit(500);
}
$secret = json_decode($result['SecretString'],true);
var_dump($secret); // ログ出力
return "ok";
};
bref localコマンド
serverless.ymlで指定したファンクション(以下ではhello)をローカルで実行します。
serverless.ymlの一部
functions:
hello:
handler: index.php
bref localコマンドでindex.phpをローカルで実行します。引数にはserverless.ymlで指定したファンクション名を指定します。
$ vendor/bin/bref local hello
START RequestId: 8f507cfc-example-4697-b07a-ac58fc914c95 Version: $LATEST
array(5) {
["dbInstanceIdentifier"]=>
string(10) "database-c"
["engine"]=>
string(7) "mariadb"
["resourceId"]=>
string(29) "db-GTICCHJASIYNJADTJNZALFF2YY"
["username"]=>
string(5) "admin"
["password"]=>
string(8) "1234abcd"
}
END RequestId: 8f507cfc-example-4697-b07a-ac58fc914c95
REPORT RequestId: 8f507cfc-example-4697-b07a-ac58fc914c95 Duration: 2433 ms Billed Duration: 2433 ms Memory Size: 1024 MB Max Memory Used: 11 MB
"ok"
$
参考サイト

Local development for functions ??? Bref

KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES20xx),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^


コメント