2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Serverless Frameworkにおける環境変数の外部ファイル化の一例

Last updated at Posted at 2021-01-12
/conf/dev/dev.json
{
  "Other_File": "Development environment"
}
serverless.ts

import type { AWS } from '@serverless/typescript';

// 関数間で共通の環境変数はこちらに定義
const commonEnvironmentVariables = {
  Other_File: "${self:custom.otherfile.${self:provider.stage}.Other_File}",
};

const serverlessConfiguration: AWS = {
  service: "create-category-collections-api",
  frameworkVersion: "2",
  custom: {
    webpack: {
      webpackConfig: "./webpack.config.js",
      includeModules: true,
    },
    otherfile: {
      dev: "${file(./conf/dev/dev.json)}",
      stg: "${file(./conf/stg/stg.json)}",
      prd: "${file(./conf/prd/prd.json)}",
    },
  },
  // Add the serverless-webpack plugin
  plugins: ["serverless-webpack", "serverless-jest-plugin"],
  provider: {
    name: "aws",
    runtime: "nodejs12.x",
    apiGateway: {
      minimumCompressionSize: 1024,
    },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: "1",
      ...commonEnvironmentVariables,
    },
    region: "ap-northeast-1",
    stage: "${opt:stage, self:custom.defaultStage}",
  },
  functions: {
    hello: {
      handler: "handler.hello",
      events: [
        {
          http: {
            method: "get",
            path: "hello",
          },
        },
      ],
    },
  },
};

module.exports = serverlessConfiguration;



参考

Serverless Frameworkで環境変数を外部ファイルから読み込み、環境毎に自動で切り替えてみる
https://dev.classmethod.jp/articles/serverless-framework-conf-change/

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?