LoginSignup
10
8

More than 3 years have passed since last update.

sam deployの新しいオプション(--config-env,--config-file)を動作検証する

Posted at

0.前置き

少し前にsam deployの仕様が整理され、samconfig.tomlを参照してデプロイするようになりました。
https://dev.classmethod.jp/articles/aws-sam-simplifies-deployment/

つい3週間ほど前に--config-envと--config-fileというオプションが追加されたようです。
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html#rules
https://github.com/aws/aws-sam-cli/pull/2176

動作検証します。

A.結論

下記です。

オプション 仮説
--config-env [environment] samconfig.tomlに記述した環境別の設定を読み込む
--config-file [filename] [filename]を設定ファイルとして読み込む

※組み合わせ可

1.基本情報

1−1.環境

項目 バージョン
macOS mojave 10.14.6
anaconca 4.8.3
python 3.6
aws-sam-cli 1.6.2

※使おうと思ってる直近の案件にあわせてpython3.6

1−2.検証手順

No 分類 コマンド 概要
1 default sam deploy デフォルト設定ファイルのdefaultを実行
2 test1 sam deploy --config-env test1 デフォルト設定ファイルのtest1を実行
3 test2 sam deploy --config-file another-config.toml 追加の設定ファイルのdefaultを実行
4 test3 sam deploy --config-file another-config.toml --config-env test3 追加の設定ファイルのtest3を実行

1−3.設定ファイル

設定群のヘッダーに[environment.command.parameters]を記述します。
--config-envをつけない場合は、environmentがdefaultになります。
environmentには任意の文字列を設定します。
commandのところは、利用するコマンドを指定します。

1−3−1.デフォルト設定ファイル

samconfig.toml
version = 1.0
[default.deploy.parameters]
stack_name = "default-stack"
s3_bucket = "sam-deploy-option-bucket-1"
region = "ap-southeast-1"
profile = "default-stack"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides="Environment=default"

[test1.deploy.parameters]
stack_name = "test1-stack"
s3_bucket = "sam-deploy-option-bucket-1"
region = "ap-southeast-1"
profile = "test-sam1"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides="Environment=test1"

1−3−2.追加の設定ファイル

another-config.toml
version = 1.0
[default.deploy.parameters]
stack_name = "test2-stack"
s3_bucket = "sam-deploy-option-bucket-3"
region = "ap-southeast-1"
profile = "test-sam2"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides="Environment=test2"

[test3.deploy.parameters]
stack_name = "test3-stack"
s3_bucket = "sam-deploy-option-bucket-4"
region = "ap-southeast-1"
profile = "test-sam3"
confirm_changeset = true
capabilities = "CAPABILITY_IAM"
parameter_overrides="Environment=test3"

2.検証

2−1.デフォルト設定ファイルのdefaultを実行

オプションを指定しないで実行します。

$ sam deploy

default-stackを呼び出すはずです。

Deploying with following values
===============================
Stack name                 : default-stack
Region                     : ap-southeast-1
Confirm changeset          : True
Deployment s3 bucket       : sam-deploy-option-bucket-1
Capabilities               : ["CAPABILITY_IAM"]
Parameter overrides        : {'Environment': 'default'}

default-stackが呼び出されています。

2−2.デフォルト設定ファイルのtest1を実行

--config-envでtest1を指定します。

$ sam deploy --config-env test1

test1-stackが呼び出されるはずです。

Deploying with following values
===============================
Stack name                 : test1-stack
Region                     : ap-southeast-1
Confirm changeset          : True
Deployment s3 bucket       : sam-deploy-option-bucket-2
Capabilities               : ["CAPABILITY_IAM"]
Parameter overrides        : {'Environment': 'test1'}

test1-stackが呼び出されています。

2−3.追加の設定ファイルのdefaultを実行

$ sam deploy --config-file another-config.toml

another-config.tomlのdefaultなので、test2-stackになるはずです。

Deploying with following values
===============================
Stack name                 : test2-stack
Region                     : ap-southeast-1
Confirm changeset          : True
Deployment s3 bucket       : sam-deploy-option-bucket-3
Capabilities               : ["CAPABILITY_IAM"]
Parameter overrides        : {'Environment': 'test2'}

test2-stackが呼び出されています。

2−4.追加の設定ファイルのtest3を実行

sam deploy --config-file another-config.toml --config-env test3

2つのオプションのあわせ技です。test3-stackになるはずです。

Deploying with following values
===============================
Stack name                 : test3-stack
Region                     : ap-southeast-1
Confirm changeset          : True
Deployment s3 bucket       : sam-deploy-option-bucket-4
Capabilities               : ["CAPABILITY_IAM"]
Parameter overrides        : {'Environment': 'test3'}

test3-stackが呼び出されています。

3.生成されたスタックの確認

別々のスタックが生成されています。
cFnスタックの確認.png

4.結論

下記の通りでした。

オプション 仮説
--config-env [environment] samconfig.tomlに記述した環境別の設定を読み込む
--config-file [filename] [filename]を設定ファイルとして読み込む

※組み合わせ可

一つの設定ファイルに環境別の設定を記述してもいいし、環境ファイルを別々に用意してもよくなりました。
ほしかった機能が正式に実装されたので素直にうれしいですね。

X.余談

今回のネタは、英語版のドキュメントを眺めていたらたまたま見つけました。まだ日本語版には載っていません。(2020/10/10現在)
こういう翻訳版の更新の遅さに加えて、awsのドキュメントが自動翻訳に頼り始めているのか、ファンキーな日本語になっているものがあります。たとえば、下記のKMSの記事です。
https://docs.aws.amazon.com/ja_jp/kms/latest/developerguide/concepts.html
原文で読める英語力を身につけるか、英語の記事をchromeで開いて日本語翻訳して読むことにします。

10
8
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
10
8