• This repository has been archived on 08/Sep/2021
  • Stars
    star
    319
  • Rank 126,656 (Top 3 %)
  • Language
    PHP
  • License
    Other
  • Created over 5 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PHP Runtime Layer for AWS Lambda

PHP Layer For AWS Lambda

Ever wanted to run PHP websites in AWS Lambda? It's your lucky day! This Lambda Runtime Layer runs the PHP 7.3/7.1 webserver in response to AWS API Gateway or AWS Application Load Balancer requests.

And, if you're looking for a great way to build serverless apps of all kinds, be sure to check out Stackery!

This is an early iteration of the PHP runtime Layer which is not yet ready for production. Please feel free to use this Layer to learn about the Lambda Layers feature and begin experimenting with PHP functions. We welcome feedback and stay tuned for the production-ready version coming soon.

Current Layer Version ARN

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 ARNs for PHP 7.3 and 7.1 are:

arn:aws:lambda:<region>:887080169480:layer:php73:3

arn:aws:lambda:<region>:887080169480:layer:php71:10

See Releases for release notes.

Usage

General Usage

The layer runs the PHP 7.* PHP webserver in /var/task, the root directory of function code packages:

$ php -S localhost:8000 '<handler>'

The Lambda Function Handler property specifies the location of the the script executed in response to an incoming API Gateway or Application Load Balancer request.

Configuration Files

There are three locations where PHP configuration may be located:

  • Files in layer code packages located under /etc/php-${PHP_VERSION}.d/
  • Files in function code package located under /php-${PHP_VERSION}.d/
  • php.ini located at the root of the function code package

Replace ${PHP_VERSION} with '7.3', or '7.1' according to your preferred runtime.

Extensions

The following extensions are built into the layer and available in /opt/lib/php/${PHP_VERSION}/modules:

PHP 7.3 Layer:

bz2.so
calendar.so
ctype.so
curl.so
dom.so
exif.so
fileinfo.so
ftp.so
gettext.so
iconv.so
json.so
mbstring.so
mysqli.so
mysqlnd.so
pdo_mysql.so
pdo_pgsql.so
pdo.so
pdo_sqlite.so
pgsql.so
phar.so
posix.so
shmop.so
simplexml.so
sockets.so
sqlite3.so
sysvmsg.so
sysvsem.so
sysvshm.so
tokenizer.so
wddx.so
xmlreader.so
xml.so
xmlwriter.so
xsl.so

PHP 7.1 Layer:

bz2.so
calendar.so
ctype.so
curl.so
dom.so
exif.so
fileinfo.so
ftp.so
gettext.so
iconv.so
json.so
phar.so
posix.so
shmop.so
simplexml.so
sockets.so
sysvmsg.so
sysvsem.so
sysvshm.so
tokenizer.so
wddx.so
xml.so
xmlreader.so
xmlwriter.so
xsl.so
zip.so

These extensions are not loaded by default. You must add the extension to a php.ini file to use it:

extension=json.so

Extensions can be built using the lambci/lambda:build-nodejs8.10 Docker image. It is recommended that custom extensions be provided by a separate Lambda Layer with the extension .so files placed in /lib/php/${PHP_VERSION}/modules/ so they can be loaded alongside the built-in extensions listed above.

SAM Example

Let's create an AWS SAM PHP application. We suggest using Stackery to make this super simple. It automates all the scaffolding shown below. But you may also choose to roll your own application from scratch.

First, install AWS SAM CLI. Make sure to create a SAM deployment bucket as shown in Packaging your application

Next, create a basic SAM application:

$ mkdir my-php-app
$ cd my-php-app

Create a template.yaml file with the following SAM infrastructure:

AWSTemplateFormatVersion: 2010-09-09
Description: My PHP Application
Transform: AWS::Serverless-2016-10-31
Resources:
  phpserver:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Sub ${AWS::StackName}-phpserver
      Description: PHP Webserver
      CodeUri: src/php
      Runtime: provided
      Handler: index.php
      MemorySize: 3008
      Timeout: 30
      Tracing: Active
      Layers:
        - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php73:3
      Events:
        api:
          Type: Api
          Properties:
            Path: /{proxy+}
            Method: ANY

Lastly, let's write our script. Put this in index.php:

Hello World! You've reached <?php print($_SERVER['REQUEST_URI']); ?>

You should now have a directory structure like:

.
├── template.yaml
└── src
    └── php
        └── index.php

We're ready to deploy! Run the following commands:

$ sam package \
    --template-file template.yaml \
    --output-template-file serverless-output.yaml \
    --s3-bucket <your SAM deployment bucket created above>

$ sam deploy \
    --template-file serverless-output.yaml \
    --stack-name my-first-serverless-php-service \
    --capabilities CAPABILITY_IAM

