Amazon EventBridgeからLambda(PHP)を呼び出す

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
The Serverless Framework documentation for AWS Lambda, API Gateway, EventBridge, DynamoDB and much more.

コメント

株式会社CONFRAGE ITソリューション事業部をもっと見る

今すぐ購読し、続きを読んで、すべてのアーカイブにアクセスしましょう。

続きを読む

タイトルとURLをコピーしました