Giter VIP home page Giter VIP logo

php-examples-for-aws-lambda's Introduction

The Serverless LAMP stack

The Serverless LAMP stack

Examples

  • 0.1-SimplePhpFunction a very simple implementation of a PHP Lambda function. This uses a custom rumtime bootstrap and vendor dependencies as layers.

Community Curated PHP / Serverless resources

serverless-php-resources.md - A community curated list of PHP / Serverless resources (videos, blogs, code examples frameworks, events).

Creating your custom PHP runtime

Follow the instructions below to create Lambda layers to hold your PHP custom runtime and library dependencies. Include these layers in your PHP Lambda functions with the Lambda runtime set to provided.

Compiling PHP

ℹ️ PHP 7.3.0 has been used for this example.

To create a custom runtime, you must first compile the required version of PHP in an Amazon Linux environment compatible with the Lambda execution environment.

An easy way to accomplish this is using Cloud9 on Amazon linux.

Compile PHP by running the following commands:

# Update packages and install needed compilation dependencies
sudo yum update -y
sudo yum install autoconf bison gcc gcc-c++ libcurl-devel libxml2-devel -y

# Compile OpenSSL v1.0.1 from source, as Amazon Linux uses a newer version than the Lambda Execution Environment, which
# would otherwise produce an incompatible binary.
curl -sL http://www.openssl.org/source/openssl-1.0.1k.tar.gz | tar -xvz
cd openssl-1.0.1k
./config && make && sudo make install
cd ~

# Download the PHP 7.3.0 source
mkdir -p ~/environment/php-7-bin
curl -sL https://github.com/php/php-src/archive/php-7.3.0.tar.gz | tar -xvz
cd php-src-php-7.3.0

# Compile PHP 7.3.0 with OpenSSL 1.0.1 support, and install to /home/ec2-user/php-7-bin
./buildconf --force
./configure --prefix=/home/ec2-user/environment/php-7-bin/ --with-openssl=/usr/local/ssl --with-curl --with-zlib
make install

Packaging PHP with the Bootstrap file

⚠️ This custom runtime bootstrap file is for demonstration purposes only. It does not contain any error handling or abstractions.

  1. Download this example bootstrap file and save it together with the complied php binary into the following directory structure:
+--runtime
|  |-- bootstrap*
|  +-- bin/
|  |   +-- php*
cd /home/ec2-user/environment/php-7-bin
wget https://raw.githubusercontent.com/aws-samples/php-examples-for-aws-lambda/master/bootstrap
# make executable
chmod +x bootstrap
  1. Package the PHP binary and bootstrap file together into a file named runtime.zip:
zip -r runtime.zip bin bootstrap

ℹ️ Consult the Runtime API documentation as you build your own production custom runtimes to ensure that you’re handling all eventualities as gracefully as possible.

Creating dependencies

This bootstrap file uses Guzzle, a popular PHP HTTP client, to make requests to the custom runtime API. The Guzzle package is installed using Composer package manager.

In an Amazon Linux environment compatible with the Lambda execution environment

  1. Install Composer:
curl -sS https://getcomposer.org/installer | ./bin/php
  1. Install Guzzle (and any additional libraries you require):
./bin/php composer.phar require guzzlehttp/guzzle
  1. Package the dependencies into a vendor.zip binary
zip -r vendor.zip vendor/

Publish to Lambda layers

  1. Use the AWS Command Line Interface (CLI) to publish layers from the binaries created earlier.
aws lambda publish-layer-version \
    --layer-name PHP-example-runtime \
    --zip-file fileb://runtime.zip \
    --region eu-west-1
aws lambda publish-layer-version \
    --layer-name PHP-example-vendor \
    --zip-file fileb://vendor.zip \
    --region eu-west-1
  1. Make note of each command’s LayerVersionArn output value (for example arn:aws:lambda:eu-west-1:XXXXXXXXXXXX:layer:PHP-example-runtime:1). You will use this to add the Layers to your PHP Lambda functions.

Resources

AWS Blog posts & documentation

Open source Lambda layers

Issue Reporting

If you have found a bug or if you have a feature request, please report them at this repository issues section.

License

This project is licensed under the MIT license. See the LICENSE file for more info.

php-examples-for-aws-lambda's People

Contributors

adaamz avatar amazon-auto avatar f3l1x avatar kukielp avatar mnapoli avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.