API Gatewayから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';
use Nyholm\Psr7\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
class HttpHandler implements RequestHandlerInterface
{
public function handle(ServerRequestInterface $request): ResponseInterface
{
return new Response(200, [], "ok");
}
}
return new HttpHandler();
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:
- httpApi: 'GET /articles/{id}'
# Exclude files from deployment
package:
patterns:
- '!tests/**'
デプロイします。
$ sls deploy
API Gateway+Lambdaがデプロイされます。
curlコマンドでテストします。
$ curl -i -X GET https://2ok1sz54md.execute-api.us-east-1.amazonaws.com/articles/takahashi HTTP/2 200 date: Sun, 05 Feb 2023 22:10:49 GMT content-type: text/plain; charset=utf-8 content-length: 2 apigw-requestid: f4toei0-IAMESQA= ok
参考サイト

PHP functions runtime for AWS Lambda – Bref
Run serverless event-driven PHP functions on AWS Lambda using Bref.

Serverless Framework - AWS Lambda Events - HTTP API (API Gateway v2)
Deploying HTTP APIs with AWS Lambda and API Gateway v2 via the Serverless Framework

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




コメント