node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

@alicloud/fun

(have)Fun with Serverless

It's a fun thing to programing with API Gateway & Function Compute.

Installation

You can install it as a command line tool via NPM/yarn/CNPM

$ npm install @alicloud/fun

Usage

The fun tool use faas.yml to describe the API Gateway & Function Compute things, then publish it online.

faas.yml

The fun tool think a folder with faas.yml as a Serverless project. faas.yml describe these things:

  • credentials
  • role
  • function compute
  • api gateway

credentials

Includes account id, access key id, access key secret. It's optional. If not specificated by faas.yml, get it from environment variable.

# faas.yml 
accountid: '<account id, default is process.env.ACCOUNT_ID>'
accessKeyId: '<ACCESS KEY ID, default is process.env.ACCESS_KEY_ID>'
accessKeySecret: '<ACCESS KEY SECRET, default is process.env.ACCESS_KEY_SECRET>'

role

Give API Gateway right to access Function Computed. Optional.

# faas.yml 
role:
  name: 'apigatewayAccessFC'

Function Compute

Write your function into hello.js with:

'use strict';
 
const hook = require('fc-helper');
 
exports.index = hook((req, res) => {
  res.send('Hello world!\n');
});

The helper.js can be found at here.

Describe it in faas.yml:

# faas.yml 
function-compute:
  region: 'cn-shanghai'
  services:
    name: 'fc'
      description: 'fc test'
      functions:
        name: 'helloworld'
          description: 'hello!'
          # entry point 
          handler: hello.index
          # which files should be ziped 
          codes:
            - 'hello.js'

API Gateway

exports the functions with API Gateway:

# faas.yml 
api-gateway:
  endpoint: 'http://apigateway.cn-hangzhou.aliyuncs.com'
  groups:
    name: 'apigw_fc'
      description: 'API Gateway & Function Compute'
      apis:
        name: 'helloworld'
          method: 'GET'
          path: '/helloworld'
          function: 'cn-shanghai/fc/helloworld'

Upload & deploy

Use fun command to upload & deploy it to AliCloud:

$ fun deploy
Function compute(region):
  service service_name ok.
    function function_name ok.
API gateway(region):
    URL: POST http://<groupid>-<region>.alicloudapi.com/<the api path>
      => cn-hangzhou/service_name/function_name
      stage: RELEASE, deployed, version: 20171101125034887
      stage: PRE, deployed, version: 20171101125107780
      stage: TEST, deployed, version: 20171101144618017

Deploy to TEST & PRE stage:

$ fun deploy TEST
$ fun deploy PRE

Examples

Architect

See relationship of API Gateway & Function Compute:

Referrences

License

The MIT License