Simple PHP/Bref serverless application using a shared Lambda layer deployable with AWS CDK to LocalStack.
This PHP/Bref application without fpm implements a typed PHP Lambda handler as an HTTP handler class for serving API Gateway HTTP events. Bref turns an API Gateway event into a PSR-7request and one Lambda per route implements a handler class and returns a PSR-7 response.
Bref typically runs Web applications on AWS Lambda to support traditional PHP frameworks such as Laravel and Symphony.
In this php-fpm
approach, Bref turns an API Gateway event into a FastCGI (PHP-FPM) request and one Lambda receives all URLs and responds using echo
, header()
function, etc.
Checkout the different kinds of applications at php-runtime/bref and select the correct layer with or without fpm
accordingly.
To deploy the php-fpm
Laravel base project from brefphp/examples to LocalStack:
-
Install the serverless-localstack plugin
npm install --save-dev serverless-localstack
-
Add serverless-localstack to
plugins
in the serverless.ymlplugins: - ./vendor/bref/bref - serverless-localstack
-
Add
custom
properties in theserverless.yml
custom: localstack: # list of stages for which the plugin should be enabled stages: - local
-
Deploy to LocalStack
serverless deploy --stage local
- LocalStack
- Docker
make
curl
jq
- Node.js /
npm
cdklocal
To install the dependencies:
make install
Make sure that LocalStack is started:
LOCALSTACK_AUTH_TOKEN=$LOCALSTACK_AUTH_TOKEN DEBUG=1 localstack start
Deploy the app locally and run an HTTP test invocation:
make run
The script first bootstraps and deploys the CDK app locally and subsequently invokes the HTTP endpoint via curl (make invoke
).
Outputs:
CdkBrefStack.Url = https://bd0f6b19.execute-api.localhost.localstack.cloud:4566/
Stack ARN:
arn:aws:cloudformation:us-east-1:000000000000:stack/CdkBrefStack/dec480c5
✨ Total time: 7.9s
CDK app successfully deployed. Now trying to invoke the Lambda through API gateway.
endpoint=$(jq .CdkBrefStack.Url cdk-outputs.json --raw-output) && \
echo endpoint=${endpoint} && \
curl ${endpoint}?name=LocalStack!
endpoint=https://bd0f6b19.execute-api.localhost.localstack.cloud:4566/
Hello LocalStack!%
This code is available under the Apache 2.0 license.