Serverless Framework

Prev Next

Available in Classic and VPC

Serverless Frameworkis an open-source web framework that enables the development, deployment, and testing of serverless applications by integrating with various cloud providers' function-as-a-service. NAVER Cloud's Cloud Functions support the serverless framework provider, allowing easy configuration and management of Cloud Functions utilizing the serverless framework.

Key concepts

The key concepts provided by the serverless framework and their associations with each item's Cloud Functions are as follows: For more information, see Serverless Framework Concepts.

Functions

The functions in the serverless framework are equivalent to the actions in Cloud Functions. In terms of deployment, the action is the most basic unit, and typically, 1 action performs 1 task.

Events

Events in the serverless framework are the triggers that run the functions, similar to triggers in Cloud Functions.

Services

It refers to an organizational unit or project unit in the serverless framework. In the serverless.yml file, write the contents necessary for the service such as functions and events.

Components

Functions

Defines the action information to be deployed/managed.

Settings Description Required Default Service limits
handler The file name and function name where the main function of the action is defined. <File name>.<Function name> O - -
runtime Action runtime O - Provided nodejs, python only
name Action name X <Service name>-<Stage>-<FUNCTION_KEY> -
memory Action memory X 256 (MB) 128, 256, 512, 1024
timeout Maximum time limit for which the action can be run X 60000 (ms) MIN 500 MAX 300000
vpc VPC and subnet information associated with the action X If omitted, deployed as a Classic resource
  • Examples
# serverless.yml
service: myService
 
provider:
  name: navercloud
  region: kr
 
functions:
  hello:
    handler: handler.main 
    runtime: nodejs:16
    name: myAction
    memory: 512
    timeout: 60000
    parameters:
      foo: bar
    vpc:
      vpcNo: 11
      subnetNo: 123
 
plugins:
  - serverless-navercloud

Packages

Cloud Functions provides package resources to manage actions. You can specify no package; however, if no package is defined, one will be created automatically.

functions:
  hello:
    handler: handler.main
    packageName: myPackage

If you explicitly deploy a package and specify it in an action, you must define the package to be deployed in the resource/packages. vpc: if true option is not provided, it is deployed as a Classic resource.

functions:
  hello:
    handler: handler.main
    packageName: myPackage
    runtime: nodejs:16
    name: myAction
    vpc:
      vpcNo: 11
      subnectNo: 123
    
resources:
  packages:
     myPackage:
       parameters:
         hello: world
      vpc: true

Events

Defines the trigger information to be deployed/managed. Upon deployment, the event information is converted into trigger resources. The platform of the trigger is determined depending on the platform of the action that the events are specified for.

functions:
   myAction:
     name: myAction
     handler: hadler.main
     events:
       - triggerName: myCronTrigger
          type: cron
          cronOptions: "*/5 * * * *"
       - triggerName: myBasicTrigger
           ...

Cron trigger

- triggerName: cronTrg
  type: cron
  cronOption: <Cron Expression>

GitHub trigger

- triggerName: gitTrg
  type: github
  username: <Github Username>
  accessToken: <Github Access Token>
  repository: <Github Repository>

Cloud Insight trigger

- triggerName: insightTrg
  type: insight
  insightLink:
    - prodKey: <Cloud Insight Event Rule prodKey>
      ruleGrpId: <Cloud Insgiht Event Rule ruleGrpId>
      reminderTime: <Remind Time(m)>
      enableNotiWhenEventClose: <true|false>

Object Storage trigger

- triggerName: obsTrg
  type: object_storage
  objectStorageLink:
    - bucketName: <Object Storage Bucket Name>
      eventRuleName: <Object Storage Event Rule Name>

Source Commit trigger

- triggerName: scTrg
  type: source_commit
  sourceCommitLink:
    - repositoryName: <Source Commit Repository Name>
      webhookName: <Source Commit Respository's Webhook Name>
      enable: <true | false>

