LoginSignup
5
1

More than 1 year has passed since last update.

aws-nodejs-typescriptのテンプレートを使ってみる(serverless v3)

Last updated at Posted at 2022-12-14

この記事は、Ateam Finergy Inc.× Ateam CommerceTech Inc.× Ateam Wellness Inc.のカレンダー | Advent Calendar 2022 の15日目の記事になります。

本日は、@tommy1038が担当いたします。

はじめに

今年の始めごろに下記の記事を書いてみたのですが、その後すぐに、serverless v3がリリースされました。

serverless v3へのアップデートガイドはこちら(https://www.serverless.com/framework/docs/guides/upgrading-v3)

業務で使用しているサービスも、serverless v3に対応していきましたが、公式のテンプレはどう変わったのかなと思ったので、前回の記事をアップデートしていければと思います。

事前準備

実行環境としては、以下のバージョンで実施しました。
ただserverlessを試してみたい、という方であれば、こちらからすぐ試せそうです。(https://github.com/serverless/serverless#quick-start)

❯ node -v
v18.12.1

❯ npm --version
8.19.3

❯ serverless --version
Framework Core: 3.25.1
Plugin: 6.2.2
SDK: 4.3.2

❯ aws --version
aws-cli/2.9.6 Python/3.11.0 Darwin/21.6.0 source/x86_64 prompt/off

AWSの認証情報をセットアップします。
aws configure コマンドを実行して、プロンプトに従えばOKです。

❯ aws configure
AWS Access Key ID [None]: your_access_key_id
AWS Secret Access Key [None]: your_secret_access_key
Default region name [None]:
Default output format [None]:

アクセスキー ID とシークレットアクセスキーがない場合は、
設定の基本 - AWS Command Line InterfaceのアクセスキーIDとシークレットアクセスキーのところで作成の手順が記載されていますので、そちらで作成してから登録してください。

サービスの作成

上記を参考に、サービスを作成していきます。

--templateオプションで作成する

現在利用できるテンプレート一覧がこのようになっています。

  • aws-clojurescript-gradle
  • aws-clojure-gradle
  • aws-nodejs
  • aws-nodejs-typescript
  • aws-alexa-typescript
  • aws-nodejs-ecma-script
  • aws-python
  • aws-python3
  • aws-ruby
  • aws-provided
  • aws-kotlin-jvm-maven
  • aws-kotlin-jvm-gradle
  • aws-kotlin-nodejs-gradle
  • aws-groovy-gradle
  • aws-java-maven
  • aws-java-gradle
  • aws-scala-sbt
  • aws-csharp
  • aws-fsharp
  • aws-go
  • aws-go-dep
  • aws-go-mod
  • plugin

では、上記のaws-nodejs-typescriptを使ってみます。

❯ serverless create --template aws-nodejs-typescript --path aws-nodejs-typescript-1

✔ Project successfully created in "aws-nodejs-typescript-1" from "aws-nodejs-typescript" template (7s)

❯ tree .
.
├── README.md
├── package.json
├── serverless.ts
├── src
│   ├── functions
│   │   ├── hello
│   │   │   ├── handler.ts
│   │   │   ├── index.ts
│   │   │   ├── mock.json
│   │   │   └── schema.ts
│   │   └── index.ts
│   └── libs
│       ├── api-gateway.ts
│       ├── handler-resolver.ts
│       └── lambda.ts
├── tsconfig.json
└── tsconfig.paths.json

4 directories, 13 files

serverless v3で実行したので、examples/aws-node-typescript at v3 · serverless/examplesが適用されるのかな思いましたが、examples/legacy/aws-nodejs-typescript at v3 · serverless/examplesが適用されているっぽいです。

このテンプレだと、年初に書いたaws-nodejs-typescriptのテンプレートを使ってみる#プロジェクトの作成 で確認したいことは事足りそうでした。

今回は、v3のテンプレを適用させたいので、公式のテンプレをURLで指定したいと思います。

--template-urlオプションで作成する

❯ serverless create --template-url https://github.com/serverless/examples/tree/v3/aws-node-typescript --path aws-nodejs-typescript-2

✔ Project successfully created in "aws-nodejs-typescript-2" (5s)

❯ tree .
.
├── README.md
├── handler.ts
├── package.json
└── serverless.yml

0 directories, 4 files

無事、v3で更新されたテンプレートを適用できたようです。

実際のテンプレートのコードはこちらです。

コードを細かくみていくことはしませんが、以前のテンプレだと機能がもりもりで読み解いていかないといけないところから、ミニマム実装になり非常にわかりやすくなっていました。たとえば、以前であればエンドポイントが自動で作成されていましたが、今回はなくなっているみたいです。また、細かいですが、serverless.tsからserverless.ymlになっていますね。

エンドポイント作成したりするテンプレートとしては、aws-node-http-api-typescriptを使えば、エンドポイント周りの作成の際、うまくお作法に則って作れそうですね。

ローカル実行

さて、では実際にローカルで実行してみましょう。

❯ yarn install
yarn install v1.22.19
warning package.json: No license field
info No lockfile found.
warning aws-nodejs-typescript-2: No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 🔨  Building fresh packages...
success Saved lockfile.
✨  Done in 7.65s.

❯ serverless invoke local --function hello
{
    "message": "Go Serverless v3! Your function executed successfully!",
    "input": ""
}

handler.tshelloファンクションが正しく呼び出されていることがわかります。

デプロイ

では、実際にawsにデプロイしたいと思いますが、リージョンをap-northeast-1にして、デプロイできるように追記します。

serverless.yml
service: aws-nodejs-typescript-2
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs14.x
  region: ap-northeast-1 # 追記

functions:
  hello:
    handler: handler.hello

plugins:
  - serverless-esbuild

では、デプロイします。(ログを出すために、--verboseをつけています。)

❯ serverless deploy --verbose

Deploying aws-nodejs-typescript-2 to stage dev (ap-northeast-1)

Packaging
Compiling to node14 bundle with esbuild...
Compiling with concurrency: Infinity
Compiling completed.
Zip service aws-nodejs-typescript-2 - 688.00 B [50 ms]
Retrieving CloudFormation stack
Creating CloudFormation stack
Creating new change set
Waiting for new change set to be created
Change Set did not reach desired state, retrying
Executing created change set
  CREATE_IN_PROGRESS - AWS::CloudFormation::Stack - aws-nodejs-typescript-2-dev
  CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
  CREATE_IN_PROGRESS - AWS::S3::Bucket - ServerlessDeploymentBucket
  CREATE_COMPLETE - AWS::S3::Bucket - ServerlessDeploymentBucket
  CREATE_IN_PROGRESS - AWS::S3::BucketPolicy - ServerlessDeploymentBucketPolicy
  CREATE_IN_PROGRESS - AWS::S3::BucketPolicy - ServerlessDeploymentBucketPolicy
  CREATE_COMPLETE - AWS::S3::BucketPolicy - ServerlessDeploymentBucketPolicy
  CREATE_COMPLETE - AWS::CloudFormation::Stack - aws-nodejs-typescript-2-dev
Uploading
Uploading CloudFormation file to S3
Uploading State file to S3
Uploading service aws-nodejs-typescript-2.zip file to S3 (688 B)
Updating CloudFormation stack
Creating new change set
Waiting for new change set to be created
Change Set did not reach desired state, retrying
Executing created change set
  UPDATE_IN_PROGRESS - AWS::CloudFormation::Stack - aws-nodejs-typescript-2-dev
  CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
  CREATE_IN_PROGRESS - AWS::Logs::LogGroup - HelloLogGroup
  CREATE_IN_PROGRESS - AWS::IAM::Role - IamRoleLambdaExecution
  CREATE_IN_PROGRESS - AWS::Logs::LogGroup - HelloLogGroup
  CREATE_COMPLETE - AWS::Logs::LogGroup - HelloLogGroup
  CREATE_COMPLETE - AWS::IAM::Role - IamRoleLambdaExecution
  CREATE_IN_PROGRESS - AWS::Lambda::Function - HelloLambdaFunction
  CREATE_IN_PROGRESS - AWS::Lambda::Function - HelloLambdaFunction
  CREATE_COMPLETE - AWS::Lambda::Function - HelloLambdaFunction
  CREATE_IN_PROGRESS - AWS::Lambda::Version - HelloLambdaVersion****************************************
  CREATE_IN_PROGRESS - AWS::Lambda::Version - HelloLambdaVersion****************************************
  CREATE_COMPLETE - AWS::Lambda::Version - HelloLambdaVersion****************************************
  UPDATE_COMPLETE_CLEANUP_IN_PROGRESS - AWS::CloudFormation::Stack - aws-nodejs-typescript-2-dev
  UPDATE_COMPLETE - AWS::CloudFormation::Stack - aws-nodejs-typescript-2-dev
Retrieving CloudFormation stack
Removing old service artifacts from S3

✔ Service deployed to stack aws-nodejs-typescript-2-dev (110s)

functions:
  hello: aws-nodejs-typescript-2-dev-hello (688 B)

Stack Outputs:
  HelloLambdaFunctionQualifiedArn: arn:aws:lambda:ap-northeast-1:************:function:aws-nodejs-typescript-2-dev-hello:3
  ServerlessDeploymentBucketName: aws-nodejs-typescript-2-serverlessdeploymentbuck-************

ではファンクションの実行と、デプロイした情報を見てみましょう。

❯ serverless invoke --function hello
{
    "message": "Go Serverless v3! Your function executed successfully!",
    "input": {}
}

❯ serverless info
service: aws-nodejs-typescript-2
stage: dev
region: ap-northeast-1
stack: aws-nodejs-typescript-2-dev
functions:
  hello: aws-nodejs-typescript-2-dev-hello

ここで念の為確認ですが、serverless infoで示されていたり、先ほどのログにも

Deploying aws-nodejs-typescript-2 to stage dev (ap-northeast-1)

と書いてあるので、stage名がdevとして登録されています。(serverless.ymlに明記した方が混乱を防げると思います。)

これと、serverless v3で導入された、stage parametersを用いることで、devstagingproductionなどの環境の変数定義と出し分けがしやすくなります。(参考: Serverless Framework v3新機能のStage Parameters紹介 | 豆蔵デベロッパーサイト

node v18にアップデート

冒頭から記事を読み進めてきた方の中で、あれ?と違和感を覚えている人がおられると思いますが、僕はローカル環境でnode v18にしていましたが、serverless.ymlnodejs14.xのままにしていたので、ちぐはぐな状態でした。

今年の11月に、aws lambdaがv18に対応しているので、nodejs v18に上げてしまいましょう。

serverless.yml
service: aws-nodejs-typescript-2
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs18.x # v14->v18にアップデート
  stage: ${opt:stage, 'dev'} # デプロイ時に設定できるように設定。デフォルトでは'dev'とする
  region: ap-northeast-1

functions:
  hello:
    handler: handler.hello

plugins:
  - serverless-esbuild
❯ serverless deploy --stage dev --verbose

Deploying aws-nodejs-typescript-2 to stage dev (ap-northeast-1)

Packaging
Compiling to node18 bundle with esbuild...
Compiling with concurrency: Infinity
Compiling completed.
Zip service aws-nodejs-typescript-2 - 688.00 B [35 ms]
Retrieving CloudFormation stack
Uploading
Uploading CloudFormation file to S3
Uploading State file to S3
Uploading service aws-nodejs-typescript-2.zip file to S3 (688 B)
Updating CloudFormation stack
Creating new change set
Waiting for new change set to be created
Change Set did not reach desired state, retrying
Executing created change set
(中略)
Retrieving CloudFormation stack
Removing old service artifacts from S3

✔ Service deployed to stack aws-nodejs-typescript-2-dev (64s)

functions:
  hello: aws-nodejs-typescript-2-dev-hello (688 B)

Stack Outputs:
  HelloLambdaFunctionQualifiedArn: arn:aws:lambda:ap-northeast-1:************:function:aws-nodejs-typescript-2-dev-hello:4
  ServerlessDeploymentBucketName: aws-nodejs-typescript-2-serverlessdeploymentbuck-************

❯ serverless invoke --function hello
{
    "message": "Go Serverless v3! Your function executed successfully!",
    "input": {}
}

管理画面でも見てみましょう。(nodejs v14 -> v18に)

nodejs v14 nodejs v18
スクリーンショット 2022-12-11 16.44.01.png スクリーンショット 2022-12-11 16.44.14.png

無事、v18にアップデートができているようです。

yarn v3にアップデート

最後におまけですが、yarnをv3にしてみます。元々、自分の環境では、

❯ yarn -v
1.22.19

で、1系だったのでアップデートしてみます。

❯ rm -rf yarn.lock node_modules
❯ yarn set version berry

.yarnrc.ymlの追記

.yarnrc.yml
yarnPath: .yarn/releases/yarn-3.3.0.cjs
nodeLinker: node-modules #追加

.gitginoreの追記

.gitignore
# package directories
node_modules
.esbuild

# Serverless directories
.serverless

# yarn v3
.yarn/
!.yarn/releases/*

これで問題なく動作するか、確認してみます。

❯ yarn install
❯ serverless invoke local --function hello
{
    "message": "Go Serverless v3! Your function executed successfully!",
    "input": ""
}

問題なさそうでした。ちょっと細かいところは、他の方の記事を見たほうが良さそうです。ひとまず今回のアップデートはうまくいったように思います。

サービスの削除

サービスは以下の形で削除できます。stage名はデフォルトでdevなのでなくても問題ありません。

❯ serverless remove --stage dev

まとめ

今回、以前の記事のアップデートということで、serverless v3の環境下でテンプレートを実行してみました。以前は、機能がもりもりのテンプレートでしたが、今回はミニマムで必要な部分だけかいつまんで進めていけそうです。

参考リンク集

5
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
5
1