Skip to content

Latest commit

 

History

History

lambda-php-bref-cdk-app

LocalStack Demo: Deploying PHP/Bref Lambda via CDK

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.

PHP/Bref with fpm and Serverless

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:

  1. Install the serverless-localstack plugin

    npm install --save-dev serverless-localstack
  2. Add serverless-localstack to plugins in the serverless.yml

    plugins:
    - ./vendor/bref/bref
    - serverless-localstack
  3. Add custom properties in the serverless.yml

    custom:
      localstack:
        # list of stages for which the plugin should be enabled
        stages:
          - local
  4. Deploy to LocalStack

    serverless deploy --stage local

Prerequisites

  • LocalStack
  • Docker
  • make
  • curl
  • jq
  • Node.js / npm
  • cdklocal

Installing

To install the dependencies:

make install

Starting LocalStack

Make sure that LocalStack is started:

LOCALSTACK_AUTH_TOKEN=$LOCALSTACK_AUTH_TOKEN DEBUG=1 localstack start

Running

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!%

License

This code is available under the Apache 2.0 license.