Easy and Fast way to implement AWS Lambda service

MicroPyramid
2 min readNov 9, 2017

Create lambda function and triggering actions is time taking and involves repetitive steps. We are going to use a simple application called Gordon.

Gordon creates CloudFormation templates based on our settings files, cloudformation is a configuration management tool for Amazon Web Services similar to chef, puppet, ansible etc.. but for AWS.

Gordon isolates all the processes, like it creates separate roles, separate bucket names etc..which enhance security and doesn’t mess up with other services already running.

Using gordon we can trigger our lambda function for the following services:

  1. API Gateway
  2. Scheduled CloudWatch Events
  3. CloudWatch Events
  4. Dynamodb Streams
  5. Kinesis Streams
  6. S3

Gordon Architecture:

Gordon consist of project and project apps, project consists of project apps and project wide settings file.

Each app has a lambda module which consists of lambda function and setting file which override project wide settings.

The following example will walkthrough creating lambda function which is triggered when an object is created in S3.

Setup Environment and Install Gordon:

mkdir gordon
cd gordon
virtualenv genv
source genv/bin/activate
pip install gordon

Create Project:

Gordon startproject oscar_wilde

This will create a directory with name oscar_wilde and a settings.yml file

oscar_wilde
└── settings.yml

Create Application:

Goto oscar_wilde project directory and run:

gordon startapp astrophe

This will create astrophe directory with settings.yml, helloworld directory which contains code.py.

Oscar_wilde
settings.yml
astrophe
helloworld
code.py
settings.yml

You can rename helloworld directory to whatever name you desire but remember to change it in astrophe settings.yml file too.

S3 Integration:

Edit the oscar_wilde/settings.yml file with following content: — -

project: oscar_wilde
default-region: us-east-1
code-bucket: gordon-s3-0do9a8d
apps:
- gordon.contrib.helpers
- gordon.contrib.lambdas
- gordon.contrib.s3
- astrophe
s3:
trigger_on_object_creation:
bucket: us-east-1-oscarwilde
notifications:
run_lambda_on_object_create:
lambda: astrophe.s3consumer
events:
- s3:ObjectCreated:*

We have included our app astrophe in apps and set to trigger our lambda function when an object is created to us-east-1-oscarwilde bucket in us-east-1 region.

Lambda Handler Code:

By default the handler just prints the output when the event happens, in our case when an object is created in S3 bucket.

#astrophe/helloworld/code.py file contents.  import json
def handler(event, context):
data = "Hello World!"
print(data)
print("Received Event: " + json.dumps(event, indent=2))

Building CloudFormation Templates:

Configuring yaml files with appropriate actions is our part, building cloudformation templates is automatically done by gordon. Goto project root directory and run:

gordon build

This will create _build directory with contents:

0001_p.json         # creates s3 bucket and upload lambdas.
0002_pr_r.json # custom template.
0003_r.json # creates event sources.
Code # zipped files of our lambda code.
Contrib_lambdas_version.zip
firstapp_helloworld.zip

Deploy the Project:

Goto project root directory and run:

gordon apply

This will take a while, at the end you will have lambda handler that gets executed whenever you upload a file to S3 bucket.

The article was originally published at MicroPyramid blog

--

--

MicroPyramid

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company