Development

Build the layers by:

  1. Installing a Docker environment
  2. Running make

This will launch Docker containers that will build php73.zip and php71.zip.

If you are behind a proxy server, just set the environment variable http_proxy before invoking make, eg.:

	$ export http_proxy=http://myproxy.acme.com:8080
	$ make php73.zip

Debugging Layer Builds

Run:

	$ docker run --rm -it -v `pwd`:/opt/layer lambci/lambda:build-nodejs8.10 /bin/bash

If you are on Windows, run this instead:

	> docker run --rm -it -v %cd%:/opt/layer lambci/lambda:build-nodejs8.10 /bin/bash

then manually execute the commands in the build.sh file.

Disclaimer

THIS SOFTWARE IS PROVIDED BY THE PHP DEVELOPMENT TEAM ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE PHP DEVELOPMENT TEAM OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

More Repositories

1

wild-rydes-workshop

Deploy a serverless ride hailing service using Lambda, Cognito, S3 & DynamoDB backend services
HTML
30
star
2

sam-crud-demo

JavaScript
15
star
3

s3-signed-urls-example

Example Stackery stack demonstrating the usage of signed S3 URLs for accessing binary files
JavaScript
11
star
4

knex-aurora-data-api-mysql

Knex.js driver for AWS Aurora Serverless Data API
JavaScript
9
star
5

demo-url-shortener

JavaScript
5
star
6

sss-local-debugging

Serverless Summer School Local Debugging Workshop
JavaScript
4
star
7

newsletter-signup-example

AWS Lambda newsletter signup form application
JavaScript
3
star
8

sam-crud-demo-java8

Java
3
star
9

language-translator

A serverless application to translate text from one language to another.
JavaScript
3
star
10

hello-world-python

HTML
3
star
11

sam-crud-demo-python37

Python
3
star
12

serverless-webhooks

This is the repository for the Serverless Webhooks Tutorial from Stackery.
JavaScript
3
star
13

hapi-serverless-example

Example of a hapi.js API service running in a serverless Stackery stack
JavaScript
3
star
14

ml-image-recognition

Python
3
star
15

quickstart-python

Stackery Quickstart Python
Python
3
star
16

demo-api-integrations

2
star
17

quickstart-dotnet

Stackery Quickstart .NET
C#
2
star
18

sam-image-processing-demo

JavaScript
2
star
19

stackery-quickstart

Stackery Quickstart
HTML
2
star
20

empty-sam-stack

2
star
21

demo-appsync-stock-trades

Demo AWS AppSync app built with Stackery
2
star
22

node8-serverless

JavaScript
1
star
23

node10-sam

JavaScript
1
star
24

xflags-quicktick

JavaScript
1
star
25

sam-image-processing-demo-java8

Java
1
star
26

sam-image-processing-demo-java

Stackery Image Processing Demo - Java
Java
1
star
27

python37-serverless

Python
1
star
28

hello-world-java11

Stackery Hello World - Java
HTML
1
star
29

quickstart-nodejs

Stackery Quickstart NodeJS
JavaScript
1
star
30

sam-crud-demo-dotnet

C#
1
star
31

quickstart-golang

Stackery Quickstart Go
Go
1
star
32

quickstart-frontend-nodejs

JavaScript
1
star
33

python37-sam

Python
1
star
34

lerna-example

JavaScript
1
star
35

sam-image-processing-demo-python

Stackery Image Processing Demo - Python
Python
1
star
36

react-front-end-with-rest-api

HTML
1
star
37

stackery-node-stub

Stub for Stackery Node.js functionality used outside of a Stackery deployment
JavaScript
1
star
38

hello-world-java8

HTML
1
star
39

sam-crud-demo-ruby

Ruby
1
star
40

demo-multi-api-monorepo

JavaScript
1
star
41

python3-serverless

Python
1
star
42

image-processing-example

JavaScript
1
star
43

sam-crud-demo-java

Java
1
star
44

livestream-event-fork

JavaScript
1
star
45

node10-serverless

JavaScript
1
star
46

ssm-layers-deployhooks

JavaScript
1
star
47

python2-serverless

Python
1
star
48

dotnet-serverless

C#
1
star
49

sss-wild-rydes

HTML
1
star
50

sam-crud-demo-python

Stackery CRUD Demo - Python
Python
1
star
51

quickstart-java

Stackery Quickstart Java
Java
1
star
52

hello-world-node

HTML
1
star
53

install-cli

hosted script to install stackery cli
Shell
1
star
54

node8-sam-advanced

JavaScript
1
star
55

alexa-reinvent-quiz

An Alexa skill to test your AWS knowledge
JavaScript
1
star