はじめに
公式にはserverlessをグローバルインストールしろって書いてるけど、それが嫌なのです。nodeの依存関係は全部package.jsonで管理したいのです。
っていう僕向けの備忘録です。
yarnは分かりません。ごめんなさい。
宗教上の理由でgo-mod を使ってます。go-depは分かりません。ごめんなさい。
好みの問題もあるので、「あ、こんな人もいるんだな」程度に読んでいただければと。
どうやったの?
とりあえずnpmのプロジェクトを作成
npm init
serverlessをローカルインストール
npm i -D serverless
プロジェクト作成
npx sls create --template aws-go-mod --path myService
作成されたファイルたちの階層をあげる(階層が深くなることが気にならない人はそのままでもok)
mv myService/* ./
ビルド
Makefile
がそのまま使えます。
make
ローカル実行
npx sls invoke local --function hello
npx sls invoke local --function world
package.json使えるので、script書いとくと便利。
package.json
{
"name": "hogehoge",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "sls invoke local --function hello",
"hello": "sls invoke local --function hello",
"world": "sls invoke local --function world",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"devDependencies": {
"serverless": "^1.52.2"
}
}
実行
npm start
npm run hello
npm run world
終わり。
感想
nodenvでnodeのバージョン管理してるので、ディレクトリごとに管理できるのだいぶ幸せになれた気がします。