0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[小ネタ]CDKでmaxAzsを複数指定したVPCを作成しようとするとエラーになる

Posted at

メモ書き程度

事象

CDKでVPCのAZを複数にしてデプロイしようとするとエラーが出る。

    this.vpc = new ec2.Vpc(this, 'Vpc', {
      maxAzs: 2
    });

エラー

XXXStack failed: ToolkitError: The stack named XXXStack failed creation, it may need to be manually deleted from the AWS console: ROLLBACK_COMPLETE: Template error: Fn::Select cannot select nonexistent value at index 1, Template error: Fn::Select cannot select nonexistent value at index 1

原因

以下のように変換されたCloudFormationテンプレートで使われている、Fn::GetAZsの仕様。

      "Type": "AWS::EC2::Subnet",
      "Properties": {
        "AvailabilityZone": {
          "Fn::Select": [
            0,
            {
              "Fn::GetAZs": ""
            }
          ]
        },

Fn::GetAZsはいつもすべてのAZを返すわけではない。デフォルトサブネットがある場合、デフォルトサブネットのあるAZしか返さない。

デフォルト サブネットがあるアベイラビリティーゾーンがない場合を除き、Fn::GetAZs 関数はデフォルト サブネットがあるアベイラビリティーゾーンのみを返します。その場合、すべてのアベイラビリティーゾーンが返されます。

よって、デフォルトサブネットが1個だけあるアカウント/リージョンでは"Fn::GetAZs": ""は長さ1の配列しか返さないので、maxAZsを2以上にするとエラーになる

対処法

2個ある

1. デフォルトサブネットをすべて削除する

商用環境であればなんならVPCごと消すシーンもあるかもしれない。

2. すべてのAZにデフォルトサブネットを作成する

開発環境ならこっちの方が便利。

aws ec2 create-default-subnet --availability-zone ap-northeast-1d
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?