AWS CLI で update-stack 実行時に An error occurred (ValidationError) when calling the UpdateStack operation: Parameters: [xxx] must have values エラーが発生した時の対処方法

2022.02.24

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

困っていた内容

AWS CLI の「aws cloudformation update-stack」にてパラメータを変更しようとしましたが、コマンド実行した際に以下エラーが発生しました。

An error occurred (ValidationError) when calling the UpdateStack operation: Parameters: [xxx, xxx] must have values

対処方法を教えてください。

どう対応すればいいの?

変更したいパラメータ以外のパラメータも ParameterKey で指定してコマンドを実行ください。

コマンド例

aws cloudformation update-stack \
--stack-name xxx \
--use-previous-template \
--parameters \
ParameterKey='InstanceType',UsePreviousValue=true \
ParameterKey='KeyName',UsePreviousValue=true \
ParameterKey='RDPLocation',UsePreviousValue=true \
ParameterKey='SSHLocation',ParameterValue='xxx.xxx.xxx.xxx/xx'

意図しない設定変更を避けるために、設定変更しないパラメータでは「UsePreviousValue=true」を付けることをお薦めします。理由は後述します。

エラーについて

本エラーは以下の2点が原因と考えられます。

  • update-stack コマンドの parameters オプションで ParameterKey と value を指定していない
  • 上記に加え、CloudFormation テンプレートの Parameters で Default 値を設定していない

Parameter - AWS CloudFormation API Reference

The key associated with the parameter. If you don't specify a key and value for a articular parameter, AWS CloudFormation uses the default value that's specified in your template.

挙動を確認してみた

変更しないパラメータの指定は「UsePreviousValue=true」を付けることをお薦めしました。

CloudFormation スタック作成時、指定したパラメータがテンプレートの Default 値と違う場合、update-stack コマンドを実行した際に意図せず設定変更される可能性があります。

今回は意図せずパラメータの設定変更が行われる挙動の例を確認します。

CloudFormation テンプレートの Parameters には以下を指定して検証します。

※Parameters の一部を抜粋

Parameters:
  KeyName:
    Type: 'AWS::EC2::KeyPair::KeyName'
  InstanceType:
    Type: String
    Default: t3.micro
  SSHLocation:
    Type: String
    Default: 192.0.2.10/32
  RDPLocation:
    Type: String

Default 値を指定しているのは「InstanceType」「SSHLocation」のみです。 CloudFormation テンプレートを使用し、t3.nano で EC2 インスタンスを作成します。

CloudFormation で EC2 インスタンス作成後、この状態で SSHLocation のパラメータを変更する update-stack コマンドを実行します。

$ aws cloudformation update-stack \
> --stack-name stack-test \
> --use-previous-template \
> --parameters \
> ParameterKey='SSHLocation',ParameterValue='192.0.2.30/32'

An error occurred (ValidationError) when calling the UpdateStack operation: Parameters: [KeyName, RDPLocation] must have values

CloudFormation テンプレートの Parameters で Default 値を設定していない「KeyName」「RDPLocation」の値を指定していないため、エラーになりました。

次のコマンドを実行します。

$ aws cloudformation update-stack \
--stack-name stack-test \
--use-previous-template \
--parameters \
ParameterKey='KeyName',UsePreviousValue=true \
ParameterKey='RDPLocation',UsePreviousValue=true \
ParameterKey='SSHLocation',ParameterValue='192.0.2.30/32'
{
    "StackId": "arn:aws:cloudformation:ap-northeast-1:xxxxxxxxxxxx:stack/stack-test/xxx"
}

「KeyName」「RDPLocation」を指定したため、コマンドは成功しました。

パラメータを確認すると、SSHLocation は指定通り「192.0.2.10/32」→「192.0.2.30/32」に変更されていますが、変更を指定していないインスタンスタイプが「t3.nano」→「t3.micro」に変更されてしまいました。

update-stack コマンドの parameters オプションで ParameterKey と value を指定していない場合、CloudFormation テンプレートで指定した Default 値が使用されます。

今回のコマンドでは パラメータ「InstanceType」を指定していないため、Default 値である「t3.micro」が使用されて update-stack が実行され、インスタンスタイプが変更されました。

意図せぬパラメータ変更を防ぐためにも「UsePreviousValue=true」の指定をお薦めします。

UsePreviousValue の詳細は以下をご参照ください。

Parameter - AWS CloudFormation API Reference

UsePreviousValue During a stack update, use the existing parameter value that the stack is using for a given parameter key. If you specify true, do not specify a parameter value.

参考資料

スタックの直接更新 - AWS CloudFormation

Parameter - AWS CloudFormation API Reference

Sample templates - AWS CloudFormation