serverless の設定で簡単なロジックを含める
stage とかに応じて設定を変える方法があったのでいじってみます、${opt:stage}
とかよりもっと複雑なことしたくなったときに使えると思います: https://www.serverless.com/framework/docs/providers/aws/guide/variables/#exporting-a-function
なんとなく TypeScript でテンプレート作成しました
sls create -t aws-nodejs-typescript -p serverless-demo
import type { AWS } from '@serverless/typescript'; import hello from '@functions/hello'; const serverlessConfiguration: AWS = { ... custom: { hoge: "${file(./hoge.js)}", ... }, }; module.exports = serverlessConfiguration;
hoge: "${file(./hoge.js)}"
で hoge.js
に定義した関数を呼び出してくれるらしい
パラメータはどんな感じか見たいのでこんな感じの関数を書いた
module.exports = async (p) => { console.log(p) return {} }
serverless print
したらこんなの出てきた
{ options: [Object: null prototype] { format: null, path: null, transform: null, region: null, 'aws-profile': null, app: null, org: null, 'use-local-credentials': null, config: null, stage: null, param: null, help: null, version: null, verbose: null, debug: null }, resolveConfigurationProperty: [AsyncFunction: resolveConfigurationProperty], resolveVariable: [AsyncFunction: resolveVariable] }
で hoge: "${file(./hoge.js)}"
は hoge: {}
として解決されていた
感想
stage
とか手に入るので、環境毎に何か変えるとかに使えそう、定期実行関数のスケジュールを開発環境では少なくしておくとか。resolveXXX
系の関数で設定から値を持ってくることもできるfile
の実体、多分ここっぽいのだけど typescript は対応してなさそう?: https://github.com/serverless/serverless/blob/8dae21375801be00f24d68d6a3e12854132b7bb9/lib/configuration/variables/sources/file.js#L58serverless print
が知れてよかった。諸々 resolve された状態を出してくれるコマンド