edit-icon download-icon

Fun

Last Updated: Oct 16, 2018

(Have) Fun with Serverless. Fun is a development tool for serverless applications. It could help you 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.

logo

Please see the Chinese documentation if needed. If you want to use the earlier syntax, see README.md.

Installation

Fun is a command line tool developed by Node.js, however it also support Python, Java, or other runtime enviroments. It can be installed by npm:

  1. $ npm install @alicloud/fun -g

A fun command is available after installed completely. Type fun in the console to print usage:

  1. $ fun -h
  2. Usage: fun [options] [command]
  3. The fun tool use template.yml to describe the API Gateway & Function Compute things, then publish it online.
  4. Options:
  5. -v, --version output the version number
  6. -h, --help output usage information
  7. Commands:
  8. config Configure the fun
  9. validate [options] Validate a fun template
  10. deploy Deploy a project to AliCloud
  11. build Build the dependencies

Usage

Before going ahead to develop, you must create a directory which contains a file named template.yml. The directory works as project root directory.

We define a series of resources in this template file. Resources that can be defined by fun can be found 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.

Example

Here is an example. First create a hello.js file in the project root directory.

  1. exports.handler = function(event, context, callback) {
  2. var response = {
  3. isBase64Encoded: false,
  4. statusCode: 200,
  5. body: 'hellow wrold'
  6. };
  7. callback(null, response);
  8. };

Then Let’s configure related services. Create a template.yml file in the project root directory:

  1. ROSTemplateFormatVersion: '2015-09-01'
  2. Transform: 'Aliyun::Serverless-2018-04-03'
  3. Resources:
  4. fc: # service name
  5. Type: 'Aliyun::Serverless::Service'
  6. Properties:
  7. Description: 'fc test'
  8. helloworld: # function name
  9. Type: 'Aliyun::Serverless::Function'
  10. Properties:
  11. Handler: helloworld.handler
  12. Runtime: nodejs8
  13. CodeUri: './'
  14. Timeout: 60
  15. HelloworldGroup: # Api Group
  16. Type: 'Aliyun::Serverless::Api'
  17. Properties:
  18. StageName: RELEASE
  19. DefinitionBody:
  20. '/': # request path
  21. get: # http method
  22. x-aliyun-apigateway-api-name: hello_get # api name
  23. x-aliyun-apigateway-fc:
  24. 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 to online.

  1. $fun deploy
  2. Waiting for service fc to be deployed...
  3. service fc deploy success
  4. Waiting for api gateway HelloworldGroup to be deployed...
  5. URL: GET http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/
  6. stage: RELEASE, deployed, version: 20180627110526681
  7. stage: PRE, undeployed
  8. stage: TEST, undeployed
  9. api gateway HelloworldGroup deploy success

Open the browser to access http://2c2c4629c42f45a1b73000dd2a8b34b2-cn-shanghai.alicloudapi.com/ to view the result.

Configuration

In addition to configuring fun with fun config, you can also configure for fun with environment variables and .env.

The way of using environment variables is simple. We briefly describe the configuration of fun through .env.

Create a file named .env in the project directory with the following content:

  1. ACCOUNT_ID=xxxxxxxx
  2. REGION=cn-shanghai
  3. ACCESS_KEY_ID=xxxxxxxxxxxx
  4. ACCESS_KEY_SECRET=xxxxxxxxxx

We recommend that add the .env into .gitignore file to prevent your account credentials be checked into code repository.

Configuring Priority

The priority of the fun configuration is decremented in the following order:

  • .env
  • environment variables
  • ~/.fcli/config.yaml

For more examples, please visit https://github.com/aliyun/fun/tree/master/examples.

Referrences

License

The MIT License

Thank you! We've received your feedback.