Friday, 11 November 2016

AWS LAMBDA FUNCTION EXAMPLES WITH AWS S3

AWS LAMBDA EXAMPLES



Example 1: Basic Lambda function for printing hello-world
from __future__ import print_function

import json

print('Loading function')

def lambda_handler(event, context):
print("value1 = " + event['key1'])
return event['key2'] # Echo back the first key value

Description:
Lambda function syntax:
def lambda_handler(event, context):
return “xyz”

event input to above function is in Json format

{
"key3": "value3",
"key2": "how r u",
"key1": "Hello world"
}

in above program event['key1'] is printed, i.e Hello world. This is displayed in Logs.
Program returns event['key2'] which will return “how r u”.


HYPERSCAN GOLANG LIBRARY FOR REGEX OPERATIONS


Installing Hyperscan Library (Linux machine)

Quick guide for Installing Hyperscan:

step 1:Clone Hyperscan
cd
git
clone git://github/01org/hyperscan

step
2:Install all dependencies that are required

Dependencies List

1:Cmake >=2.8.11

2:Ragel 6.9

3:Python 2.7

4:
Boost >=1.57
(
Headers are required)

5:
Pcap (optional) >=0.8

Installing Cmake:

Download
cmake-3.0.0.tar.gz
run
the following commands from terminal tar 
-zxvf
cmake-3.0.0.tar.gz
cd cmake-3.0.0
./bootstrap
gmake
sudo
gmake install


Installing
Ragel:

Run
the following commands from terminal
cd
curl
-O
http://www.colm.net/files/ragel/ragel-6.9.tar.gz
tar
zxf ragel-6.9.tar.gz
cd
ragel-6.9
./configure
make
make
install


Installing
python:
sudo
yum install python


Getting
BOOST library
wget
http://sourceforge.net/projects/boost/files/boost/1.57.0/boost_1_57_0.tar.gz


tar
-xvf boost_1_57_0.tar.gz 
ln
-s boost_1_57_0/boost /include/boost









STEP
3: Configure Build Library:
create
a dir for Hyperscan build
then,
cd
~/cmake-3.0.0/bin/cmake
[-G Unix
Makefiles]
[options]  
 (or)

~/cmake-3.0.0/bin/cmake
~/<hyperscan-source-path>
cmake
--build . 
bin/unit-hyperscan


STEP
4:
install
GOLANG from following link
https://golang.org/doc/install
set
GOPATH,BINPATH,ROOTPATH

STEP
5:
clone
gohs in golang src folder.
Clone
gohs from github.
git
clone https://github.com/flier/gohs.git

STEP
6:
clone
hashicorp/go-multierror in same directory were you cloned gohs
git
clone https://github.com/hashicorp/go-multierror

STEP
7:(if errors occurs while doing step 7 then vist step 8)
run
command on terminal
 go
build $GOPATH/src/gohs/examples/simplegrep/main.go 
$GOPATH/bin/main
  

example
1: file sample.txt, pattern int
$GOPATH/bin/main
s[a-z]i sample.txt
output:
sai
sri krishna -521 int nikhil kumar 
sai
sri krishna -521 int nikhil kumar 
example
2:
$GOPATH/bin/main
k[a-z]*r sample.txt
sai
sri krishna -521 int nikhil kumar 
sai
sri krishna -521 int nikhil kumar 


STEP
8:
if
errors occur while doing step 7 then follow step 8, there are some
possible errors that may occur and solutions in step 8.
error
1:  
gohs/hyperscan/api.go:6:2:
cannot find package "github.com/hashicorp/go-multierror" in
any of: 
 /usr/local/go/src/github.com/hashicorp/go-multierror
(from $GOROOT) 
 /home/centos/projects/src/github.com/hashicorp/go-multierror
(from $GOPATH) 

solution--->https://github.com/hashicorp/go-multierror

error
2: 
#
pkg-config --cflags libhs --static 
Package
libhs was not found in the pkg-config search path. 
Perhaps
you should add the directory containing `libhs.pc' 
to
the PKG_CONFIG_PATH environment variable 
No
package 'libhs' found 
pkg-config:
exit status 1



solution--->find
-name libhs.pc 
./build_hyperscan/libhs.pc

export
PKG_CONFIG_PATH=/home/centos/build_hyperscan

error
3:
#
gohs/hyperscan 
projects/src/gohs/hyperscan/internal.go:19:16:
fatal error: hs.h: No such file or directory 
 #include
 
                ^

compilation
terminated. 

solution----->#include

error
4:
#
gohs/hyperscan 
/usr/bin/ld:
cannot find -lhs 
collect2:
error: ld returned 1 exit status 

solution---->sudo
cp /lib/libhs.a /lib64