|
| 1 | +# php-lambda-container-demo |
| 2 | + |
| 3 | +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 contaier image. It deploys an Amazon HTTPs API endpoint to invoke the Lambda function. |
| 4 | + |
| 5 | +The following files and folders are included: |
| 6 | + |
| 7 | +* [runtime/](./0.7-PHP-Lambda-functions-with-Docker-container-images/runtime), code for the custom runtime bootstrap file |
| 8 | + * [bootstrap](./0.7-PHP-Lambda-functions-with-Docker-container-images/runtime/bootstrap), a custom bootstrap file implementing the Lambda Runtime API. |
| 9 | +* [src/](./0.7-PHP-Lambda-functions-with-Docker-container-images/src), code for the application's Lambda function. |
| 10 | + * [index.php](./0.7-PHP-Lambda-functions-with-Docker-container-images/src/index.php), a PHP Lambda function handler |
| 11 | +* [Dockerfile](./0.7-PHP-Lambda-functions-with-Docker-container-images/Dockerfile), a docker file containg commands to assemble an image for a custom Lambda runtime for PHP |
| 12 | +* [readme.md](./0.7-PHP-Lambda-functions-with-Docker-container-images/readme.md) This file. |
| 13 | + |
| 14 | + |
| 15 | +## Testing locally |
| 16 | +1. Build the previous custom runtime image using the Docker build command: |
| 17 | +```bash |
| 18 | +docker build -t phpmyfuntion . |
| 19 | +``` |
| 20 | + |
| 21 | +2. Run the function locally using the Docker run command, bound to port 9000: |
| 22 | +```bash |
| 23 | +docker run -p 9000:8080 phpmyfuntion:latest |
| 24 | +``` |
| 25 | + |
| 26 | +3. This command starts up a local endpoint at `localhost:9000/2015-03-31/functions/function/invocations` |
| 27 | + |
| 28 | + |
| 29 | +4. 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: |
| 30 | + |
| 31 | +```bash |
| 32 | +curl "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"queryStringParameters": {"name":"Ben"}}' |
| 33 | +``` |
| 34 | + |
| 35 | +5. A 200 status response is returned: |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +## Deploy the sample application |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +To use the AWS SAM CLI, you need the following tools: |
| 45 | + |
| 46 | +* AWS SAM CLI - [Install the AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html). |
| 47 | +* Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community). |
| 48 | + |
| 49 | +This [Dockerfile](./0.7-PHP-Lambda-functions-with-Docker-container-images/Dockerfile),) uses the base image for Amazon Linux provided by AWS. The instructions perform the following: |
| 50 | + |
| 51 | +1. Install system-wide Linux packages (zip, curl, tar). |
| 52 | +1. Download and compile PHP. |
| 53 | +1. Download and install composer dependency manager and dependencies. |
| 54 | +1. Move PHP binaries, bootstrap, and vendor dependencies into a directory that Lambda can read from. |
| 55 | +1. Set the container entrypoint. |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +## Deploy the sample application |
| 61 | + |
| 62 | +1. Use the AWS CLI to create a new ECR repository to store the container image for the phpLambdaFuncton. |
| 63 | + |
| 64 | +```bash |
| 65 | +aws ecr create-repository --repository-name php-lambda-functon \ |
| 66 | +--image-tag-mutability IMMUTABLE --image-scanning-configuration scanOnPush=true |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +Make a note of the repositoryUri as you need it in the next step. |
| 71 | + |
| 72 | +2. Authenticate the Docker CLI to your Amazon ECR registry. |
| 73 | + |
| 74 | +``` bash |
| 75 | +aws ecr get-login-password --region {region} | docker login --username AWS --password-stdin {yourAccountID}.dkr.ecr.{region} .amazonaws.com |
| 76 | +``` |
| 77 | +3. Build the application locally |
| 78 | +```bash |
| 79 | +sam build |
| 80 | +``` |
| 81 | + |
| 82 | +4. Use the guided version of the sam deploy command and follow these steps: |
| 83 | + |
| 84 | +```bash |
| 85 | +sam deploy -g |
| 86 | +``` |
| 87 | +* For Stack Name, enter my-php-lambda-container-demo. |
| 88 | +* Choose the same Region that you created the ECR repository in. |
| 89 | +* Enter the Image Repository for the HelloWorldFunction (this is the repositoryUri of the ECR repository). |
| 90 | +* For Confirm changes before deploy and Allow SAM CLI IAM role creation, keep the defaults. |
| 91 | +* For HelloWorldFunction may not have authorization defined, Is this okay? Select Y. |
| 92 | +* Keep the defaults for the remaining prompts: |
| 93 | + |
| 94 | + |
| 95 | +5. The output displays the HTTP APIs endpoint: |
| 96 | +  |
| 97 | + |
| 98 | + |
| 99 | +## Cleanup |
| 100 | + |
| 101 | +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: |
| 102 | + |
| 103 | +```bash |
| 104 | +aws cloudformation delete-stack --stack-name my-php-lambda-container-demo |
| 105 | +``` |
| 106 | + |
| 107 | +## Resources |
| 108 | + |
| 109 | +For an introduction to the AWS SAM specification, the AWS SAM CLI, and serverless application concepts, see the [AWS SAM Developer Guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html). |
| 110 | +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](https://aws.amazon.com/serverless/serverlessrepo/) and the [AWS Serverless Application Repository Developer Guide](https://docs.aws.amazon.com/serverlessrepo/latest/devguide/what-is-serverlessrepo.html). |
0 commit comments