How to use

Describes how to run commands such as build and deploy using the serverless framework CLI.

Package

This service configuration can be packaged as a file rather than deployed to the cloud. You can use the packaged file for version management or integrate it with CI/CD workflows. There are 3 different types of files created for each resource of Cloud Functions.

  • functions.json (action)
  • packages.json (package)
  • triggers.json (trigger)
$ serverless package --package <package path>

Deploy

When changes are made to the contents of the serverless.yml file, the changes are deployed to the cloud. By default, the package process is included, and the --package option enables you to deploy a previously created package without a separate build.

$ serverless deploy 
$ serverless deploy --package <package path>

Invoke

Activates an action deployed on the cloud. The return value of the action code is output as the run results. If detailed run results are required, you must add the verbose option. You cannot run actions that have not been deployed.

$ serverless invoke -f <action name>
$ serverless invoke -f <action name> --verbose
$ serverless invoke -f <action name> --timeout <timeout (ms)>

Logs

View action run history and results.

$ serverless logs -f <action name>
$ serverless logs -f <action name> --pageSize <number of logs per page> --pageNo <page number>
The format of # start/end should be written as "yyyy-MM-ddTHH:mm:ss" based on the standard time zone of the Region.
$ serverless logs -f <action name> --start <view start date and time> --end <view end date and time> 
$ serverless logs -f <action name> --tail

Tutorial

This tutorial introduces how to configure Cloud Functions actions and triggers in the serverless framework. For more information on each configuration element, see Components.

Environment configuration

Install serverless framework

  • Install the serverless framework package using NPM.
npm install -g serverless@3
Note
  • Serverless Framework v4 or higher is not supported.
  • --global or -g option must be included.
  • Node.js >= 16.0 is required.

Register Ncloud SDK credentials

serverless-navercloud uses ncloud-sdk. NCLOUD_ACCESS_KEY (or NCLOUD_ACCESS_KEY_ID) and NCLOUD_SECRET_KEY (or NCLOUD_SECRET_ACCESS_KEY) must be registered in environment variables.

$ export NCLOUD_ACCESS_KEY="Access key obtained from NAVER Cloud Platform portal or sub account"
$ export NCLOUD_SECRET_KEY="Secret key obtained from NAVER Cloud Platform portal or sub account"

Create service

Create a directory named sls-tutorial using the following command:

$ mkdir -p sls-tutorial
$ cd sls-tutorial

Create serverless.yml, the service configuration file. You must set the provider and plugins as follows:

Create a service configuration. You must set the provider and plugins as follows:

Note
  • (Supported Region)
    • kr (default)
    • sg
    • jp
# serverless.yml
service: sls-tutorial
 
provider:
  name: navercloud
  region: kr
 
functions:
  tuto1:
    handler: handler.tuto1
    runtime: nodejs:16
    memory: 256
    timeout: 60000
  tuto2: # tuto2 action is deployed on the VPC platform.
    handler: handler.tuto2
    runtime: nodejs:16
    memory: 256
    timeout: 60000
    vpc:
      vpcNo: 1
      subnetNo: 2
 
plugins:
  - serverless-navercloud

Write the action code to be referenced by the service.

// handler.js
'use strict';
 
function tuto1(params) {
  const name = params.name || 'World';
  console.log('log', { payload: `Hello, ${name}` });
  return { payload: `Hello, ${name}!` };
}
 
function tuto2(params) {
  return { payload: 'hello world' };
}
 
module.exports = {
  tuto1, tuto2
};

Install the serverless-navercloud plugin to configure Cloud Functions on NAVER Cloud Platform.

$ serverless plugin install -n serverless-navercloud

Deploy action

Deploy the actions defined in serverless.yml to Cloud Functions. If the functions' events property is defined, the Cloud Functions triggers are also deployed and connected to the action.

$ serverless deploy

Run action

Actions can be run on the cloud.

$ serverless invoke -f tuto1

