Skip to content

Standard Function

Lellansin Huang edited this page Aug 6, 2020 · 6 revisions

create a function

first of all, just type:

f create

Then, let's select faas-standard:

Generating boerplate...
? Hello, traveller.
  Which template do you like? …

 ⊙ Boilerplate
❯ faas-standard - A serverless boilerplate for aliyun fc, tencent scf and so on
  faas-layer - A serverless runtime layer boilerplate

 ⊙ Examples
  faas-react - A serverless example with react
  faas-vue - A serverless example with vue

As we see:


image.png

Install dependencies.

npm install

Directory Structure

Here is the most simplifying structure of one function. Including standard spec file f.yml and a classic TypeScript project directory.

.
├── f.yml           # standard spec file
├── package.json    # project dependencies
├── src             # resources code dir
│   └── index.ts    # function entry, demo
└── tsconfig.json   # config for tsc

Function Code

In Midway function is forming by class:

import { Func, Inject, Provide } from '@midwayjs/decorator';
import { FaaSContext, FunctionHandler } from '@midwayjs/faas';

@Provide()						// The identity for IoC container to scan
@Func('index.handler')					// Function Handler identity
export class IndexService implements FunctionHandler {

  @Inject()
  ctx: FaaSContext;  					// Function context

  async handler() {					// Function body
    return 'hello world';				// return value
  }
}

Spec File

f.yml is 是函数的定义文件,midway faas 通过这个文件,在构建时生成不同平台所能认识的文件,示例中的文件内容如下。

service:
  name: serverless-hello-world				## Service name

provider:
  name: aws						## The provider you deploy
  
functions:						## Definition of functions
  index:						   ## first function name `index`
    handler: index.handler				   ## function entry
    events:						   ## event triggers
      - http:						     ## http trigger with GET /* support
          method: get
package:						## built artifact name
  artifact: code.zip

Function Develop

Local invoke

$ f invoke -f index

Result:

--------- result start --------

"hello world"

--------- result end --------

Deploy

First check f.yml file, ensure your provider.name is aws. And the f.yml would seem like:

service: serverless-hello-world

provider:
  name: aws                           # cloud platform name

functions:
  index:                              # functionName
    handler: index.handler            # handler name
    events:                           # function events
      - http:
          method: get

package:
  artifact: code.zip

Then just go:

f deploy

Sample output:

Start package
Information
 - BaseDir: /Users/lellansinhuang/workspace/aws-hello
 - AnalyzeResult
   ◎ ProjectType: midway_faas
   ◎ MidwayRoot: .
   ◎ TSCodeRoot: src
   ◎ TSBuildTemporaryRoot: dist
   ◎ PackageRoot: .serverless
Install development dependencies...
 - Find node_modules and skip...
Copy Files to build directory...
   ◎ Copy f.yml
   ◎ Copy package-lock.json
   ◎ Copy package.json
   ◎ Copy tsconfig.json
   ◎ Copy src/index.ts
   ◎ Copy src/test.ts
 - File copy complete
Building Midway FaaS directory files...
 - Using tradition build mode
 - Build Midway FaaS complete
Generate entry file...
Install layers...
 - Layers install complete
Install production dependencies...
 - Dependencies install complete
Package artifact...
 - Artifact file code.zip
 - Zip size 1.66MB
Start deploy by aws-sdk
Check aws s3 bucket...
Start upload artifact...
  - artifact uploaded
Start stack create
  - generate stack template json
  - creating stack request
  - Stack [ms-stack-midway-hello] already exists
  - upadte function
  - upadte over
  - stack already exists, do stack update
  - generate stack template json
  - creating stack request
  - wait stack ready 
    - checking..
  - stack ready, check api url
midway-hello-index test url https://xxxx/v1/
Deploy over

Here is the test url for us to curl.