(have)Fun with Serverless
Fun is a development tool for serverless applications. It can help you to efficiently arrange cloud resources such as Function Compute, API Gateway, Log Service and so on. You can use it to develop,build and deploy FC by describing relative resources in a template.yml
file.
If you want to use the old syntax, please refer to README.md.
Fun is a command line tool developed for Node.js, however it also support Python, Java or other runtime enviroments. It can be installed by npm:
$ npm install @alicloud/fun -g
A fun command is available after a successful installation. Typing fun
in the console will print usage:
$ fun
Usage: fun [options] [command]
The fun tool use template.yml to describe the API Gateway & Function Compute things, then publish it online.
Options:
-v, --version output the version number
-h, --help output usage information
Commands:
config Configure the fun
validate [options] Validate a fun template
deploy Deploy a project to AliCloud
build Build the dependencies
Before going ahead to develop, you would need to create a directory which contains a file named template.yml
. The directory will act as project root directory.
We will define a series of resources in this template file. You can find the resources available to use with fun on fun's specification document.
Before using fun
, we need to configure it first by typing fun config
and then, following the prompts, configure Account ID
, Access Key Id
, Secret Access Key
and Default Region Name
.
After the fun config
is completed, fun saves the configuration to the ~/.fcli/config.yaml
file in the user home directory.
Now you are ready to use the fun command.
Here is an example. First, create a helloworld.js
file in the project root directory:
exports.handler = function(event, context, callback) {
var response = {
isBase64Encoded: false,
statusCode: 200,
body: 'hello world'
};
callback(null, response);
};
Then, let's configure related services. Create a template.yml
file in the project root directory:
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
fc: # service name
Type: 'Aliyun::Serverless::Service'
Properties:
Description: 'fc test'
helloworld: # function name
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: helloworld.handler
Runtime: nodejs8
CodeUri: './'
Timeout: 60
HelloworldGroup: # Api Group
Type: 'Aliyun::Serverless::Api'
Properties:
StageName: RELEASE
DefinitionBody:
'/': # request path
get: # http method
x-aliyun-apigateway-api-name: hello_get # api name
x-aliyun-apigateway-fc:
arn: acs:fc:::services/${fc.Arn}/functions/${helloworld.Arn}/
After the template file and code are written, you can use the deploy command to deploy the service, function and api gateway online.
$fun deploy
Waiting for service fc to be deployed...
service fc deploy success
Waiting for api gateway HelloworldGroup to be deployed...
URL: GET http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/
stage: RELEASE, deployed, version: 20180627110526681
stage: PRE, undeployed
stage: TEST, undeployed
api gateway HelloworldGroup deploy success
Open the browser to access http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/
to view the result.
In addition to configuring fun with fun config
, you can also configure for fun with environment variables and .env
files.
The process for using environment variables is very simple. We briefly describe the configuration of fun through .env
.
Create a file named .env
in the project directory with the following content:
ACCOUNT_ID=xxxxxxxx
REGION=cn-shanghai
ACCESS_KEY_ID=xxxxxxxxxxxx
ACCESS_KEY_SECRET=xxxxxxxxxx
It is recommended to add the .env
into .gitignore
file to prevent your account credentials from being checked into code repository.
The priority of the fun configuration is decremented in the following order:
- .env
- environment variables
- ~/.fcli/config.yaml
You can find more complex examples here:
The MIT License