Skip to content

Serverless Framework #

Find similar titles

1회 업데이트 됨.

Edit
  • 최초 작성자
    Sardor
  • 최근 업데이트
    Sardor

Overview #

The Serverless Framework is a free and open-source web framework written using Node.js. Serverless is the first framework for building applications exclusively on AWS Lambda, a serverless computing platform provided by Amazon as a part of the Amazon Web Services. A Serverless app can simply be a couple of lambda functions to accomplish some tasks, or an entire back-end composed of hundreds of lambda functions.[1] Serverless currently supports Node.js and Python runtimes. Support for Java and other runtimes for AWS Lambda will be coming soon.[2] Serverless is developed by Austen Collins[2] and maintained by a full-time team.[3] It was first introduced in October 2015 under the name JAWS.[4]

Besides it is written in Node.js, it supports multiple languages (Node.js, Python, Java, and more).

Requirements #

For installing Serverless Framework, Node.js is mandatory.

Installation #

Node.js can be installed in varous OS platforms. For more information, check Node.js homepage.

For macOS users it is simple to install via Homebrew.

$ brew install nodejs
$ npm install -g serverless

Code example #

To start coding we need to generate configuration for our app. First of all, we should set access tokens to connect our cloud provider, for our case Amazon Web Services. To do this, we need to run following command:

$ serverless config credentials --provider aws -k aws_token -s aws_secret
Serverless: Setting up AWS...
Serverless: Saving your AWS profile in "~/.aws/credentials"...
Serverless: Success! Your AWS access keys were stored under the "default" profile.

We now create new service based on Python 2.7.

$ serverless create --name hello-world --template aws-python --path hello-world
Serverless: Generating boilerplate...
Serverless: Generating boilerplate in "/Users/sardor/hello-world"
 _______                             __
|   _   .-----.----.--.--.-----.----|  .-----.-----.-----.
|   |___|  -__|   _|  |  |  -__|   _|  |  -__|__ --|__ --|
|____   |_____|__|  \___/|_____|__| |__|_____|_____|_____|
|   |   |             The Serverless Application Framework
|       |                           serverless.com, v1.5.0
 -------'

Serverless: Successfully generated boilerplate for template: "aws-python"

Let's write some code.

import json

def hello(event, context):
    body = {
        "message": "Hello world",
    }

    response = {
        "statusCode": 200,
        "body": json.dumps(body)
    }

    return response

Deployment #

Deployment is easy and simple. All we need to do is following command. We deploy into dev stage.

$ serverless deploy --stage dev --function handler --verbose

Let's test our deployed function.

$ curl https://hello-world-aws-generated-execute-id.aws.amazon.com/dev/handler
{
    "message": "Hello world"
}

Log monitoring #

For monitoring our deployed function we can use following command:

$ serverless log --function handler --stage dev

References #

  1. http://justserverless.com/blog/what-is-serverless-com/
  2. http://docs.serverless.com/docs/backstory
  3. https://github.com/serverless/serverless
  4. https://www.youtube.com/watch?v=D_U6luQ6I90
  5. http://serverless.com/
0.0.1_20230725_7_v68