AWS CLI のエラーメッセージ「The parameter groupName cannot be used with the parameter subnet」の解消法

AWS CLI コマンドで「The parameter groupName cannot be used with the parameter subnet」のエラーが発生した時は、セキュリティグループを指定するオプションの見直しを行ってください。
2021.04.14

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

困っていた内容

以下の AWS CLI コマンドを実行したところ、エラーが発生しました。

aws ec2 run-instances \
--image-id ami-0123456789abcdef0 \
--instance-type t3.micro \
--key-name testkey \
--security-groups sg-01234567 \
--subnet-id subnet-01234567 \

An error occurred (InvalidParameterCombination) when calling the RunInstances operation: The parameter groupName cannot be used with the parameter subnet

解消法を教えてください。

どう対応すればいいの?

--security-groups オプションを --security-group-ids オプションに変更してください。

aws ec2 run-instances \
--image-id ami-0123456789abcdef0 \
--instance-type t3.micro \
--key-name testkey \
--security-group-ids sg-01234567 \
--subnet-id subnet-01234567 \

補足説明

--security-groups オプションはデフォルト VPC に存在するセキュリティグループの指定のみ有効になります。

そのため、通常の場合は--security-group-ids オプションを使用することで未然防止ができると考えられます。

参考資料

run-instances — AWS CLI 1.19.51 Command Reference

--security-groups (list)

[EC2-Classic, default VPC] The names of the security groups. For a nondefault VPC, you must use security group IDs instead.

If you specify a network interface, you must specify any security groups as part of the network interface.

Default: Amazon EC2 uses the default security group.

(string)