-
Notifications
You must be signed in to change notification settings - Fork 577
Standard Function
Lellansin Huang edited this page Aug 13, 2020
·
6 revisions
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.
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:
Install dependencies.
npm install
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
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
}
}
$ f invoke -f index
Result:
--------- result start --------
"hello world"
--------- result end --------
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.