🦁
CloudFormationでプライベートサブネット用のルートテーブルを作成した時のエラーの解消
Cloudformationでvpcスタック作成時にエラーが出力され作成に失敗した時の備忘録です。
結論
Cloudformationはメインルートテーブルを認識しない
メインテーブルは自動で作成される
事象
先日CloudFormationでプライベートサブネットのルートテーブルを作成する際、以下のエラーが出力され、スタックの作製に失敗しました。
Resource handler returned message: "Route already exists for the specified
destination CIDR block" (RequestToken: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,
HandlerErrorCode: AlreadyExists)
その際プライベートサブネットのルートテーブルは下記のように記述していました。
### RouteTable for Private Subnet
RouteTablePrivate:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: RouteTablePrivate
RoutePrivate:
Type: AWS::EC2::Route
Properties:
RouteTableId: !Ref RouteTablePrivate
DestinationCidrBlock: local
RouteTableAssocPrivate:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet
RouteTableId: !Ref RouteTablePrivate
原因
調べたところ参考URLのre.postに以下の記載がありました。
CloudFormation を使用して Amazon VPC を作成すると、CloudFormation はデフォルトで作成されたメインのルートテーブルを認識しません。CloudFormation テンプレートからルートテーブルを参照できないため、メインのルートテーブルにルートを追加したり、メインのルートテーブルからルートを削除したりすることはできません。
要するにcloudformationはメインルートテーブルを認識せず、
VPC作成時に「メインルートテーブル」を自動で作成されるが、メインテーブルと同じルーティングを設定したため競合し、エラーとなったようです。
解消
エラーメッセージにあるように既にメインルートテーブルのルーティングの設定があるので、単純にルーティングの記述を消せばエラーも解消しました。
### RouteTable for Private Subnet
RouteTablePrivate:
Type: AWS::EC2::RouteTable
Properties:
VpcId: !Ref VPC
Tags:
- Key: Name
Value: RouteTablePrivate
RouteTableAssocPrivate:
Type: AWS::EC2::SubnetRouteTableAssociation
Properties:
SubnetId: !Ref PrivateSubnet
RouteTableId: !Ref RouteTablePrivate
参考URL
Amazon Web Services, Inc. CloudFormation を使用してメインの Amazon VPC ルートテーブルにルートを追加します
Discussion