DA事業本部の横山です。
業務でServerless Frameworkのテンプレートを書くことがよくあるのですが、テンプレート内に変数を入れていくと実際にデプロイされる際にどんな値が入るのかを確認したくなることがあります。
Serverless Frameworkにprint
というコマンドがあるので、それで確認してみます。
前提条件
- Serverless Frameworkがインストールされていること
- Version
3.30.1
で確認しました。
- Version
serverless printをたたいてみる
作成したserverless.yml
は以下のような内容です。
serverless.yml
service: aws-python-project
frameworkVersion: '3'
provider:
name: aws
runtime: python3.9
region: ap-northeast-1
params:
default:
ips: 111.11.11.111/32,222.22.22.222/32,333.33.333.333/32
functions:
hello:
handler: handler.hello
outputs:
param_raw: ${param:ips}
param_list: !Split
- ','
- ${param:ips}
account_id: ${aws:accountId}
service: ${self:service}
instance_id: ${sls:instanceId}
stage: ${sls:stage}
cf_output: ${cf:test-stack.TestOutput}
parameter_store_string: ${ssm:/Test/TestString}
parameter_store_string_list: ${ssm:/Test/TestString}
env: ${env:AWS_PROFILE}
CloudFormationのOutput欄に、Serverless Frameworkのいろいろな変数参照を記載した内容になっています。
Output内容は以下です。
param_raw
: パラメータとして受け取った値そのままparam_list
: CloudFormationの関数!Split
を使ってカンマで分割した値account_id
: AWSのアカウントIDservice
: 本テンプレートのサービス名instance_id
: Serverless Frameworkが実行毎に払い出す乱数stage
: ステージ名cf_output
: 別のCloudFormationのOutputの変数参照parameter_store_string
: ParameterStoreの値(String型)parameter_store_string_list
: ParameterStoreの値(StringList型)env
: 実行時の環境変数
Serverless Frameworkのprintコマンドを叩いてみます。
$ npx sls print
service: aws-python-project
frameworkVersion: '3'
provider:
name: aws
runtime: python3.9
region: ap-northeast-1
stage: dev
versionFunctions: true
params:
default:
ips: 111.11.11.111/32,222.22.22.222/32,333.33.333.333/32
functions:
hello:
handler: handler.hello
events: []
name: aws-python-project-dev-hello
outputs:
param_raw: 111.11.11.111/32,222.22.22.222/32,333.33.333.333/32
param_list:
Fn::Split:
- ','
- 111.11.11.111/32,222.22.22.222/32,333.33.333.333/32
account_id: '000000000000'
service: aws-python-project
instance_id: '1684463139942'
stage: dev
cf_output: test-output-text
parameter_store_string: test-string
parameter_store_string_list:
- test-string-1
- test-string-2
- test-string-3
env: test-profile
変数部分に実際に値が埋め込まれたテンプレート内容が出力されました。
テンプレートのoutputsから何が変わったのか確認
いくつか気になったところだけ記載します。
provider.stage
,provider.versionFunctions
が追加されている- Serverless Frameworkが暗黙的に埋めるようです。
- 本記事の主題ではないので、無視します
functions.hello.events
,functions.hello.name
が追加されている- Serverless Frameworkが暗黙的に埋めるようです。
- 本記事の主題ではないので、無視します
param_raw
,param_list
- CloudFormationの関数
!Sprit
については実行結果が表示されるわけではないようです。
- CloudFormationの関数
instance_id
:ランダムな数字文字列が入っているので、実行毎に名称を変更させたい場合に使えそうです。parameter_store_string_list
:ParameterStoreのStringList型を読み込むとカンマ区切り文字列ではなくString型の配列として受け取れるようです。
全ての変数に値が埋め込まれた形のServerless Frameworkテンプレートが出力されました。これで、デプロイすることなく実装中のテンプレート内容が意図通りか確認できるようになりました。
おわりに
Serverless Frameworkのprint
コマンドを使って、テンプレート中の変数を実際の値を埋め込んで出力してみました。
変数の値がちゃんと設定されているかはがデプロイを行うことなくわかるので、開発していく上で利用していきたいと思います。
以上になります。この記事がどなたかの助けになれば幸いです。