Skip to content

Commit b5f1db8

Browse files
authored
Add Lambda PHP/Bref CDK sample (#207)
* Add Lambda PHP/Bref CDK sample * Add limitation notes Fixes for the mentioned issues are in the works ... * Fix CI build by removing tty Also remove unsupported `LAMBDA_DOCKER_FLAGS` by the Docker SDK client * Bump node version to fix Bref CDK build The AWS CDK requires at least Node v14 aws/aws-cdk@d0a27c1 Trying to bump conservatively to Node v14: https://github.com/localstack/localstack-pro-samples/blob/master/.github/workflows/makefile.yml#L21 Even for v14, maintenance EOL is coming soon: https://github.com/nodejs/Release * Bump serverless version to fix Node compatibility Requires more follow up work before migrating to v3: ``` Serverless: Deprecation warnings: Variables resolver reports following resolution errors: - Cannot resolve variable at "resources.Resources.ApiGatewayRestApi.Properties.Name": Value not found at "self" source, - Cannot resolve variable at "resources.Outputs.RestApiId.Export.Name": Value not found at "self" source From a next major this will be communicated with a thrown error. Set "variablesResolutionMode: 20210326" in your service config, to adapt to new behavior now More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_VARIABLES_RESOLVER Resolution of lambda version hashes was improved with better algorithm, which will be used in next major release. Switch to it now by setting "provider.lambdaHashingVersion" to "20201221". While it is highly encouraged to upgrade to new algorithm, you can still use the old approach by setting "provider.lambdaHashingVersion" to "20200924". More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2 ``` * Add PHP/Bref to top-level readme * Fix qldb exception by restricting breaking dependency update * Remove workarounds for fixed issues
1 parent dd6939a commit b5f1db8

File tree

22 files changed

+12471
-6596
lines changed

22 files changed

+12471
-6596
lines changed

.github/workflows/makefile.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Nodejs
1919
uses: actions/setup-node@v3
2020
with:
21-
node-version: 12
21+
node-version: 14
2222

2323
# see https://github.com/localstack/localstack/pull/6831
2424
# remove once no longer needed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ Some of the samples require LocalStack Pro features. Please make sure to properl
5454
| [Glue for ETL jobs](glue-etl-jobs) | Using Glue API to run local ETL jobs |
5555
| [Message Queue broker](mq-broker) | Using MQ API to run local message queue brokers |
5656
| [ELB Load Balancing](elb-load-balancing) | Using ELBv2 Application Load Balancers locally, deployed via the Serverless framework |
57-
| [Reproducible ML](reproducible-ml) | Train, save and evaluate a scikit-learn machine learning model using AWS Lambda and S3 |
58-
57+
| [Reproducible ML](reproducible-ml) | Train, save and evaluate a scikit-learn machine learning model using AWS Lambda and S3 |
58+
| [Lambda PHP/Bref CDK App](lambda-php-bref-cdk-app) | Running PHP/Bref Lambda handler locally, deployed via AWS CDK |
5959

6060
## Checking out a single sample
6161

lambda-php-bref-cdk-app/.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.js
2+
!jest.config.js
3+
*.d.ts
4+
node_modules
5+
6+
# CDK asset staging directory
7+
.cdk.staging
8+
cdk.out
9+
cdk-outputs.json
10+
11+
# PHP vendor
12+
backend/vendor

lambda-php-bref-cdk-app/.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
*.ts
2+
!*.d.ts
3+
4+
# CDK asset staging directory
5+
.cdk.staging
6+
cdk.out

lambda-php-bref-cdk-app/Makefile

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
export AWS_DEFAULT_REGION ?= us-east-1
4+
5+
# Using composer Docker image to avoid local PHP dependency:
6+
# https://hub.docker.com/_/composer
7+
DOCKER_COMPOSER_IMAGE ?= composer:2.5
8+
9+
usage: ## Show this help
10+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
11+
12+
install: ## Install dependencies
13+
@npm install
14+
@which cdklocal || npm install -g aws-cdk-local aws-cdk
15+
@which localstack || pip install localstack
16+
echo "Install Bref PHP dependencies via composer"
17+
make install-bref-docker
18+
19+
install-bref:
20+
cd backend && \
21+
composer install
22+
23+
install-bref-docker: ## Install Bref dependencies via composer into backend/vendor
24+
cd backend && \
25+
docker run --rm --volume $$PWD:/app --user $$(id -u):$$(id -g) $(DOCKER_COMPOSER_IMAGE) composer install
26+
27+
run: ## Deploy the app locally and invoke Lambda through API gateway
28+
echo "Bootstrapping CDK"; \
29+
cdklocal bootstrap && \
30+
echo "Deploying CDK app to local environment"; \
31+
cdklocal deploy --outputs-file cdk-outputs.json --require-approval never && \
32+
echo "CDK app successfully deployed. Now trying to invoke the Lambda through API gateway." && \
33+
make invoke
34+
35+
invoke:
36+
endpoint=$$(jq .CdkBrefStack.Url cdk-outputs.json --raw-output) && \
37+
echo endpoint=$${endpoint} && \
38+
curl $${endpoint}?name=LocalStack!
39+
40+
start:
41+
PROVIDER_OVERRIDE_lambda=v2 localstack start -d
42+
43+
stop:
44+
@echo
45+
localstack stop
46+
ready:
47+
@echo Waiting on the LocalStack container...
48+
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)
49+
50+
logs:
51+
@localstack logs > logs.txt
52+
53+
test-ci:
54+
make start install ready run; return_code=`echo $$?`;\
55+
make logs; make stop; exit $$return_code;
56+
57+
.PHONY: usage install install-bref install-bref-docker start run invoke stop ready logs test-ci

lambda-php-bref-cdk-app/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "localstack/bref-hello-world",
3+
"type": "project",
4+
"require": {
5+
"bref/bref": "^1.7.16"
6+
}
7+
}

0 commit comments

Comments
 (0)