Serverless Framework で deploy 用ディレクトリへ移動→環境変数を設定する方法で deploy する環境を切り替える(その2)
概要
記事一覧はこちらです。
Serverless Framework で deploy 用ディレクトリへ移動→環境変数を設定する方法で deploy する環境を切り替える(その1) の続きです。
参照したサイト・書籍
目次
- deploy/remove 用の npm-scripts を記述する
- stages/dev に移動して開発環境(dev)に deploy する
- 動作確認する(dev)
- stages/stg に移動してステージング環境(stg)に deploy する
- 動作確認する(stg)
手順
deploy/remove 用の npm-scripts を記述する
package.json に deploy/remove 用の npm-scripts を記述します。
{ "name": "ksbysample-serverless-deploy", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "deploy-service": "per-env", "deploy-service:development": "cross-env-shell \"cd $SERVICE_DIR && aws-vault exec $AWS_PROFILE -- bash -c \\\"npx sls deploy -v\\\"\"", "deploy-service:ci": "cross-env-shell \"cd $SERVICE_DIR && npx sls deploy -v\"", "remove-service": "per-env", "remove-service:development": "cross-env-shell \"cd $SERVICE_DIR && aws-vault exec $AWS_PROFILE -- bash -c \\\"npx sls remove -v\\\"\"", "remove-service:ci": "cross-env-shell \"cd $SERVICE_DIR && npx sls remove -v\"", "deploy:shared-package-layer": "cross-env SERVICE_DIR=layers/shared_package_layer run-s deploy-service", "remove:shared-package-layer": "cross-env SERVICE_DIR=layers/shared_package_layer run-s remove-service", "deploy:image-service": "cross-env SERVICE_DIR=services/image_service run-s deploy-service", "remove:image-service": "cross-env SERVICE_DIR=services/image_service run-s remove-service", "deploy:sample-service": "cross-env SERVICE_DIR=services/sample_service run-s deploy-service", "remove:sample-service": "cross-env SERVICE_DIR=services/sample_service run-s remove-service", "deploy:layers": "run-p deploy:shared-package-layer", "remove:layers": "run-p remove:shared-package-layer", "deploy:services": "run-s deploy:image-service deploy:sample-service", "remove:services": "run-s remove:image-service remove:sample-service", "deploy:all": "run-s deploy:layers deploy:services", "remove:all": "run-s remove:services remove:layers" }, "repository": { "type": "git", "url": "git+https://github.com/ksby/ksbysample-serverless-deploy.git" }, "keywords": [], "author": "", "license": "ISC", "bugs": { "url": "https://github.com/ksby/ksbysample-serverless-deploy/issues" }, "homepage": "https://github.com/ksby/ksbysample-serverless-deploy#readme", "devDependencies": { "cross-env": "^7.0.2", "npm-run-all": "^4.1.5", "per-env": "^1.0.2", "serverless": "^1.74.1", "serverless-python-requirements": "^5.1.0" } }
deploy:all
タスクのフローは以下のようになります。
eploy:shared-package-layer
やdeploy:image-service
、deploy:sample-service
では cross-env で環境変数 SERVICE_DIR に移動先のディレクトリのパスをセットしてdeploy-service
タスクを呼び出します。ポイントとしては npm-scripts は stages/dev の下に cd してから実行しても実行時のディレクトリはプロジェクトのルートディレクトリ(node_modules のあるディレクトリ)になるので、環境変数 SERVICE_DIR に設定するパスはそこからの相対パスにする必要があるという点です。
試しに
deploy-service:development
タスクを"deploy-service:development": "cross-env-shell \"ls -l\""
に変更してからnpm run deploy:all
を実行してみると、deploy-service:development
タスクが呼び出される度にプロジェクトのルートディレクトリにある node_modules ディレクトリが表示されています。deploy-service
タスクが呼び出されると per-env により NODE_ENV の値が未セットの場合(deployment になる)にはdeploy-service:development
タスクが、NODE_ENV=ci がセットされている場合にはdeploy-service:ci
タスクが呼び出されます。deploy-service:development
及びdeploy-service:ci
タスクでは cross-env-shell(cross-env に含まれている)でcd
及びsls deploy
を実行します。cross-env-shell で実行することで git-bash や cross-env で設定された環境変数をcd
及びsls deploy
側で利用できるようになります。
stages/dev に移動して開発環境(dev)に deploy する
stages/dev に移動してから npm run deploy:all
を実行します。
作成されたリソースを確認すると全てに STAGE 名である dev
が入っています。
■Lambda 関数
■Lambda レイヤー
■IAM ロール
■CloudWatch ロググループ
■S3 バケット
■CloudFormation スタック
■SQS キュー
■DynamoDB テーブル
動作確認する(dev)
Postman から API Gateway の endpoint にアクセスすると 200 OK が返ってきて、
DynamoDB の sample-table-dev テーブルにデータが保存されました。
ksbysample-upload-bucket-dev バケットに sample.jpg をアップロードすると、
ksbysample-resize-bucket-dev バケットに sample_thumb.jpg が生成されました。
S3 --> Lambda --> S3 の処理をする時の X-Ray を見てみると以下のように表示されていました。
問題なく動作しています。
stages/stg に移動してステージング環境(stg)に deploy する
stages/dev で deploy したリソースは残したままの状態で stages/stg に移動して deploy します。
(..........途中省略..........)
作成されたリソースを確認すると全てに STAGE 名である stg
が入っていました(キャプチャは省略)。
動作確認する(stg)
Postman から API Gateway の endpoint にアクセスすると 200 OK が返ってきて、
DynamoDB の sample-table-stg テーブルにデータが保存されました。
ksbysample-upload-bucket-stg バケットに sample2.jpg をアップロードすると、
ksbysample-resize-bucket-stg バケットに sample2_thumb.jpg が生成されました。
dev のリソースがある状態でも stg も問題なく動作しました。
履歴
2020/07/19
初版発行。