Amazon EventBridgeからLambda(PHP)を呼び出す
前提
| 項目 | バージョン |
|---|---|
| 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';
return function ($event) {
echo "ok";
return "ok";
};
serverless.yml
serverless.ymlファイルでeventsを指定します。これによりEventBridgeが作成されます。
service: app
provider:
name: aws
region: us-east-1
runtime: provided.al2
deploymentMethod: direct # fastest deployment method
plugins:
- ./vendor/bref/bref
functions:
hello:
handler: index.php
description: ''
layers:
- ${bref:layer.php-74}
events:
- schedule: rate(1 minute) #★1分ごと
# Exclude files from deployment
package:
patterns:
- '!tests/**'
デプロイします。
$ sls deploy
Lambda実行すると、1分ごとにLambdaが実行されていることがログから確認できます。
参考サイト

Cron tasks on AWS Lambda – Bref
Learn how to create serverless cron functions with PHP on AWS Lambda.

Serverless Framework - AWS Lambda Events - Scheduled & Recurring
Setting up Scheduled, Recurring, CRON Task Events with AWS Lambda via the Serverless Framework

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


コメント