A Symfony Console implementation to run as a AWS Lambda Function on a container.
Clone the repo and run composer install on its base directory.
composer installThen, it's always a good practice to generate a different App Key (secret)
sed -i "s/^APP_SECRET=.*/APP_SECRET=$(date | md5sum | cut -f1 -d' ')/" .envNow you can already build your container, tag it, and push to AWS ECR to be used on Lambda.
docker build -t local/lambda-php:1.0 .
docker tag local/lambda-php:1.0 <uid>.dkr.ecr.<aws-region>.amazonaws.com/<your-repo>:latest
docker push <uid>.dkr.ecr.<aws-region>.amazonaws.com/<your-repo>:latest* Please replace all the <tags> with valid values that you can find in your aws ecr repo
** Also, remember that you need to be authenticated on aws ecr in the same terminal before pushing
It's actually something quite far from serverless, in the real meaning of the word.
AWS Lambda runs a REST API for your function and expects your function to call this API to check for events. In the positive reply of an event, it passes along the payload so you can handle it and POST a response back to that API.
It spawns your container only when there are events to be requested from this API, so there's no waste of resources. But this container is not only stateless but "apparently" also read-only. Hence the configuration of Symfony Cache being "array". When I tried to write to the disk, it failed.
Apart from writing to its own disk, the handler is quite free to run whatever you want. And the App Environment variables are also available.
Using webdevops/php docker image, everything necessary is already there. So:
- Dockerfile sets
/app/bin/console lambda:serveas the Entrypoint; - "/app/src/Command/Lambda.php" executes that, instantiating the LambdaService;
- "/app/src/Service/LambdaService.php" reads the request, handles it, and sends the response.
The LambdaService handle method
has the $payload of the original request ready to be used as an array.
So you can simply change the contents of this method to add all the code you want.
Add more classes, more dependencies, ...
Just call it from the handle and let it return a string and you should be good.