AWS AWS Lambda Serverless Framework

Serverless Framework and AWS Lambda Tutorial

Serverless is a framework which will allow us to deploys functions as a service( aka lambdas) in either AWS, Google or Azure cloud.
In this serverless framework tutorial we will be talking about AWS Lambda.

Benefits of Serverless

  • Infinitely Scalable
  •  Pay per pricing.
  •  You will not need to manage any infra.

If you guys want to follow along make sure you go to AWS and create a user with admin and developer access.

  • Install serverless framework by running npm install -g serverless on the command line. You will need to be a root user to run the command.
  •  Configure Serverless credentials serverless config credentials –provider aws –key –secret 
  • mkdir simple-app
  •  cd simple-app
  •  Let’s create a new app serverless create –template aws-python3 –name simple-app

Understanding serverless.yml

Name
service: simple-app

Provider will give information about the cloud provider and runtime.

provider:
name: aws
runtime: python3.6

iamRoleStatements is where we decide what resources our lambda has access to. In the example below, we are telling that lambda has access to perform get on all object in any s3 bucket.

Example:

iamRoleStatements:
    - Effect: "Allow"
       Action:
            - "s3:GetObject"
       Resource: "*"

Next section is where we define our functions in the `functions` section.

functions:
  get:
    handler: handler.get

Which defines a aws lambda function get which is located in the file handler.py with get as the method. At this point, you should be able to do serverless deploy from the project root and have the api’s deployed to AWS.

events section in the serverless.yml is where things get interesting. It’s through events we plumb together different AWS services and AWS lambda calls or any call any other services

AWS Serverless Events.

Almost, every service in AWS tends to emit events to signal any key actions that are happening within the service. Serverless framework allows us to integrate these key events with the necessary services.

How to integrate events in Serverless Framework

1) Example: How to integrate http and aws lambda

get:
  handler: handler.get
  events:
    - http:
        method: get
        path: get

You can think of your application as a set of components which work together well. In this case, we say on every http call of /get endpoint call the lambda function get and return the result back.

There are pros and cons to using lambda. Pro is its very cost effective. Cons are they might suffer from cold start issue where the first call tends to be slow and once the function is loaded we can expect to the have responses as fast as our lambda code can return.

2) AWS S3 and lambda integration.

functions:
  resize:
    handler: transformer_handler.thumb
    events:
      - s3:
          bucket: my-bucket-name-1
          event: s3:ObjectCreated:*

Let's assume you are building an image thumbnail service where user upload images to your site and you give them back a thumbnail version of it.
In a traditional way we can think of implementing the above using a worker and message queue. In serverless world they get translated to s3 events and lambda. In the above example we are creating a lambda function `resize` which will get trigged when an image is uploaded to the s3 bucket.

3) AWS SNS and lambda integration

image_resize:
   handler: resizer.image_resize_worker
   events:
     - sns:
        arn: ${self:custom.snsWorkerTopicArn}
        topicName: ${self:custom.snsTopicName}

The above section allows us to call image_resize_worker method whenever a message arrive on particular topic & topic arn.

This is one of the many posts that will follow in the AWS lambda or serverless series. Do let me know if you want me to write about anything in specific.

Here is one of the project I built using Serverless framework, AWS lambda and Python : https://imageresize.xyz

Github Repo: https://github.com/sarchak/aws-projects

About the author

Shrikar

Backend/Infrastructure Engineer by Day. iOS Developer for the rest of the time.

/* ]]> */