Find us on facebook

Oct 29, 2016

Lambda Function Basic

Create serverless function.
serverless function create functions/Function1/func1 (This command should be given within the project folder)

Within Function1 folder it creates
func1 folder and package.json file

Within func1 folder it creates following files.
event.json
handler.js
s-function.json

In handler.js file, we can include all the functions.

First we need to get required packages as below.

var aws = require('aws-sdk');
var SqsQueueParallel = require('sqs-queue-parallel');
var sqs = new aws.SQS({"accessKeyId":process.env.ACCESS_KEY_ID, "secretAccessKey": process.env.SECRET_ACCESS_KEY, "region": process.env.REGION});

Within handler function we can include the logic we want.

module.exports.handler = function(event, context, cb) {
};

s-function.json structure will be like
{
  "name": "func1",
  "runtime": "nodejs4.3",
  "description": "Serverless Lambda function for project: XXXXX",
  "customName": "func1",
  "customRole": "arn:aws:iam::xxxxxxxxxxxx:role/role",
  "handler": "func1/handler.handler",
  "timeout": 300,
  "memorySize": 128,
  "authorizer": {},
  "custom": {
    "excludePatterns": []
  },
  "endpoints": [],
  "events": [
    {
      "name": "V1Schedule",
      "type": "schedule",
      "config": {
        "schedule": "rate(5 minutes)",
        "enabled": true
      }
    }
  ],
  "environment": {
    "SERVERLESS_PROJECT": "${project}",
    "SERVERLESS_STAGE": "${stage}",
    "SERVERLESS_REGION": "${region}",
    "ACCESS_KEY_ID": "AWSKEYHERE",
    "SECRET_ACCESS_KEY": "AWSSECRETHERE",
    "REGION": "ap-southeast-1",
    "LIMIT": "300"
  },
  "vpc": {
    "securityGroupIds": [],
    "subnetIds": []
  }
}

package.json structure will be like,
{
  "name": "LambdaFunction",
  "version": "0.0.1",
  "description": "A Serverless Project and its Serverless Plugin dependencies.",
  "author": "ishara",
  "license": "MIT",
  "private": false,
  "repository": {
    "type": "git",
    "url": "git://github.com/"
  },
  "dependencies": {
    "async-foreach": "^0.1.3",
    "mysql": "^2.11.1",
    "object-sizeof": "^1.0.10",
    "sqs-queue-parallel": "^0.1.6"
  }
}

No comments:

Post a Comment