| Key | Value |
|---|---|
| Services | Lambda, API Gateway |
| Integrations | AWS CDK, Bref, PHP |
| Categories | Serverless; PHP |
Introduction
A PHP/Bref serverless application using a shared Lambda layer deployable with AWS CDK to LocalStack. The application implements a typed PHP Lambda handler as an HTTP handler class for serving API Gateway HTTP events. Bref turns API Gateway events into PSR-7 requests for PHP processing.
Prerequisites
- A valid LocalStack for AWS license. Your license provides a
LOCALSTACK_AUTH_TOKENto activate LocalStack. - Docker
localstackCLIcdklocal— install withnpm install -g aws-cdk-local- Node.js with
npm curlandjq
Check prerequisites
make check
Installation
make install
Start LocalStack
export LOCALSTACK_AUTH_TOKEN=<your-auth-token> make start
Run the application
make run
The script bootstraps and deploys the CDK app locally, then invokes the HTTP endpoint via curl. You should see output similar to:
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=https://bd0f6b19.execute-api.localhost.localstack.cloud:4566/
Hello LocalStack!
PHP/Bref with FPM and Serverless Framework
Bref also supports running web applications on AWS Lambda via php-fpm, enabling traditional PHP frameworks like Laravel and Symfony. To deploy a php-fpm Laravel project from brefphp/examples to LocalStack:
-
Install the serverless-localstack plugin:
npm install --save-dev serverless-localstack
-
Add
serverless-localstacktopluginsinserverless.yml:plugins: - ./vendor/bref/bref - serverless-localstack
-
Add
customproperties:custom: localstack: stages: - local
-
Deploy:
serverless deploy --stage local
License
This code is available under the Apache 2.0 license.