AWSServerless FW

Serverless Frameworkの`serverless.yaml`を`serverless.ts`に変換する

いつのまにか、sls create -t aws-nodejs-typescriptで作られる定義ファイルがYAMLからTypeScriptになっていました。 PythonなどではYAMLのままなので、一部テンプレートの […]

広告ここから
広告ここまで

いつのまにか、sls create -t aws-nodejs-typescriptで作られる定義ファイルがYAMLからTypeScriptになっていました。

import type { Serverless } from 'serverless/aws';

const serverlessConfiguration: Serverless = {
  service: {
    name: 'scp-db',
    // app and org for use with dashboard.serverless.com
    // app: your-app-name,
    // org: your-org-name,
  },
  frameworkVersion: '2',
  custom: {
    webpack: {
      webpackConfig: './webpack.config.js',
      includeModules: true
    }
  },
  // Add the serverless-webpack plugin
  plugins: ['serverless-webpack'],
  provider: {
    name: 'aws',
    runtime: 'nodejs12.x',
    apiGateway: {
      minimumCompressionSize: 1024,
    },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
    },
  },
  functions: {
    hello: {
      handler: 'handler.hello',
      events: [
        {
          http: {
            method: 'get',
            path: 'hello',
          }
        }
      ]
    }
  }
}

module.exports = serverlessConfiguration;

PythonなどではYAMLのままなので、一部テンプレートのみの様子です。

ちょっとびっくりするかもしれませんが、書き方が変わるわけではないので慣れましょう。

既存のYAMLを変換する

で、TypeScriptユーザーの場合「これまでのYAMLもTSで管理したい」という気持ちになると思います。

変換操作をやるのが面倒だったので、雑なCLIを作ってみました。

変換前のYAML

service:
  name: example-project
frameworkVersion: '2'
custom:
  webpack:
    webpackConfig: ./webpack.config.js
    includeModules: true

plugins:
  - serverless-webpack

provider:
  name: aws
  runtime: nodejs12.x
  apiGateway:
    minimumCompressionSize: 1024
  environment:
    AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1

functions:
  hello:
    handler: handler.hello
    events:
      - http:
          method: get
          path: hello

変換コマンド

npx serverless-yaml2tsを実行すると、実行したディレクトリにあるserverless.ymlまたはserverless.yamlを探してserverless.tsを作ります。

% npx serverless-yaml2ts
 npx: 78個のパッケージを3.656秒でインストールしました。
 Loading: /Users/development/my-services/scp-db/serverless.yaml
 serverless.yaml not found.
 Loading: /Users/development/my-services/scp-db/serverless.yml
 serverless.ts created

変換後のTSファイル

import { Serverless } from 'serverless/aws';
export const service: Serverless = {
  "service": {
    "name": "example-project"
  },
  "frameworkVersion": "2",
  "custom": {
    "webpack": {
      "webpackConfig": "./webpack.config.js",
      "includeModules": true
    }
  },
  "plugins": [
    "serverless-webpack"
  ],
  "provider": {
    "name": "aws",
    "runtime": "nodejs12.x",
    "apiGateway": {
      "minimumCompressionSize": 1024
    },
    "environment": {
      "AWS_NODEJS_CONNECTION_REUSE_ENABLED": 1
    }
  },
  "functions": {
    "hello": {
      "handler": "handler.hello",
      "events": [
        {
          "http": {
            "method": "get",
            "path": "hello"
          }
        }
      ]
    }
  }
}

module.exports = service

ブックマークや限定記事(予定)など

WP Kyotoサポーター募集中

WordPressやフロントエンドアプリのホスティング、Algolia・AWSなどのサービス利用料を支援する「WP Kyotoサポーター」を募集しています。
月額または年額の有料プランを契約すると、ブックマーク機能などのサポーター限定機能がご利用いただけます。

14日間のトライアルも用意しておりますので、「このサイトよく見るな」という方はぜひご検討ください。

広告ここから
広告ここまで

Related Category posts