Executes PHP code within an AWS Lambda function using Docker containers.
All the heavy-lifting - such as interacting with the Lambda runtime API and proxying requests between API Gateway and PHP itself - is handled by the base image and a package provided by Bref.
Here we use CloudFormation templates to create the AWS resources - you could also use the AWS Management Console or CLI. Ensure you have Docker Desktop, the AWS CLI, and Composer installed.
-
Create the AWS ECR private repository using the
repository.jsonCloudFormation template. -
Create the IAM user using the
user.jsonCloudFormation template and note the access key and secret access key available as outputs. Specify the above stack name as a parameter. -
Add a new profile to your AWS CLI (to make things easier, keep the profile name the same as the
IMAGE_NAMEvariable in your.envfile):aws configure --profile php-docker-lambda -
Create your
.envfile by copying the example file and filling in the details (leaveLAMBDA_FUNCTION_NAMEfor now):cp .env.example .env -
Build the Docker image and push it up to the ECR repository using the
buildbash script:sh ./build -
Create the Lambda function using the
lambda.jsonCloudFormation template. Specify the URI of the image in ECR as a parameter. -
Update
LAMBDA_FUNCTION_NAMEvariable in your.envfile (helpful for future updates - see below). -
Create the API Gateway using the
api-gateway.jsonCloudFormation template. You will need to specify the name of the stack that created the Lambda function. The invocation URL is available asInvocationURLstack output - visiting that URL will invoke the Lambda function.
When you make updates to the source code, you will need to build and push the image again:
sh ./build
The Lambda function will continue to use the previous image until you re-deploy it using the deploy bash script:
sh ./deploy