こんにちは。
繁松です。
はじめに
Transit Gateway Attachmentの作成/削除が切り替え可能なCloudFormationテンプレートを使って検証時のコスト削減を試みたのでブログにしました。
Transit Gatewayの料金
Transit Gatewayの固定料金は「Transit Gateway Attachmentごとの料金」がかかります。
東京リージョンでは1時間あたり 0.07USD
30日間の利用では 50.4USD
4つのVPCで利用すると 201.6USD です。
複数VPCでTransit Gatewayを使った検証をしていた時に思ったよりも料金がかかってしまうことがありました。
毎回作成/削除を実施するのは手間だったのでCloudFormationでTransit Gateway Attachmentの作成/削除を切り替えるテンプレートを考えてみました。
やってみた
前提
- VPC、Subnet 作成済み
- Transit Gateway 作成済み
- Transit Gateway Routetable 作成済み
- Transit Gateway RouteTableは同一のルートテーブルを使用
- 別々のルートテーブルでも問題ないですが、VPC間を通信させる場合手動でルートの設定が必要になります。
- 単体アカウントでの利用を想定
- テンプレートの中でTransit Gateway Routetableを設定します。
- 別アカウントからではTransit Gateway Routetableを参照できない為エラーになります。
CloudFormation
CloudFormation テンプレートです。
[Condition]で作成/削除を切り替えれるようにしています。
作成に必要な以下の情報をCloudFormationのパラメータで入力します。
VPC ID:Transit Gateway Attachementを作成するVPC
VPC CIDR:上記VPCのCIDR、Transit Gateway Routetabelの設定に利用
TransitGatewayAttachmentSubnetId:Transit Gateway Attachmentを作成するサブネット
TransitGatewayRouteTableId:Transit Gateway Routetable ID
TransitGatewayId:Transit Gateway ID
CreateTransitGatewayAttachment:yes/no
AWSTemplateFormatVersion: "2010-09-09"
Description: "Transit Gateway Attachment Create"
Metadata:
"AWS::CloudFormation::Interface":
ParameterGroups:
- Label:
default: "TransitGateway Attachment Create"
Parameters:
- VpcId
- VPCCidr
- TransitGatewayAttachmentSubnetId
- TransitGatewayRouteTableId
- TransitGatewayId
- CreateTransitGatewayAttachment
# ------------------------------------------------------------#
# Input Parameters
# ------------------------------------------------------------#
Parameters:
VpcId:
Description: "VPC ID"
Type: String
VPCCidr:
Description: "VPC CIDR"
Type: String
TransitGatewayAttachmentSubnetId:
Description: "TransitGatewayAttachment Subnet ID"
Type: String
TransitGatewayRouteTableId:
Description: "TransitGateway RouteTable ID"
Type: String
TransitGatewayId:
Description: "TransitGateway ID"
Type: String
CreateTransitGatewayAttachment:
Description: "Create Attachment"
Default: "yes"
Type: String
AllowedValues: [ "yes", "no" ]
# ------------------------------------------------------------#
# Conditions
# ------------------------------------------------------------#
Conditions:
CreateAttachment: !Equals
- !Ref CreateTransitGatewayAttachment
- "yes"
Resources:
# ------------------------------------------------------------#
# Transit Gateway Attachment
# ------------------------------------------------------------#
# Transit Gateway Attachment
TransitGatewayAttachment:
Type: AWS::EC2::TransitGatewayAttachment
Condition: CreateAttachment
Properties:
SubnetIds:
- !Ref TransitGatewayAttachmentSubnetId
TransitGatewayId: !Ref TransitGatewayId
VpcId: !Ref VpcId
Tags:
- Key: Name
Value: "TransitGateway-attachment"
Options:
DnsSupport: enable
# ------------------------------------------------------------#
# TransitGateway Routing Setting
# ------------------------------------------------------------#
# TransitGateway Route
TransitGatewayRouteTable:
Type: AWS::EC2::TransitGatewayRoute
Condition: CreateAttachment
Properties:
DestinationCidrBlock: !Ref VPCCidr
TransitGatewayAttachmentId: !Ref TransitGatewayAttachment
TransitGatewayRouteTableId: !Ref TransitGatewayRouteTableId
Transit Gateway Attachmentの削除
CloudFormation > スタック > 作成したスタックの更新を選択します。
[現在のテンプレートの仕様]を選択します。
パラメータのCreateTransitGatewayAttachmentを[no]に設定し更新すると削除されます。
削除後に再度作成したい場合は[yes]を設定し更新することで作成されます。
さいごに
LambdaとEventBridgeを組み合わせることで、定期的な作成/削除も可能になります。
VPC内の別サブネットからもTransit Gatewayへルーティングさせたい場合は、別途サブネット側のルートテーブルの設定が必要です。