1
0

More than 3 years have passed since last update.

Serverless FrameworkでHello World作成(入門編)

Posted at

概要

Serverless FrameworkでAWS LambdaにNestJSのプログラムをデプロイし、ブラウザでHello Worldを出力する事をゴールとした記事です。環境構築や設定を記述しておりますので、文字通り入門編となります。

アジェンダ

  1. Serverless Frameworkとは
  2. 環境構築
  3. ローカルデプロイ
  4. サーバデプロイ

1.Serverless Frameworkとは

Serverless Frameworkとは、AWSやGCP等のクラウドサービスに対しパッケージ作成/デプロイを実施できるツールです。インフラ環境構築をコードで管理することで開発の効率化や運用コストの低減するIaCツール。クラウド開発支援ツールとして人気を集めています。
○Serverless Framework
https://www.serverless.com/
image.png

2.環境構築

2-1. 必要ライブラリのインストール

利用するパッケージは下記2点。yarnコマンドでinstallしてください。

# フレームワーク 概要
1 Serverless Framework IaCツール
2 nestjs/cli サーバ側のフレームワーク
ServerlessFrameworkのインストール
~/develop/study $ yarn global add serverless
yarn global v1.22.5
warning package.json: No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning " > @vue/compiler-sfc@3.0.11" has unmet peer dependency "vue@3.0.11".
warning "serverless > @serverless/components > inquirer-autocomplete-prompt@1.4.0" has unmet peer dependency "inquirer@^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0".
[4/4] 🔨  Building fresh packages...
success Installed "serverless@2.52.0" with binaries:
      - serverless
      - sls
  Done in 6.01s.
NestJSのインストール
~/develop/study $ yarn global add @nestjs/cli
yarn global v1.22.5
warning package.json: No license field
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
warning " > @vue/compiler-sfc@3.0.11" has unmet peer dependency "vue@3.0.11".
warning "serverless > @serverless/components > inquirer-autocomplete-prompt@1.4.0" has unmet peer dependency "inquirer@^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0".
[4/4] 🔨  Building fresh packages...
success Installed "@nestjs/cli@8.0.2" with binaries:
      - nest
  Done in 3.77s.

2-2. テンプレートのインストール

gitからinstall
~/develop/study/serverless $ git clone https://github.com/rdlabo/serverless-nestjs.git
Cloning into 'serverless-nestjs'...
remote: Enumerating objects: 170, done.
remote: Counting objects: 100% (64/64), done.
remote: Compressing objects: 100% (61/61), done.
remote: Total 170 (delta 30), reused 13 (delta 3), pack-reused 106
Receiving objects: 100% (170/170), 379.27 KiB | 1.18 MiB/s, done.
Resolving deltas: 100% (75/75), done.
ライブラリのインストール
~/develop/study/serverless/serverless-nestjs $ pwd
/Users/kawamurakouji/develop/study/serverless/serverless-nestjs
~/develop/study/serverless/serverless-nestjs $ yarn
yarn install v1.22.5
warning ../../../../package.json: No license field
info No lockfile found.
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~省略~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[4/4] 🔨  Building fresh packages...
success Saved lockfile.
  Done in 63.89s.
~/develop/study/serverless/serverless-nestjs $ 

これで、環境構築の準備完了です。

3. ローカルデプロイ

ローカルデプロイコマンド
~/develop/study/serverless/serverless-nestjs $ serverless offline start
offline: Starting Offline: dev/us-east-1.
offline: Offline [http for lambda] listening on http://localhost:3002
offline: Function names exposed for local invocation by aws-sdk:
           * index: serverless-nestjs-dev-index

   ┌─────────────────────────────────────────────────────────────────────────┐
                                                                            
      ANY | http://localhost:3000/dev                                       
      POST | http://localhost:3000/2015-03-31/functions/index/invocations   
      ANY | http://localhost:3000/dev/{proxy*}                              
      POST | http://localhost:3000/2015-03-31/functions/index/invocations   
                                                                            
   └─────────────────────────────────────────────────────────────────────────┘

offline: [HTTP] server ready: http://localhost:3000 🚀
offline: 
offline: Enter "rp" to replay the last request

【実行結果】
スクリーンショット 2021-07-20 12.38.52.png

4. サーバデプロイ

サーバデプロイの上、実行してみます。
※事前にAwsProfileの設定が必要。
AwsProfileの設定方法

サーバデプロイコマンド
~/develop/study/serverless/serverless-nestjs $ serverless deploy   
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service serverless-nestjs.zip file to S3 (11.2 MB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..............
Serverless: Stack update finished...
Service Information
service: serverless-nestjs
stage: dev
region: us-east-1
stack: serverless-nestjs-dev
resources: 14
api keys:
  None
endpoints:
  ANY - https://XXXX.execute-api.us-east-1.amazonaws.com/dev/
  ANY - https://XXXX.execute-api.us-east-1.amazonaws.com/dev/{proxy+}
functions:
  index: serverless-nestjs-dev-index
layers:
  None
Serverless: Deprecation warnings:

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:
 - ServerlessOffline for "apiKey", "corsAllowHeaders", "corsAllowOrigin", "corsDisallowCredentials", "corsExposedHeaders", "disableCookieValidation", "enforceSecureCookies", "hideStackTraces", "host", "httpPort", "httpsProtocol", "lambdaPort", "noPrependStageInUrl", "noAuth", "ignoreJWTSignature", "noTimeout", "prefix", "printOutput", "resourceRoutes", "useChildProcesses", "useWorkerThreads", "websocketPort", "webSocketHardTimeout", "webSocketIdleTimeout", "useDocker", "layersDir", "dockerReadOnly", "functionCleanupIdleTimeSeconds", "allowCache", "dockerHost", "dockerHostServicePath", "dockerNetwork"
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

Support for "package.include" and "package.exclude" will be removed with next major release. Please use "package.patterns" instead
More Info: https://www.serverless.com/framework/docs/deprecations/#NEW_PACKAGE_PATTERNS

Resolution of lambda version hashes was improved with better algorithm, which will be used in next major release.
Switch to it now by setting "provider.lambdaHashingVersion" to "20201221"
More Info: https://www.serverless.com/framework/docs/deprecations/#LAMBDA_HASHING_VERSION_V2

【実行結果】
スクリーンショット 2021-07-20 12.49.00.png

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