Easy and Fast way to implement AWS Lambda service

In this blog post, You will know an easy and fast way to implement AWS Lambda service. If you want any support regarding AWS lambda service then get in touch with best AWS Consulting

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:

  • API Gateway
  • Scheduled CloudWatch Events
  • CloudWatch Events
  • Dynamodb Streams
  • Kinesis Streams
  • 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 gordonclass="pretty-print"

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.

Posted On 27 January 2017 By MicroPyramid


Need any Help in your Project?Let's Talk

Latest Comments
AWS Lambda - Best Practices

After Years of Developing Lambda Scripts from creating Serverless Applications to Pipelining your Tasks, Here are the Best Practices that we follow.

Continue Reading...
Autoscaling Application with AutoScaling Groups and AWS LoadBalancer

Autoscaling Application with AutoScaling Groups and AWS LoadBalancer

Continue Reading...
How to Build and verify an application using aws codepipeline and creating custom events with lambda

How to build and verify an application using aws codepipeline and creating custom events with lambda.

Continue Reading...
AWS tips and tricks to optimize cost and performance for better ROI

Best Practices of AWS cost and Performance Optimization

Continue Reading...
How to process message queuing system by amazon SQS

How to process attributes of message queuing system by amazon SQS using Boto3

Continue Reading...
Easy and Fast way to implement AWS Lambda service

We are going to use a simple application called Gordan to prevent creating a lambda function and triggering actions which involves time taking and repetitive …

Continue Reading...
How To Send And Receive Email With Django And Amazon SES

django-ses-gateway a pluggable Django application is used for sending mails from your verified domains and verified emails. We can also use django-ses-gateway for receiving messages …

Continue Reading...
Deploy Django using CloudFormation Template

CloudFormation helps in Using JSON templates to describe the resources needed from aws. With this approach, we don't have to repeat the same manual configuration …

Continue Reading...
How to Mount S3 Bucket on Local Disk

It all starts with FUSE, FUSE is File System User Space. Operating Systems have Kernel Space and User Space. Kernel Space is where low level …

Continue Reading...
Using AWS Lambda with S3 and DynamoDB

AWS lambda is handy tool for event driven computation, here we will learn how to configure and setup lambda function so to run our function …

Continue Reading...
How to access EC2 instance even if pem file is lost

Accessing the EC2 instance even if you loose the pem file is rather easy.

1. First, create a new instance by creating new access …

Continue Reading...
Deploying Django project on Elastic Beanstalk

Here You can learn about how to setup and deploy a Django application to Amazon Web Services (AWS).

Tools/technologies used:
Python v2.7
Django v1.7
Amazon …

Continue Reading...
how to setup custom domain for amazon cloudfront

We all want our own domain name to be setup for cloud front instead of amazon default cloud front domain name. We need two things …

Continue Reading...
Paginating S3 objects using boto3

When using Boto you can only List 1000 objects per request. So to obtain all the objects in the bucket. You can use s3's paginator.

Continue Reading...
Creating Elastic Search Cluster (EC2, cloud-aws Plugin)

While handling Large amounts of data with elasticsearch, you may run out of server capacity or compute power, Forming a Elasticsearch cluster will reduce the …

Continue Reading...
Configuring and Testing Load Balancer in AWS EC2

When You have an application that is serving Huge Customer Base, so will be your Traffic. Sometimes The Application simply stops responding. We can use …

Continue Reading...
Django Hosting on Amazon EC2 with wordpress on same domain

Configuring the Wordpress as subdirectory can be tricky. In this tutorial we will Setup a Django Website alongside a wordpress blog.

Continue Reading...
Amazon SES - Handling Bounces and Complaints

In general while sending emails, we will prepare some recipient addresses as our mailing list, which are valid and our recipients want and expect our …

Continue Reading...
Amazon AWS IAM Roles and Policies

When You want to Provide access to Amazon Web Services Console or if you're planning to provide REST API Keys to your Developers of a …

Continue Reading...
CORS with Amazon S3 and CloudFront

We struggle to load fonts from CloudFront because of CORS.

CORS - Cross Origin Resource Sharing is a security measure to block macious scripts or …

Continue Reading...

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties