Skip to content

Commit

Permalink
Add Lambda PHP/Bref CDK sample (#207)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
joe4dev authored Feb 27, 2023
1 parent dd6939a commit b5f1db8
Show file tree
Hide file tree
Showing 22 changed files with 12,471 additions and 6,596 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/makefile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Setup Nodejs
uses: actions/setup-node@v3
with:
node-version: 12
node-version: 14

# see https://github.com/localstack/localstack/pull/6831
# remove once no longer needed
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ Some of the samples require LocalStack Pro features. Please make sure to properl
| [Glue for ETL jobs](glue-etl-jobs) | Using Glue API to run local ETL jobs |
| [Message Queue broker](mq-broker) | Using MQ API to run local message queue brokers |
| [ELB Load Balancing](elb-load-balancing) | Using ELBv2 Application Load Balancers locally, deployed via the Serverless framework |
| [Reproducible ML](reproducible-ml) | Train, save and evaluate a scikit-learn machine learning model using AWS Lambda and S3 |

| [Reproducible ML](reproducible-ml) | Train, save and evaluate a scikit-learn machine learning model using AWS Lambda and S3 |
| [Lambda PHP/Bref CDK App](lambda-php-bref-cdk-app) | Running PHP/Bref Lambda handler locally, deployed via AWS CDK |

## Checking out a single sample

Expand Down
12 changes: 12 additions & 0 deletions lambda-php-bref-cdk-app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
*.js
!jest.config.js
*.d.ts
node_modules

# CDK asset staging directory
.cdk.staging
cdk.out
cdk-outputs.json

# PHP vendor
backend/vendor
6 changes: 6 additions & 0 deletions lambda-php-bref-cdk-app/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
57 changes: 57 additions & 0 deletions lambda-php-bref-cdk-app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION ?= us-east-1

# Using composer Docker image to avoid local PHP dependency:
# https://hub.docker.com/_/composer
DOCKER_COMPOSER_IMAGE ?= composer:2.5

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

install: ## Install dependencies
@npm install
@which cdklocal || npm install -g aws-cdk-local aws-cdk
@which localstack || pip install localstack
echo "Install Bref PHP dependencies via composer"
make install-bref-docker

install-bref:
cd backend && \
composer install

install-bref-docker: ## Install Bref dependencies via composer into backend/vendor
cd backend && \
docker run --rm --volume $$PWD:/app --user $$(id -u):$$(id -g) $(DOCKER_COMPOSER_IMAGE) composer install

run: ## Deploy the app locally and invoke Lambda through API gateway
echo "Bootstrapping CDK"; \
cdklocal bootstrap && \
echo "Deploying CDK app to local environment"; \
cdklocal deploy --outputs-file cdk-outputs.json --require-approval never && \
echo "CDK app successfully deployed. Now trying to invoke the Lambda through API gateway." && \
make invoke

invoke:
endpoint=$$(jq .CdkBrefStack.Url cdk-outputs.json --raw-output) && \
echo endpoint=$${endpoint} && \
curl $${endpoint}?name=LocalStack!

start:
PROVIDER_OVERRIDE_lambda=v2 localstack start -d

stop:
@echo
localstack stop
ready:
@echo Waiting on the LocalStack container...
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)

logs:
@localstack logs > logs.txt

test-ci:
make start install ready run; return_code=`echo $$?`;\
make logs; make stop; exit $$return_code;

.PHONY: usage install install-bref install-bref-docker start run invoke stop ready logs test-ci
103 changes: 103 additions & 0 deletions lambda-php-bref-cdk-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# LocalStack Demo: Deploying PHP/Bref Lambda via CDK

Simple [PHP/Bref](https://bref.sh/) 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](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).
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.

## PHP/Bref with fpm and Serverless

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.
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](https://github.com/php-runtime/bref) and select the correct layer with or without `fpm` accordingly.

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:

1. Install the [serverless-localstack](https://github.com/LocalStack/serverless-localstack) plugin

```bash
npm install --save-dev serverless-localstack
```

2. Add serverless-localstack to `plugins` in the [serverless.yml](https://github.com/brefphp/examples/blob/master/Laravel/base/serverless.yml)

```yml
plugins:
- ./vendor/bref/bref
- serverless-localstack
```

3. Add `custom` properties in the `serverless.yml`

```yml
custom:
localstack:
# list of stages for which the plugin should be enabled
stages:
- local
```

4. Deploy to LocalStack

```bash
serverless deploy --stage local
```

Start localstack with:

* `LAMBDA_DOCKER_FLAGS=--user nobody` until [this user permission issue](https://github.com/localstack/localstack/issues/7722) is resolved for running `fpm`.
* `PROVIDER_OVERRIDE_LAMBDA=v2` until the [new Lambda provider implementation](https://github.com/localstack/localstack/pull/6724) becomes the default in LocalStack Version 2.

## Prerequisites

* LocalStack
* Docker
* `make`
* `curl`
* `jq`
* Node.js / `npm`
* [`cdklocal`](https://github.com/localstack/aws-cdk-local)

## Installing

To install the dependencies:
```
make install
```

## Starting LocalStack

Make sure that LocalStack is started:
```
LOCALSTACK_API_KEY=... PROVIDER_OVERRIDE_LAMBDA=v2 LAMBDA_DOCKER_FLAGS="--user nobody" DEBUG=1 localstack start
```

## Running

Deploy the app locally and run an HTTP test invocation:
```bash
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.
7 changes: 7 additions & 0 deletions lambda-php-bref-cdk-app/backend/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "localstack/bref-hello-world",
"type": "project",
"require": {
"bref/bref": "^1.7.16"
}
}
Loading

0 comments on commit b5f1db8

Please sign in to comment.