This project contains source code and supporting files for a serverless application that you can deploy with the AWS Serverless Application Model (AWS SAM) command line interface (CLI). It contains a custom runtime PHP Lambda function packaged as a docker container image. It deploys an Amazon HTTPs API endpoint to invoke the Lambda function.
The following files and folders are included:
- runtime/, code for the custom runtime bootstrap file
- bootstrap, a custom bootstrap file implementing the Lambda Runtime API.
- src/, code for the application's Lambda function.
- index.php, a PHP Lambda function handler
- Dockerfile, a docker file containg commands to assemble an image for a custom Lambda runtime for PHP
- readme.md This file.
- template.yaml The application AWS SAM (serverless application model) template.
- Build the previous custom runtime image using the Docker build command:
docker build -t phpmyfunction .
- Run the function locally using the Docker run command, bound to port 9000:
docker run -p 9000:8080 phpmyfunction:latest
-
This command starts up a local endpoint at
localhost:9000/2015-03-31/functions/function/invocations
-
Post an event to this endpoint using a curl command. The Lambda function payload is provided by using the -d flag. This is a valid Json object required by the Runtime Interface Emulator:
curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"queryStringParameters": {"name":"Ben"}}'
- A 200 status response is returned:
The AWS SAM CLI is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
To use the AWS SAM CLI, you need the following tools:
- AWS SAM CLI - Install the AWS SAM CLI.
- Docker - Install Docker community edition.
This Dockerfile,) uses the base image for Amazon Linux provided by AWS. The instructions perform the following:
- Install system-wide Linux packages (zip, curl, tar).
- Download and compile PHP.
- Download and install composer dependency manager and dependencies.
- Move PHP binaries, bootstrap, and vendor dependencies into a directory that Lambda can read from.
- Set the container entrypoint.
- Build the application locally
sam build
- Use the guided version of the sam deploy command and follow these steps: (AWS SAM will create a new ECR repository to store the container image for the phpLambdaFunction)
sam deploy -g
- For Stack Name, enter my-php-lambda-container-demo.
- Choose the region that you want to deploy to.
- For Confirm changes before deploy and Allow SAM CLI IAM role creation, keep the defaults.
- For HelloWorldFunction may not have authorization defined, Is this okay? Select Y.
- Keep the defaults for the remaining prompts:
-
Send a POST request to the endpoint URL to invoke the Lambda function: (replace the api-gateway-url below with the value from the previous step)
curl "{api-gateway-url}?name=Ben"
To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:
aws cloudformation delete-stack --stack-name my-php-lambda-container-demo
For an introduction to the AWS SAM specification, the AWS SAM CLI, and serverless application concepts, see the AWS SAM Developer Guide. Next, you can use the AWS Serverless Application Repository to deploy ready-to-use apps that go beyond Hello World samples and learn how authors developed their applications. For more information, see the AWS Serverless Application Repository main page and the AWS Serverless Application Repository Developer Guide.