PHP in Amazon Lambda

Clone or Download this GitHub Repo
https://github.com/svnlabs/aws-lambda-php-template

Make new folder lambda in current working directory of EC2 server
# mkdir lambda

Upload 2 files from zip to Amazon EC2 server Instance in folder lambda

lambda
– index.js
– php (binary)

index.js

'use strict';
exports.handler = function(event, context, callback) {
var exec = require('child_process').exec;

  exec('./php -v', function (error, stdout, stderr) {
    if(stdout){
      console.log('stdout: ' + stdout);
    }
    if(stderr){
      console.log('stderr: ' + stderr);
    }
    if (error !== null) {
      console.log('Exec error: ' + error);
    }
    context.succeed(stdout);
    console.log(stdout);
  });
};

Go inside folder lambda

# cd lambda
# zip -r ../lambda.zip *

Copy lambda.zip for AWS S3 bucket

# aws s3 cp ../lambda.zip s3://bucket-name/

How to install AWS CLI?

Create new Lambda Function from AWS Console with NodeJS Runtime*

Name*: phpv
Runtime*: Node.js 4.3
Code entry type: Upload a file from Amazon S3
S3 link URL*: https://s3.amazonaws.com/bucket-name/lambda.zip

Handler*: index.handler
Role*: Lambda Basic Execution

All other info will be default**

Save & Test

“PHP 5.6.5 (cli) (built: Jul 21 2015 18:56:08) \nCopyright (c) 1997-2014 The PHP Group\nZend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies\n”

Published by

Sandeep Verma

I’m an Entrepreneur. I’m proud to work as Blogger, LAMP Programmer, Linux Admin, Web Consultant, Cloud Manager, Apps Developer, Searcher. Concentrate > Observe > Imagine > Launch