Serverless FW

[serverless-yaml2ts] Simply convert command from serverless.yml to serverless.ts

Serverless Framework will support a serverless.ts. That’s a great news for TypeScript users. So I publis […]

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

Serverless Framework will support a serverless.ts. That’s a great news for TypeScript users.

So I publish a new npm command to generate serverless.ts from serverless.yaml or serverless.yml.

Usage

Now we have a various serverless.yml file in our own project.

service:
  name: example-project
frameworkVersion: '>=1.72.0'
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

If we want to use serverless.ts, we have to convert it.

$ cd /path/to/your/serverless/project
$ npx serverless-yaml2ts

Then, we can get a new serverless.ts file.

import { Serverless } from 'serverless/aws';
export const service: Serverless = {
  "service": {
    "name": "example-project"
  },
  "frameworkVersion": ">=1.72.0",
  "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

Additional tasks

After converting it, we need to add a several type definition files from npm.

$ npm install -D @types/node @types/serverless ts-node typescript

And if TypeScript throw any type error, we have to fix it.

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

WP Kyotoサポーター募集中

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

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

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

Related Category posts