custom示例代码:
51ab7acc-929c-40fb-8247-d50a3a60304f.zip
项目目录
-rwxrwxrwx@ 1 apple staff 123B 4 21 18:40 Funfile # 必须,依赖包
-rwxrwxrwx@ 1 apple staff 27B 4 21 14:59 bootstrap # 必须,启动脚本
-rw------- 1 apple staff 231B 4 21 18:34 index.py # 主函数
-rwxrwxrwx 1 apple staff 1.1K 4 21 18:25 template.packaged.yml
-rw------- 1 apple staff 863B 4 21 18:34 template.yml # fc配置
发布
fun install
fun package -b tianyunperfect
fun deploy
Funfile
RUNTIME custom
RUN PYTHONUSERBASE=/code/.fun/python # 这一行不一定需要,我的mac是必须要加的
RUN fun-install pip install fastapi
RUN fun-install pip install uvicorn
bootstrap
#!/bin/bash
python index.py
index.py
from fastapi import FastAPI
app = FastAPI()
@app.get('/random/') # 点get就支持get请求
def read_root():
return {"hello":'world'}
if __name__ == '__main__':
import uvicorn
uvicorn.run(app,host='0.0.0.0',port=9000)
template.yml
ROSTemplateFormatVersion: '2015-09-01'
Transform: 'Aliyun::Serverless-2018-04-03'
Resources:
FastAPI-example: # service name
Type: 'Aliyun::Serverless::Service'
Properties:
Description: This is FC service
FastAPI-example: # function name
Type: 'Aliyun::Serverless::Function'
Properties:
Handler: index.handler
Runtime: custom
CodeUri: ./
MemorySize: 128
Timeout: 120
Events:
httpTrigger:
Type: HTTP
Properties:
AuthType: ANONYMOUS
Methods: ['GET', 'POST', 'PUT']