Alibaba Cloud 関数コンピューティング
Alibaba Cloud 関数コンピューティングは、完全にマネージドされたイベントドリブンコンピューティングサービスです。関数コンピューティングでは、サーバーなどのインフラストラクチャを管理する必要なく、コードの記述とアップロードに集中できます。
1. セットアップ
serverless-devsは、開発者に強力なツールチェーンシステムを提供することに特化したオープンソースでオープンなサーバーレス開発者プラットフォームです。このプラットフォームを通じて、開発者はワンクリックでマルチクラウドサーバーレス製品を体験し、サーバーレスプロジェクトを迅速にデプロイするだけでなく、サーバーレスアプリケーションのライフサイクル全体でプロジェクトを管理し、サーバーレス開発者を他のツール/プラットフォームと非常にシンプルかつ迅速に組み合わせて、R&D、運用、メンテナンスの効率をさらに向上させることができます。
serverless-devsCLI をインストールする
sh
npm install @serverless-devs/s -g
AK & SK 構成を追加する
sh
s config add
# Please select a provider: Alibaba Cloud (alibaba)
# Input your AccessKeyID & AccessKeySecret
2. Hello World
新しいディレクトリに新しいプロジェクトを作成する
sh
npm init
必要な依存関係を追加する
sh
npm add hono @hono/node-server
npm add esbuild --save-dev
package.json
のscripts
セクションを編集する
json
{
"scripts": {
"build": "esbuild --bundle --outfile=./dist/index.js --platform=node --target=node20 ./src/index.ts",
"dev": "node ./dist/index.js",
"deploy": "s deploy -y"
}
}
src/index.ts
を編集する
ts
import { serve } from '@hono/node-server'
import { Hono } from 'hono'
const REQUEST_ID_HEADER = 'x-fc-request-id'
const app = new Hono()
app.post('/initialize', (c) => {
console.log(`RequestId: ${c.req.header(REQUEST_ID_HEADER)}`)
return c.text('Initialize')
})
app.post('/invoke', (c) => {
console.log(`RequestId: ${c.req.header(REQUEST_ID_HEADER)}`)
return c.text('Invoke')
})
app.get('/', (c) => {
return c.text('Hello from index!')
})
app.get('/hello', (c) => {
return c.text('Hi!')
})
const port = 9000
console.log(`Server is running on port ${port}`)
serve({
fetch: app.fetch,
port,
})
tsconfig.json
を編集する
json
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "Bundler",
"strict": true,
"skipLibCheck": true,
"lib": ["ESNext"],
"types": [],
"jsx": "react-jsx",
"jsxImportSource": "hono/jsx"
}
}
s.yaml
を編集する
yaml
edition: 3.0.0
name: my-app
access: 'default'
vars:
region: 'us-west-1'
resources:
my_app:
component: fc3
props:
region: ${vars.region}
functionName: 'my-app'
runtime: 'custom.debian10'
description: 'hello world by Hono'
timeout: 10
memorySize: 512
environmentVariables:
PATH: >-
/var/fc/lang/nodejs20/bin:/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin
NODE_PATH: /opt/nodejs/node_modules
cpu: 0.5
diskSize: 512
code: ./dist
customRuntimeConfig:
command:
- node
- index.js
port: 9000
triggers:
- triggerConfig:
methods:
- GET
- POST
- PUT
- DELETE
authType: anonymous
disableURLInternet: false
triggerName: default
description: ''
qualifier: LATEST
triggerType: http
3. デプロイ
最後に、コマンドを実行してデプロイします。
sh
npm install # install dependencies
npm run build # Compile the TypeScript code to JavaScript
npm run deploy # Deploy the function to Alibaba Cloud Function Compute