Follow this step by step guide to use Lambda custom runtime for running PHP code on AWS Lambda.
- Launch an EC2 instance amzn-ami-hvm-2018.03.0.20181129-x86_64-gp2
- SSH to the instance and install packages by running following commands
sudo yum update -y sudo yum install autoconf bison gcc gcc-c++ openssl-devel libcurl-devel libxml2-devel libjpeg-devel libpng-devel git -y
- Compiling PHP from source
- Download PHP 7.3 source
mkdir ~/php-7.3-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
- Compiling
./buildconf --force ./configure --prefix=/home/ec2-user/php-7.3-bin/ --with-openssl --with-openssl-dir=/usr/include/openssl --with-curl --with-zlib --with-gd --with-jpeg-dir=/usr/lib64 make install
- Download PHP 7.3 source
- Clone repository
cd ~; git clone https://github.com/kgruszowski/lambda-custom-runtime-php.git
- Copy PHP interpreter
cp ~/php-7.3-bin/bin/php ~/lambda-custom-runtime-php/bin
- Enter to working directory
- Install Composer
curl -sS http://getcomposer.org/installer | ./bin/php
- Install dependencies
./bin/php composer.phar install
- Make zip files necessary to deploy code
zip -r runtime.zip bin/ bootstrap zip -r vendor.zip vendor/ zip add_watermark.zip src/add_watermark.php
- Create IAM role and attach AWSLambdaBasicExecutionRole and S3FullAccess policies
- Configure AWS CLI tool
- Prepare layers (make note about ARN of every layer that you published, it will be needed to create lambda function)
- Custom runtime layer
aws lambda publish-layer-version --layer-name php-custom-runtime --zip-file fileb://runtime.zip
- Layer with vendor files
aws lambda publish-layer-version --layer-name php-custom-runtime-vendor --zip-file fileb://vendor.zip
- Custom runtime layer
- Create function
aws lambda create-function \ --function-name add_watermark-php \ --zip-file fileb://add_watermark.zip \ --handler add_watermark \ --role "{ARN_OF_CREATED_ROLE}" \ --runtime provided \ --layers "{ARN_OF_RUNTIME_LAYER}" "{ARN_OF_VENDOR_LAYER}"
- In Lambda Configuration Designer set environment variables public_key and secret_key (access key documentation)
- Create an S3 bucket with two folders inside (origin, watermarkedg)
- Create an S3 Trigger in Lambda Configuration Designer
- in Designer sidebar choose S3
- choose bucket you've created
- choose All object create events in Event type selector
- in Prefix field set origin/ and click Add button
- Test function
- Upload a jpg image to {YOUR_BUCKET}/origin
- See result image in {YOUR_BUCKET}/watermark
- Examine CloudWatch logs for Lambda function