AWS管理コンソール上で、CloudFormationのパラメータを整理・分類する

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

よく訓練されたアップル信者、都元です。本日は雪による交通機関の乱れによりリモートワークをしております。こんな日はブログ日和ですね。

ということで、今回はCloudFormation (CFn)のパラメータ整理についてお話します。CloudFormationの比較的新し目の機能なんですが、登場から一ヶ月以上経っています。

CFnのパラメータが多いとアタマの整理が大変

CFnテンプレートを汎用的に作ろうとすればするほど、構築時に入力すべきパラメータが増えてしまいますね。パラメータの最大数は現時点で60個とされていますが、それでも多いですね。

従来、テンプレートで定義したパラメータは、AWS管理コンソール上では名前でソートされ、入力欄が提示されていました。例えば下記のようなパラメータ定義があった場合。

...

"Parameters": {
"VPC" : {
"Description" : "VPC ID for deploying this system",
"Type" : "AWS::EC2::VPC::Id"
},
"FrontendSubnets" : {
"Description" : "Subnet IDs for ELB",
"Type" : "List"
},
"ApplicationSubnets" : {
"Description" : "Subnet IDs for Application servers",
"Type" : "List"
},
"KeyName": {
"Description": "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type": "AWS::EC2::KeyPair::KeyName"
},

"HostedZoneId" : {
"Description" : "The ID of an existing Amazon Route 53 hosted zone",
"Type" : "AWS::Route53::HostedZone::Id"
},
"Hostname" : {
"Description" : "The DNS name of an existing Amazon Route 53 hosted zone",
"Type" : "String"
},

"SolutionStack" : {
"Description" : "Solution stack name of Elastic Beanstalk",
"Type" : "String"
},
"AppVersionLabel" : {
"Description" : "The version label string of application version.",
"Type" : "String"
},
"EnvironmentType" : {
"Default" : "development",
"Description" : "The environment type - production, development or local",
"AllowedValues": [ "production", "development" ],
"Type" : "String",
"ConstraintDescription" : "must specify production, development or local."
},
"EnableXxxFeature" : {
"Default" : "yes",
"AllowedValues": [ "yes", "no" ],
"Type" : "String",
"ConstraintDescription" : "must specify yes or no."
}
},

...

一応、テンプレート上では空行を駆使してパラメータを3つに分類しています。1つ目は主にネットワーク設定に関わるパラメータ。2つ目はDNS設定に関わるパラメータ、3つ目はアプリケーションの動作環境に関わるパラメータです。

AWS管理コンソールにて、このテンプレートでスタックを作ろうとしてみると、下記のようにパラメータ名でソートされます。

screenshot-1453098082518

パラメータをグルーピングして、グループ内の順序も制御できる

というアップデートが、2015年12月にリリースされています。

CFnテンプレートJSONのトップレベル(Parameters等のsiblings)の名前 Metadata に対して、AWS::CloudFormation::Interface を定義することにより実現します。

具体的にはこんな感じ。

"Metadata" : {
"AWS::CloudFormation::Interface" : {
"ParameterGroups" : [
{
"Label" : { "default" : "Network Configuration" },
"Parameters" : [ "VPC", "FrontendSubnets", "ApplicationSubnets", "KeyName" ]
},
{
"Label" : { "default" : "Application Configuration" },
"Parameters" : [
"SolutionStack", "AppVersionLabel",
"EnvironmentType", "EnableXxxFeature"
]
},
{
"Label" : { "default":"DNS Configuration" },
"Parameters" : [
"HostedZoneId", "Hostname"
]
}
],
"ParameterLabels" : {
"VPC" : { "default" : "VPC ID" },
"KeyName" : { "default" : "EC2 key pair name" },
"EnableXxxFeature" : { "default" : "Enable application xxx feature?" }
}
}
},

ここでは ParameterGroups において下記の3つのグループを定義しています。そしてそれぞれについて、属するパラメータ名を表示したい順番で定義します。

  • Network Configuration
  • Application Configuration
  • DNS Configuration

また ParameterLabels において、パラメータ名ではない自由なラベルをパラメータに対して定義しています。

すると。

screenshot-1453098024453

キレイに整理されますね。

まとめ

CFnによるシステム環境構築を、CLIやその他、管理コンソール以外の手段で行う場合は全く関係ないのですが。。。ともあれ、管理コンソール経由でCFnスタックの作成または更新を行う機会は多いと思います。

そんな時、パラメータがこのように綺麗に整理されていることでオペミスを減らすような効果もあると思います。是非積極的に使っていきたい機能です。