It's a fun thing to programing with API Gateway & Function Compute.
You can install it as a command line tool via NPM/yarn/CNPM
$ npm install @alicloud/fun
The fun tool use faas.yml
to describe the API Gateway & Function Compute things, then publish it online.
The fun tool think a folder with faas.yml
as a Serverless project. faas.yml
describe these things:
- credentials
- role
- function compute
- api gateway
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>'
Give API Gateway right to access Function Computed. Optional.
# faas.yml
role:
name: 'apigatewayAccessFC'
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'
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'
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
See relationship of API Gateway & Function Compute:
The MIT License