|
1 | 1 | # PHP Layer For AWS Lambda
|
2 | 2 |
|
3 |
| -Ever want to run PHP websites in AWS Lambda? It's your lucky day! |
| 3 | +Ever wanted to run PHP websites in AWS Lambda? It's your lucky day! This Lambda Runtime Layer runs the [PHP 7.1 webserver](http://php.net/manual/en/features.commandline.webserver.php) in response to [AWS API Gateway](https://aws.amazon.com/api-gateway/) requests. |
4 | 4 |
|
5 |
| -**:warning: This is an experimental AWS Lambda Layer and Runtime. It should not be used for production services. Obligatory legal statement follows:** |
| 5 | +**:warning: This is an experimental AWS Lambda Runtime Layer. It should not be used for production services. Please read the disclaimer at the bottom of this readme before using!** |
6 | 6 |
|
7 |
| -> STACKERY INC. AND/OR ITS RESPECTIVE SUPPLIERS MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY, RELIABILITY, AVAILABILITY, TIMELINESS, AND ACCURACY OF THE INFORMATION, SOFTWARE, PRODUCTS, SERVICES AND RELATED GRAPHICS CONTAINED ON THE MSN WEB SITES FOR ANY PURPOSE. ALL SUCH INFORMATION, SOFTWARE, PRODUCTS, SERVICES AND RELATED GRAPHICS ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT AND/OR ITS RESPECTIVE SUPPLIERS HEREBY DISCLAIM ALL WARRANTIES AND CONDITIONS WITH REGARD TO THIS INFORMATION, SOFTWARE, PRODUCTS, SERVICES AND RELATED GRAPHICS, INCLUDING ALL IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL MICROSOFT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY DIRECT, INDIRECT, PUNITIVE, INCIDENTAL, SPECIAL, CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF USE, DATA OR PROFITS, ARISING OUT OF OR IN ANY WAY CONNECTED WITH THE USE OR PERFORMANCE OF THE MSN WEB SITES, WITH THE DELAY OR INABILITY TO USE THE MSN WEB SITES OR RELATED SERVICES, THE PROVISION OF OR FAILURE TO PROVIDE SERVICES, OR FOR ANY INFORMATION, SOFTWARE, PRODUCTS, SERVICES AND RELATED GRAPHICS OBTAINED THROUGH THE MSN WEB SITES, OR OTHERWISE ARISING OUT OF THE USE OF THE MSN WEB SITES, WHETHER BASED ON CONTRACT, TORT, NEGLIGENCE, STRICT LIABILITY OR OTHERWISE, EVEN IF MICROSOFT OR ANY OF ITS SUPPLIERS HAS BEEN ADVISED OF THE POSSIBILITY OF DAMAGES. BECAUSE SOME STATES/JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES, THE ABOVE LIMITATION MAY NOT APPLY TO YOU. IF YOU ARE DISSATISFIED WITH ANY PORTION OF THE MSN WEB SITES, OR WITH ANY OF THESE TERMS OF USE, YOUR SOLE AND EXCLUSIVE REMEDY IS TO DISCONTINUE USING THE MSN WEB SITES. |
| 7 | +## Current Layer Version ARN |
| 8 | +When creating/updating a Lambda function you must specify a specific version of the layer. This readme will be kept up to date with the latest version available. The latest available Lambda Layer Version ARN is: |
| 9 | + |
| 10 | +**arn:aws:lambda:<region>:887080169480:layer:php71:2** |
8 | 11 |
|
9 | 12 | ### Usage
|
10 |
| -Simply configure your function as follows: |
| 13 | +#### General Usage |
| 14 | +The layer runs the PHP 7.1 [PHP webserver](http://php.net/manual/en/features.commandline.webserver.php) in /var/task, the root directory of function code packages: |
| 15 | + |
| 16 | +```sh |
| 17 | +$ php -S localhost:8000 '<handler>' |
| 18 | +``` |
11 | 19 |
|
12 |
| -1. Set the `Runtime` to `provided` |
13 |
| -1. Determine the latest version of the layer: `aws lambda list-layer-versions --layer-name arn:aws:lambda:<your region>:887080169480:layer:php71` |
14 |
| -1. Add the following Lambda Layer: `arn:aws:lambda:<your region>:887080169480:layer:php71:<latest version>` |
| 20 | +The Lambda Function Handler property specifies the location of the of the webserver router script. This script must be provided. |
15 | 21 |
|
16 |
| -If you are using AWS SAM it's even easier! Update your function: |
| 22 | +##### Extensions |
| 23 | +Extensions can be built using the lambci/lambda:build-nodejs8.10 Docker image. It is recommended that extensions be built into a Lambda Layer in /lib/php/7.1/modules. Then, in a php.ini file in the Lambda Function root directory put: |
| 24 | + |
| 25 | +```ini |
| 26 | +extension_dir=/opt/lib/php/7.1/modules |
| 27 | +extension=... |
| 28 | +``` |
| 29 | + |
| 30 | +The following extensions are built into the layer and available in /opt/lib/php/7.1/modules: |
| 31 | + |
| 32 | +``` |
| 33 | +bz2.so |
| 34 | +calendar.so |
| 35 | +ctype.so |
| 36 | +curl.so |
| 37 | +dom.so |
| 38 | +exif.so |
| 39 | +fileinfo.so |
| 40 | +ftp.so |
| 41 | +gettext.so |
| 42 | +iconv.so |
| 43 | +json.so |
| 44 | +phar.so |
| 45 | +posix.so |
| 46 | +shmop.so |
| 47 | +simplexml.so |
| 48 | +sockets.so |
| 49 | +sysvmsg.so |
| 50 | +sysvsem.so |
| 51 | +sysvshm.so |
| 52 | +tokenizer.so |
| 53 | +wddx.so |
| 54 | +xml.so |
| 55 | +xmlreader.so |
| 56 | +xmlwriter.so |
| 57 | +xsl.so |
| 58 | +zip.so |
| 59 | +``` |
| 60 | + |
| 61 | +#### Example |
| 62 | +Let's create an AWS SAM PHP application. We suggest using [Stackery](https://stackery.io) to make this super simple. It automates all the scaffolding shown below. But you may also choose to roll your own application from scratch. |
| 63 | + |
| 64 | +First, install [AWS SAM CLI](https://github.com/awslabs/aws-sam-cli). Make sure to create a SAM deployment bucket as shown in [Packaging your application](https://github.com/awslabs/aws-sam-cli/blob/develop/docs/deploying_serverless_applications.rst#packaging-your-application) |
| 65 | + |
| 66 | +Next, create a basic SAM application: |
| 67 | + |
| 68 | +```sh |
| 69 | +$ mkdir my-php-app |
| 70 | +$ cd my-php-app |
| 71 | +``` |
| 72 | + |
| 73 | +Create a template.yaml file with the following SAM infrastructure: |
17 | 74 |
|
18 | 75 | ```yaml
|
| 76 | +AWSTemplateFormatVersion: 2010-09-09 |
| 77 | +Description: My PHP Application |
| 78 | +Transform: AWS::Serverless-2016-10-31 |
19 | 79 | Resources:
|
20 |
| - MyFunction: |
| 80 | + phpserver: |
21 | 81 | Type: AWS::Serverless::Function
|
22 | 82 | Properties:
|
23 |
| - ... |
| 83 | + FunctionName: !Sub ${AWS::StackName}-phpserver |
| 84 | + Description: PHP Webserver |
| 85 | + CodeUri: src/server |
24 | 86 | Runtime: provided
|
| 87 | + Handler: router.php |
| 88 | + MemorySize: 3008 |
| 89 | + Timeout: 30 |
| 90 | + Tracing: Active |
25 | 91 | Layers:
|
26 |
| - - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71 |
| 92 | + - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php71:2 |
| 93 | + Events: |
| 94 | + api: |
| 95 | + Type: Api |
| 96 | + Properties: |
| 97 | + Path: /{proxy+} |
| 98 | + Method: ANY |
27 | 99 | ```
|
28 | 100 |
|
29 |
| -### Development |
| 101 | +Now create the PHP application in `src/server`. We're going to create a webserver router script first. Put the following in `router.php`: |
30 | 102 |
|
31 |
| -The layer is built by: |
| 103 | +```php |
| 104 | +<?php |
| 105 | +if (is_file($_SERVER['DOCUMENT_ROOT'].'/'.$_SERVER['SCRIPT_NAME'])) { |
| 106 | + // Serve static files directly |
| 107 | + return false; |
| 108 | +} |
| 109 | +
|
| 110 | +// Run index.php for all requests |
| 111 | +$_SERVER['SCRIPT_NAME'] = '/index.php'; |
| 112 | +require 'index.php'; |
| 113 | +?> |
| 114 | +``` |
| 115 | + |
| 116 | +Lastly, let's write our script. Put this in `index.php`: |
| 117 | + |
| 118 | +```php |
| 119 | +Hello World! You've reached <?php print($_SERVER['REQUEST_URI']); ?> |
| 120 | +``` |
| 121 | + |
| 122 | +You should now have a directory structure like: |
| 123 | + |
| 124 | +``` |
| 125 | +. |
| 126 | +├── template.yaml |
| 127 | +└── src |
| 128 | + └── php |
| 129 | + ├── index.php |
| 130 | + └── router.php |
| 131 | +``` |
| 132 | + |
| 133 | +We're ready to deploy! Run the following commands: |
| 134 | + |
| 135 | +```sh |
| 136 | +$ sam package \ |
| 137 | + --template-file template.yaml \ |
| 138 | + --output-template-file serverless-output.yaml \ |
| 139 | + --s3-bucket <your SAM deployment bucket created above> |
| 140 | +
|
| 141 | +$ sam deploy \ |
| 142 | + --template-file serverless-output.yaml \ |
| 143 | + --stack-name my-first-serverless-php-service \ |
| 144 | + --capabilities CAPABILITY_IAM |
| 145 | +``` |
| 146 | + |
| 147 | +### Development |
| 148 | +Build the layer by: |
32 | 149 |
|
33 | 150 | 1. Installing a Docker environment
|
34 |
| -1. Running `make` in the layers directory |
| 151 | +1. Running `make` |
| 152 | + |
| 153 | +This will launch a Docker container that will build php71.zip. |
| 154 | + |
| 155 | +### Disclaimer |
35 | 156 |
|
36 |
| -This will launch a Docker container that will build php71.zip. |
| 157 | +> THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND |
| 158 | +> ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 159 | +> THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 160 | +> PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP |
| 161 | +> DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 162 | +> INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 163 | +> (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 164 | +> SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 165 | +> HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 166 | +> STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 167 | +> ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 168 | +> OF THE POSSIBILITY OF SUCH DAMAGE. |
0 commit comments