阿里云函数计算(Function Compute)是一个事件驱动的全托管 Serverless 计算服务,您无需管理服务器等基础设施,只需编写代码并上传,函数计算会为您准备好计算资源,并以弹性、可靠的方式运行您的代码。类似于 AWS Lambda。
安装
npm install @alicloud/fun -g
# or
brew install fun
# fun --version
3.6.20
# fun --help
Usage: fun [options] [command]
The fun command line provides a complete set of commands to define, develop,
test serverless applications locally, and deploy them to the Alibaba Cloud.
Options:
  --version       output the version number
  -v, --verbose   verbose output
  -h, --help      display help for command
Commands:
  config          Configure the fun
  init            Initialize a new fun project
  install         Install dependencies which are described in fun.yml
  build           Build the dependencies
  local           Run your serverless application locally
  edge            Run your serverless application at edge
  validate        Validate a fun template
  deploy          Deploy a fun application
  nas             Operate NAS file system
  package         Package a Function Compute application
  invoke          Remote invoke function
  help [command]  display help for command
配置
# fun config
? Aliyun Account ID 31446019
? Aliyun Access Key ID ***********Xank
? Aliyun Access Key Secret ***********fJo5
? Default region name cn-hangzhou
? The timeout in seconds for each SDK client invoking 60
? The maximum number of retries for each SDK client 3
? Allow to anonymously report usage statistics to improve the tool over time? Yes
? Use custom endpoint? No
部署
以beego框架为例:
# fun deploy -y
current folder is not a fun project.
Fun detected your application doesn't listen on '0.0.0.0:9000' in /Users/xiexianbin/work/code/go/src/github.com/xiexianbin/x-lego/main.go
Fun will replace your addr to '0.0.0.0:9000', and also backup your origin file /Users/xiexianbin/work/code/go/src/github.com/xiexianbin/x-lego/main.go to /Users/xiexianbin/work/code/go/src/github.com/xiexianbin/x-lego/main.go.bak
? Are your sure? Yes
Tips:
You must use 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"' to recompile your project every time before using fun deploy.
? Let Fun exec this command now for you? Yes
Executing command 'GOARCH=amd64 GOOS=linux go build -ldflags "-s -w"'...
Generating template.yml...
Generate Fun project successfully!
========= Fun will use 'fun deploy' to deploy your application to Function Compute! =========
(node:19760) Warning: Accessing non-existent property 'validateParameters' of module exports inside circular dependency
(Use `node --trace-warnings ...` to show where the warning was created)
using region: cn-hangzhou
using accountId: ***********6019
using accessKeyId: ***********Xank
using timeout: 60
Collecting your services information, in order to caculate devlopment changes...
Resources Changes(Beta version! Only FC resources changes will be displayed):
┌─────────────┬──────────────────────────────┬────────┬─────────────────────┐
│ Resource    │ ResourceType                 │ Action │ Property            │
├─────────────┼──────────────────────────────┼────────┼─────────────────────┤
│ x-lego      │ Aliyun::Serverless::Service  │ Add    │ Description         │
├─────────────┼──────────────────────────────┼────────┼─────────────────────┤
│             │                              │        │ Handler             │
│             │                              │        ├─────────────────────┤
│             │                              │        │ Runtime             │
│             │                              │        ├─────────────────────┤
│             │                              │        │ CodeUri             │
│ x-lego      │ Aliyun::Serverless::Function │ Add    ├─────────────────────┤
│             │                              │        │ MemorySize          │
│             │                              │        ├─────────────────────┤
│             │                              │        │ InstanceConcurrency │
│             │                              │        ├─────────────────────┤
│             │                              │        │ Timeout             │
├─────────────┼──────────────────────────────┼────────┼─────────────────────┤
│             │                              │        │ AuthType            │
│ httpTrigger │ HTTP                         │ Add    ├─────────────────────┤
│             │                              │        │ Methods             │
└─────────────┴──────────────────────────────┴────────┴─────────────────────┘
Waiting for service x-lego to be deployed...
	Waiting for function x-lego to be deployed...
		Waiting for packaging function x-lego code...
		The function x-lego has been packaged. A total of 808 files were compressed and the final size was 6.9 MB
		Waiting for HTTP trigger httpTrigger to be deployed...
		triggerName: httpTrigger
		methods: [ 'GET', 'POST', 'PUT' ]
		trigger httpTrigger deploy success
	function x-lego deploy success
service x-lego deploy success
Detect 'DomainName:Auto' of custom domain 'Domain'
Request a new temporary domain ...
The assigned temporary domain is http://37785285-31446019.test.functioncompute.com,expired at 2020-12-22 23:01:25, limited by 1000 per day.
Waiting for custom domain Domain to be deployed...
custom domain Domain deploy success
创建应用
fun init -n demo
本地调试:
cd demo
fun local invoke demo
F&Q
函数计算,http 触发,总是返回头 Content-Disposition:attachment
- 使用自定义域名即可正常返回 html 和 json 数据