|
| 1 | +# LocalStack Demo: Deploying PHP/Bref Lambda via CDK |
| 2 | + |
| 3 | +Simple [PHP/Bref](https://bref.sh/) serverless application using a shared Lambda layer deployable with AWS CDK to LocalStack. |
| 4 | + |
| 5 | +This PHP/Bref application **without fpm** implements a [typed PHP Lambda handler](https://bref.sh/docs/function/handlers.html) as an *HTTP handler class* for serving [API Gateway HTTP events](https://bref.sh/docs/function/handlers.html#api-gateway-http-events). |
| 6 | +Bref turns an API Gateway event into a [PSR-7](https://www.php-fig.org/psr/psr-7/)request and one Lambda per route implements a handler class and returns a PSR-7 response. |
| 7 | + |
| 8 | +## PHP/Bref with fpm and Serverless |
| 9 | + |
| 10 | +Bref typically runs [Web applications on AWS Lambda](https://bref.sh/docs/runtimes/http.html) to support traditional PHP frameworks such as Laravel and Symphony. |
| 11 | +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. |
| 12 | +Checkout the different kinds of applications at [php-runtime/bref](https://github.com/php-runtime/bref) and select the correct layer with or without `fpm` accordingly. |
| 13 | + |
| 14 | +To deploy the `php-fpm` Laravel [base](https://github.com/brefphp/examples/tree/master/Laravel/base) project from [brefphp/examples](https://github.com/brefphp/examples) to LocalStack: |
| 15 | + |
| 16 | +1. Install the [serverless-localstack](https://github.com/LocalStack/serverless-localstack) plugin |
| 17 | + |
| 18 | + ```bash |
| 19 | + npm install --save-dev serverless-localstack |
| 20 | + ``` |
| 21 | + |
| 22 | +2. Add serverless-localstack to `plugins` in the [serverless.yml](https://github.com/brefphp/examples/blob/master/Laravel/base/serverless.yml) |
| 23 | + |
| 24 | + ```yml |
| 25 | + plugins: |
| 26 | + - ./vendor/bref/bref |
| 27 | + - serverless-localstack |
| 28 | + ``` |
| 29 | + |
| 30 | +3. Add `custom` properties in the `serverless.yml` |
| 31 | + |
| 32 | + ```yml |
| 33 | + custom: |
| 34 | + localstack: |
| 35 | + # list of stages for which the plugin should be enabled |
| 36 | + stages: |
| 37 | + - local |
| 38 | + ``` |
| 39 | + |
| 40 | +4. Deploy to LocalStack |
| 41 | + |
| 42 | + ```bash |
| 43 | + serverless deploy --stage local |
| 44 | + ``` |
| 45 | + |
| 46 | +Start localstack with: |
| 47 | + |
| 48 | +* `LAMBDA_DOCKER_FLAGS=--user nobody` until [this user permission issue](https://github.com/localstack/localstack/issues/7722) is resolved for running `fpm`. |
| 49 | +* `PROVIDER_OVERRIDE_LAMBDA=v2` until the [new Lambda provider implementation](https://github.com/localstack/localstack/pull/6724) becomes the default in LocalStack Version 2. |
| 50 | + |
| 51 | +## Prerequisites |
| 52 | + |
| 53 | +* LocalStack |
| 54 | +* Docker |
| 55 | +* `make` |
| 56 | +* `curl` |
| 57 | +* `jq` |
| 58 | +* Node.js / `npm` |
| 59 | +* [`cdklocal`](https://github.com/localstack/aws-cdk-local) |
| 60 | + |
| 61 | +## Installing |
| 62 | + |
| 63 | +To install the dependencies: |
| 64 | +``` |
| 65 | +make install |
| 66 | +``` |
| 67 | + |
| 68 | +## Starting LocalStack |
| 69 | + |
| 70 | +Make sure that LocalStack is started: |
| 71 | +``` |
| 72 | +LOCALSTACK_API_KEY=... PROVIDER_OVERRIDE_LAMBDA=v2 LAMBDA_DOCKER_FLAGS="--user nobody" DEBUG=1 localstack start |
| 73 | +``` |
| 74 | + |
| 75 | +## Running |
| 76 | + |
| 77 | +Deploy the app locally and run an HTTP test invocation: |
| 78 | +```bash |
| 79 | +make run |
| 80 | +``` |
| 81 | + |
| 82 | +The script first bootstraps and deploys the CDK app locally and subsequently invokes the HTTP endpoint via curl (`make invoke`). |
| 83 | + |
| 84 | +``` |
| 85 | +Outputs: |
| 86 | +CdkBrefStack.Url = https://bd0f6b19.execute-api.localhost.localstack.cloud:4566/ |
| 87 | +Stack ARN: |
| 88 | +arn:aws:cloudformation:us-east-1:000000000000:stack/CdkBrefStack/dec480c5 |
| 89 | +
|
| 90 | +✨ Total time: 7.9s |
| 91 | +
|
| 92 | +
|
| 93 | +CDK app successfully deployed. Now trying to invoke the Lambda through API gateway. |
| 94 | +endpoint=$(jq .CdkBrefStack.Url cdk-outputs.json --raw-output) && \ |
| 95 | + echo endpoint=${endpoint} && \ |
| 96 | + curl ${endpoint}?name=LocalStack! |
| 97 | +endpoint=https://bd0f6b19.execute-api.localhost.localstack.cloud:4566/ |
| 98 | +Hello LocalStack!% |
| 99 | +``` |
| 100 | + |
| 101 | +## License |
| 102 | + |
| 103 | +This code is available under the Apache 2.0 license. |
0 commit comments