The basics of using SAM Cli for AWS Lambda PHP applications.
- Install SAM Cli by following the Quick Start
- Set up your AWS credentials
- Select one under the options
- Create php binary by following steps in
doc/create_php_binary.md
- Use prebuilt Php 7.3.1
- Just put the yml this projects ARN
arn:aws:lambda:eu-central-1:303814004728:layer:aws-lambda-layer-php-7-3-1:6
- Create php binary by following steps in
- Write your serverless application (!) - the default is in
src/handlers/hello.php
- Run
./deploy.sh
to deploy to Lambda
Handler: filename.functionName hello.hello The signature for the PHP function is:
function main($eventData) : array
Hello world looks like:
<?php
function hello($eventData) : array
{
return ["msg" => "Hello from PHP " . PHP_VERSION];
}
Run:
echo "hi" | sam local invoke HelloPhp73
Result:
{"msg":"hello from PHP 7.3.1","eventData":"hi\n","data":null}
Run:
sam local invoke HelloPhp73 --event=events/hello.sampledata.json
Result:
{
"msg": "hello from PHP 7.3.1",
"eventData": "{\n \"id\": \"ulbimesm12ym12kmzkzfgkzkuzeyfg\",\n \"name\": \"Lambda\",\n \"icon\": \"\u03bb\"\n}",
"data": {
"id": "ulbimesm12ym12kmzkzfgkzkuzeyfg",
"name": "Lambda",
"icon": "\u03bb"
}
}