Skip to content

Standard Function

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

Installation

you need to install Node (> 10.9), and npm first.

npm install @midwayjs/faas-cli -g

Then use f -v to check if it's success.

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
  }
}

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:
  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

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.