Quantcast
Channel: TECH.review - by TECH.insight
Viewing all articles
Browse latest Browse all 39

Serverless and Lambda

$
0
0

Serverless is a great, powerful way to quickly deploy Lambda based services

Increasingly I'm working with tools such as AWS Lambda or Azure Functions. In the most simple form they are easy to deploy and then use. Sometimes though, you want a little more: you need an S3 bucket or want API Gateway in front of the Lambda function. Enter Serverless!

The Serverless Framework allows for easy deployment of your Lambda and other services by describing your infrastructure in a serverless.yml file. You can define the API Gateway endpoints you need and which Lambdas they can point to, set up a DynamoDB or S3 bucket with permissions. You can then run 'serverless deploy' from the project root and it'll package everything up in a .serverless folder and upload to AWS, setting up a Cloudformation template and running it so you can then tear down and recreate the environment as you require. It's simple and really powerful.

Below is a very simple yaml file for Serverless. You can set the provider (Serverless runs on Azure, AWS, IBM OpenWhisk currently) and runtime for your code. In your environment, this would just read a yml file in the 'vars' folder depending on the stage. You can also run

serverless deploy --stage testing

To change the file being used from "dev" (which is set in the stage setting) to "testing" you can then exclude files from being packaged up. If you have multiple Lambda functions, you can choose to package them up individually.

At the bottom of the yaml there is an API Gateway endpoint pointing to the Lambda function.

# For full config options, check the docs:
#    docs.serverless.com
service: middleware
provider:
  name: aws
  runtime: nodejs4.3
  stage: dev
  profile: lambda
  region: us-east-1
  environment: ${file(vars/${self:custom.stage}.yml)}
custom:
  stage: "${opt:stage, self:provider.stage}"

package:
   individually: true
exclude:
  - .gitignore
  - .jshintrc
  - .npmignore

functions:
  myfunction:
    handler: functions/myfunction.handler
    events:
      - http:
          path: someendpoint
          method: post
          cors: true

A useful feature for local development is the abaility to run the Lambda function locally by using invoke local

 serverless invoke local: --function myfunction

In this short blog post I've given a quick example of how to deploy a simple Lambda & API Gateway endpoint. You can also set up a Dynamo DB instance and many other services in the yaml file. Serverless is a great, powerful way to quickly deploy Lambda based services (or Functions if using Azure) and the best part is, it'll work with your current system just by adding a yaml file.


Viewing all articles
Browse latest Browse all 39

Latest Images

Trending Articles





Latest Images