View action log

You can view the run history and log of a specific action.

$ serverless logs -f tuto1

Detailed examples of action/trigger configuration

These are examples of registering various types of resources provided by Cloud Functions in serverless.yml.

Note

In the following serverless.yml examples, the following items should be replaced with the user's resource information:

  • GitHub access information for GitHub trigger
  • Product key and rule group ID for Cloud Insight trigger
  • BucketName and event name for Object Storage trigger
  • VPC/Subnet ID information
service: sls-tutorial

provider:
  name: navercloud
  region: kr

functions:
  cAct1:
    handler: handler.classicActionFunc1
    runtime: nodejs:16
    memory: 128
    timeout: 30000
    events:
      # cron trigger on classic platform
      - triggerName: cCronTrg
        type: cron
        cronOption: "* * 1 * *"
      # github trigger on classic platform
      - triggerName: cGitTrg
        type: github
        username: "cloudfunctions@ncloud.com"
        accessToken: "*****"
        repository: "navercloud/cloudfunctions"
        events:
          - "*"
        productId: "2vnk18dvh2"
        apiName: "api"
        stageName: "v2"
      # cloud insight trigger on classic platform
      - triggerName: cCloudInsTrg
        type: insight
        description: "cloud insight trigger on classic platform"
        insightLink:
          - prodKey: "460438474722512896"
            ruleGrpId: "749659968722046976"
            reminderTime: 500
            enableNotiWhenEventClose: false
      # object storage trigger on classic platform
      - triggerName: cObsTrg
        type: object_storage
        objectStorageLink:
          - bucketName: "myBucket"
            eventRuleName: "myEventRule"
  # sequence Action on classic platform
  cAct2:
    handler: handler.classicActionFunc2
    runtime: nodejs:8
  cSeqAct:
    sequence:
      - sls-tutorial-dev-cAct1
      - sls-tutorial-dev-cAct2
  vAct1:
    name: vAct1
    handler: handler.vpcActionFunc
    runtime: nodejs:16
    memory: 256
    timeout: 60000
    vpc:
      vpcNo: 30308
      subnetNo: 65617
    events:
        # cron trigger on vpc platform
      - triggerName: vCronTrg
        type: cron
        cronOption: "* * 1 * *"
        # github trigger on vpc platform
      - triggerName: vGitTrg
        type: github
        username: "cloudfunctions@ncloud.com"
        accessToken: "*****"
        repository: "navercloud/cloudfunctions"
        events:
          - "*"
        productId: "2vnk18dvh2"
        apiName: "api"
        stageName: "v2"
        # cloud insight trigger on vpc platform
      - triggerName: vCloudInsTrg
        type: insight
        description: "cloud insight trigger on vpc platform"
        insightLink:
          - prodKey: "460438474722512896"
            ruleGrpId: "749659968722046976"
            reminderTime: 500
            enableNotiWhenEventClose: false
        # object storage trigger on vpc platform
      - triggerName: vObsTrg
        type: object_storage
        objectStorageLink:
          - bucketName: "myBucket"
            eventRuleName: "myEventRule"
        # source commit trigger on vpc platform
      - triggerName: vScTrg
        type: source_commit
        sourceCommitLink:
          - repositoryName: "myRepo"
            webhookName: "myWebhook"
            enable: true
  vAct2:
    name: vAct2
    handler: handler.vpcActionFunc
    runtime: nodejs:12
    memory: 128
    timeout: 60000
    vpc:
      vpcNo: 30308
      subnetNo: 65617
  # sequence action on vpc platform
  vSeqAct:
    sequence:
      - vAct1
      - vAct2
    vpc:
      vpcNo: 30308
      subnetNo: 65617

plugins:
  - serverless-navercloud

Service limits

  • Serverless Framework v4 or higher is not supported.
  • We provide Node.js and Python runtimes, with plans to support additional runtimes in the future.
  • Local debugging is not supported.