An example setup to run PHP inside a Docker container on AWS Lambda using Bref.
- Composer
- Node.js and NPM
- AWS account
- AWS CLI installed and configured with credentials
- A centralised Serverless Framework deployment S3 bucket using the format {aws-account-id}--serverless-deploys
Replace {aws-account-id} with your AWS account ID. Create the bucket with default settings.
Install NPM and Composer dependencies
npm installBuild Docker image, deploy infrastructure and Lambda PHP handler. Appends -dev to resource names
npx serverless deployOnce deployed the console will output a URL endpoint like below. Open the URL and you should see Hello World output to the
screen. If you add the query string ?name=YOUR_NAME to the URL, it will output Hello YOUR_NAME.
Deploying php-docker to stage dev (us-east-1)
✔ Service deployed to stack php-docker-dev (85s)
endpoint: https://ysgw63bbcfifirgzjxws3wgtm40thets.lambda-url.us-east-1.on.aws/
functions:
handler: php-docker-dev-handlerDeploy infrastructure with a specific stage name appended to resource names. In the example below resources will have -production appended to them.
npx serverless deploy --stage productionThe PHP application is inside the app folder along with the vendor (Composer dependencies) folder. The handler.php file is the event handler called by Lambda. Place all your PHP files in the app folder and call them from the handler.php handler.
You can run the Docker container locally (once the image is built) using the following command:
docker run --rm -it --entrypoint= -v ./app:/var/task:ro serverless-php-docker-dev:bref-php-app vendor/bin/bref-local handler.phpYou can also pass event parameters by adding the JSON event payload to the end of the command, like this:
docker run --rm -it --entrypoint= -v ./app:/var/task:ro serverless-php-docker-dev:bref-php-app vendor/bin/bref-local handler.php '{"queryStringParameters": {"name": "Jane"}}'There is also an npm command you can use which is much shorter:
npm run localand with parameters:
npm run local '{"queryStringParameters": {"name": "Jane"}}'