[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.

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

    Random posts

    Home
    Search
    Bookmark