Serverless Framework
    • PDF

    Serverless Framework

    • PDF

    Article Summary

    Available in Classic and VPC

    Serverless Framework is an open-source web framework that enables the development, deployment, and testing of serverless applications by integrating with various cloud provider's 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, one action performs one task.

    Events

    Events in the serverless framework are the triggers that execute 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.

    SettingDescriptionRequirement statusDefault valueRestrictions
    handlerThe file name and function name where the main function of the action is defined. <File name>.<Function name>O--
    runtimeAction runtimeO-nodejs, python only
    nameAction nameX<Service name>-<Stage>-<FUNCTION_KEY>-
    memoryAction memoryX256 (MB)128, 256, 512
    timeoutMaximum time limit for which the action can be runX60000 (ms)MIN 500 MAX 300000
    vpcVPC and subnet information associated with the actionXIf omitted, deployed as a Classic resource
    • Example
    # 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 according to 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
               ...
    

    Basic trigger

    - triggerName: basicTrg
      type: basic
      productId: <API Gateway Product ID>
      apiName: <API Gateway API Name>
      stageName: <API Gateway Stage Name>
    

    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>
    

    Cloud IoT trigger

    - triggerName: iotTrg
      type: iot
      iotLink:
        - ruleName: <Cloud IoT Core Rule Name>
    

    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 execute 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 execution returns the return value of the action code. If detailed execution results are required, you must add the verbose option. You cannot execute 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 execution 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 <search start date and time> --end <search 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
    
    Note
  • --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 attribute is defined, the Cloud Functions triggers are also deployed and connected to the action.

    $ serverless deploy
    

    Execute action

    Actions can be executed on the cloud.

    $ serverless invoke -f tuto1
    

    View action log

    You can view the execution 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 serverless.yml examples below, the following items should be replaced with the user's resource information.

    • Basic trigger, productId for generating GitHub trigger external connection address, apiName, stageName
    • GitHub access information for GitHub trigger
    • Rule name for Cloud IoT core 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:
          # basic trigger on classic platform
          - triggerName: cBasicTrg
            type: basic
            productId: "2vnk18dvh2"
            apiName: "api"
            stageName: "v2"
          # 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 iot core trigger on classic platform
          - triggerName: cCloudIotTrg
            type: iot
            iotLink:
              - ruleName: "myIoTRule"
          # 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:
            # basic trigger on vpc platform
          - triggerName: vBasicTrg
            type: basic
            productId: "2vnk18dvh2"
            apiName: "api"
            stageName: "v2"
            # 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 iot core trigger on vpc platform
          - triggerName: vCloudIotTrg
            type: iot
            iotLink:
              - ruleName: "myIoTRule"
            # 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
    

    Restrictions

    • Does not support creation of external connections to actions.
    • We provide Node.js and Python runtimes, with plans to support additional runtimes in the future.
    • Local debugging is not supported.

    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.