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”

Amazon DynamoDB Formatted JSON using JQ

Linux sed is useful tool to format and transform plain text. But sed is not so useful for structured data like JSON. JQ is a sed-like tool easily deal with JSON.

./jq

is a lightweight and flexible command-line JSON processor.

Install jq on OS X:
brew install jq

Install jq on Ubuntu:
apt-get install jq

Others:
https://stedolan.github.io/jq/download/

UserPoints.json data exported from Amazon DynamoDB

# AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX /usr/local/bin/aws dynamodb scan –table-name UserPoints –region us-east-1 –output json > UserPoints.json

# jq “[.Items[] | { userId: .userId.S, CompanyCode: .CompanyCode.S, date: .date.S, points: .points.S, Id: .Id.S }]” UserPoints.json

It will output like this…

[
{
“userId”: “user1@gmail.com”,
“CompanyCode”: “ABC107”,
“date”: “2017/04/08”,
“point”: “16”,
“Id”: “1496238999”
},
{
“userId”: “user2@gmail.com”,
“CompanyCode”: “ABC107”,
“date”: “2017/02/07”,
“point”: “45”,
“Id”: “1496239218”
}
]

Amazon DynamoDB PHP CRUD using aws.phar

DynamoDB is fully managed, fast and low cost NoSQL database service by Amazon. DynamoDB allows you to offload the administrative operations and scale highly distributed database cluster.

The AWS SDK PHP enables you to use Amazon Web Services like Amazon S3, EC2, RDS, CloudFront, Redshift, DynamoDB, Push Notification Service (SNS) and more from your PHP code project.

AWS.PHAR Now Download aws-php-sdk Packaged Phar from http://pear.amazonwebservices.com/get/aws.phar

# wget http://pear.amazonwebservices.com/get/aws.phar

You can also download the packaged phar here http://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.phar

Include the SDK in your PHP project require ‘/path/to/aws.phar’;

Now download simple dynamodb-php-wrapper from here https://github.com/masayuki0812/dynamodb-php-wrapper/blob/master/DynamoDBWrapper.php

<?php

require_once 'aws.phar';
require_once 'DynamoDBWrapper.php'; 

$ddb = new DynamoDBWrapper(array(
    'key'    => 'XXXXXXXXXXXXXXXXXXXXX',
    'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'region' => 'us-east-1'
));

createTable

$result = $ddb->createTable(‘Session’, ‘UserId::N’);
// Create a table ‘Session’, which has a hash key ‘UserId’ as NUMBER.

deleteTable

$result = $ddb->deleteTable(‘Session’);

putItem

$item = array(
‘UserId’ => 1,
‘Name’ => ‘Sandeep’,
);
$result = $ddb->put(‘Session’, $item);

updateItem

$key = array(
‘UserId’ => 1,
);
$update = array(
‘Name’ => array(‘PUT’, ‘SVNLabs’)
);
$result = $ddb->update(‘Session’, $key, $update);

deleteItem

$key = array(
‘UserId’ => 1,
);
$result = $ddb->delete(‘Session’, $key);

Query

$keyConditions = array(
‘UserId’ => 1,
);
$result = $ddb->query(‘Session’, $keyConditions);

Scan

$filter = null;

$result = $ddb->scan(‘Session’, $filter, 1000);

Scan with between date filter

$filter = array(
‘userType’ => “member”,
“date” => array(“BETWEEN”, array(“2016/08/01″,”2016/08/17”))
);
$result = $ddb->scan(“Session”, $filter, 1000);

Other Links

https://docs.aws.amazon.com/aws-sdk-php/v3/guide/getting-started/installation.html
https://github.com/masayuki0812/dynamodb-php-wrapper/wiki
https://github.com/svnlabs/aws-dynamodb-php