Serverless FrameworkをWindowsで使ってみた

2021/06/22に公開

本記事は自分がServerless Frameworkを使ってみて躓いた点を中心に記載します。

実施環境

  • Windows10 20H2
>java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment Corretto-8.272.10.3 (build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM Corretto-8.272.10.3 (build 25.272-b10, mixed mode)

必要な環境

  • Windows 7+ / Windows Server 2003+
  • PowerShell v2+
  • .NET Framework 4+

事前準備

Windows向けパッケージ管理ソフト「chcolatey」

https://chocolatey.org/install

serverless-cli をインストール

https://www.serverless.com/framework/docs/getting-started/

  • 管理者権限でPowerShellを起動し入力するコマンド

choco install serverless -y

作業ディレクトリ作成

mkdir C:\Projects\serverless-framework-sample
cd C:\Projects\serverless-framework-sample

テンプレート作成

https://www.serverless.com/framework/docs/providers/aws/cli-reference/create/

公式のテンプレート一覧

https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates/

今回はLambdaをTypeScriptで作成したいので aws-nodejs-typescript 選択します。

公式イメージだと以下のように省略可能です。

serverless create --template aws-nodejs-typescript --path aws-nodejs-typescript-demo

あるいはテンプレートURLがGithub/BitBucketのURLを指定します。

serverless create --template-url https://github.com/serverless/serverless/tree/master/lib/plugins/create/templates/aws-nodejs-typescript --path aws-nodejs-typescript-demo

DynamoDB Localの用意

npm install --save serverless-dynamodb-local@0.2.10

serverless.ts から読み込みがされなかったので、
serverless.yml を新規作成して設定を記載しました。

sls dynamodb install

https://www.serverless.com/framework/docs/providers/aws/guide/resources/#configuration
例に従って設定しました。

resources: # CloudFormation template syntax
  Resources:
    usersTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: usersTable
        AttributeDefinitions:
          - AttributeName: email
            AttributeType: S
        KeySchema:
          - AttributeName: email
            KeyType: HASH
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1

これでとりあえず動く環境ができました。
色々を検索すると Linux/Macでの開発パターンが多く、
実際に私物のMacbookだと、サクサク動いたので色々と紆余曲折ありましたが動きました。

>sls dynamodb start --migrate
Serverless: Deprecation warning: CLI options definitions were upgraded with "type" property (which could be one of "string", "boolean", "multiple"). Below listed plugins 
do not predefine type for introduced options:
             - ServerlessDynamodbLocal for "online", "port", "cors", "inMemory", "dbPath", "sharedDb", "delayTransientStatuses", "optimizeDbBeforeStartup", "migrate", "seed", "migration", "heapInitial", "heapMax", "convertEmptyValues", "localPath"
            Please report this issue in plugin issue tracker.
            Starting with next major release, this will be communicated with a thrown error.
            More Info: https://www.serverless.com/framework/docs/deprecations/#CLI_OPTIONS_SCHEMA
Serverless: Configuration warning at 'provider.runtime': should be equal to one of the allowed values [dotnetcore2.1, dotnetcore3.1, go1.x, java11, java8, java8.al2, nodejs10.x, nodejs12.x, nodejs14.x, provided, provided.al2, python2.7, python3.6, python3.7, python3.8, ruby2.5, ruby2.7]
Serverless:  
Serverless: Learn more about configuration validation here: http://slss.io/configuration-validation
Serverless:  
Serverless: Deprecation warning: Starting with next major, Serverless will throw on configuration errors by default. Adapt to this behavior now by adding "configValidationMode: error" to service configuration
            More Info: https://www.serverless.com/framework/docs/deprecations/#CONFIG_VALIDATION_MODE_DEFAULT
Dynamodb Local Started, Visit: http://localhost:8000/shell
Serverless: DynamoDB - created table usersTable

次のチャレンジとしてはDockerでAPI GatewayまでSimulationできるプラグインがあるようなので、
これでWindowsでServerlessな開発の入口に立てるかなと思います。

https://github.com/serverless-community-labs/serverless-plugin-simulate

色々と時間がかかりましたが、まずは目的のDynamoDB LocalをServerless Frameworkで実行する手順は理解できました。

Github

https://github.com/midnight480/serverless-framework-sample/tree/master/src/aws-nodejs-typescript-demo

参考

AWS公式ドキュメントでは手順としてはAWS CLIで実行する方法が紹介されています。

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.DownloadingAndRunning.html

Discussion