ushumpei’s blog

生活で気になったことを随時調べて書いていきます。

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: {} として解決されていた

感想