Serverless and scheduled lambda function

TL;DR

Continuing on from my previous post, I decided to try out serverless to build my scheduled lambda function.

Serverless is an open-source, application framework to easily build serverless architectures on AWS Lambda.

The Serverless Framework is the world’s leading development framework for building serverless architectures.

Australia public calendar custom alexa skill

Amazon Alexa

You can find the skill on the amazon marketplace here

Quick recap

There is the pattern leveraging calendar files where the data is relatively static, and it’s more important to organize it in such a manner that allows navigation through voice commands. There's an ongoing process that can refresh it over time. Going deeper, let's explore the following utterance.

“Alexa, ask Australia Calendar to find next public holiday by state Victoria”

The dialog with this question will be determining what calendar file to get. In this example, the data is static (there aren't new holidays being created every day), and the interaction will be around navigating a list of calendar events.

In this use case, we can invoke the API ahead of an individual user request, and organize and cache the data in s3 bucket. This improves performance, and simplifies the runtime model. Here’s a view of how this looks using the websites calendar files and how the data is staged.

An S3 bucket is used to store the data, and is persisted in a ical file object. Given the durability of S3, this ensures that the data is always accessible at runtime for the skill and we don't have to hammer the website again and again for what the types of calendar events are in each state.

Scheduled lambda function

Below I will define the steps needed to build my scheduled function using serverless.

Installation

Install serverless using npm
npm install serverless -g

The current version is 1.4.0

Now create a new node.js service via the command below:
serverless create --template aws-nodejs

Here you have defined the runtime to be nodejs. Now for my function I want it to run every 7 days and to put the ical files onto s3 bucket.

You will see in the serverless.yml I have defined the schedule and resource access.

# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
#    docs.serverless.com
#
# Happy Coding!

service: aus-calendar-cron # NOTE: update this with your service name

provider:  
  name: aws
  runtime: nodejs4.3

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


# you can define service wide environment variables here
environment:  
   BUCKET: slee-calendar

functions:  
  cron:
    handler: handler.hello

#    The following are a few example events you can configure
#    NOTE: Please make sure to change your handler code to work with those events
#    Check the event documentation for details
    events:
     - schedule: rate(7 days)


#    Define function environment variables here
    environment:
       BUCKET: slee-calendar

Once I defined the handler logic, all I had to do then is call:

serverless deploy --verbose

How it works

  • An AWS CloudFormation template is created from your serverless.yml.
  • If a Stack has not yet been created, then it is created with no resources except for an S3 Bucket, which will store zip files of your Function code.
  • The code of your Functions is then packaged into zip files.
  • Zip files of your Functions' code are uploaded to your Code S3 Bucket.
  • Any IAM Roles, Functions, Events and Resources are added to the AWS CloudFormation template.
  • The CloudFormation Stack is updated with the new CloudFormation template.
  • Each deployment publishes a new version for each function in your service.

To test your function out you can then run
serverless invoke --function cron --log

It allows to send event data to the function, read logs and display other important information of the function invocation.

And voilà. Thats your scheduled lambda function up and running! 😋

Stay tuned for more!!