お世話になります、株式会社エイトハンドレッド・テクノロジー本部の細江と申します。本記事では、AWS による基盤構築の基本とも言える VPC とサブネットを作成する、CloudFormation YAML テンプレートをご紹介します。
構成図
東京リージョンのアベイラビリティーゾーン 1a と 1c それぞれにパブリックとプライベートサブネットを作成します。
YAML テンプレート
今回ご説明するのは以下の YAML テンプレートになります。
【参考】 AWS CloudFormation VPC テンプレート
AWSTemplateFormatVersion: 2010-09-09 Parameters: SharedName: Type: String Default: hatena-handson Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 172.16.0.0/26 EnableDnsHostnames: true Tags: - Key: Name Value: !Sub ${SharedName}-vpc InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: !Sub ${SharedName}-igw InternetGatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref VPC InternetGatewayId: !Ref InternetGateway PublicSubnet1a: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.0/28 AvailabilityZone: ap-northeast-1a MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${SharedName}-public-1a-subnet PublicSubnet1c: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.16/28 AvailabilityZone: ap-northeast-1c MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${SharedName}-public-1c-subnet PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Sub ${SharedName}-public-rtb PublicRoute: Type: AWS::EC2::Route DependsOn: InternetGatewayAttachment Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway PublicSubnetRouteTableAssociation1a: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet1a RouteTableId: !Ref PublicRouteTable PublicSubnetRouteTableAssociation1c: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet1c RouteTableId: !Ref PublicRouteTable PrivateSubnet1a: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.32/28 AvailabilityZone: ap-northeast-1a MapPublicIpOnLaunch: false Tags: - Key: Name Value: !Sub ${SharedName}-private-1a-subnet PrivateSubnet1c: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.48/28 AvailabilityZone: ap-northeast-1c MapPublicIpOnLaunch: false Tags: - Key: Name Value: !Sub ${SharedName}-private-1c-subnet PrivateRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Sub ${SharedName}-private-rtb PrivateSubnetRouteTableAssociation1a: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PrivateSubnet1a RouteTableId: !Ref PrivateRouteTable PrivateSubnetRouteTableAssociation1c: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PrivateSubnet1c RouteTableId: !Ref PrivateRouteTable
Parameters
各リソースの Name タグ等に使用する共通の名称をパラメータとして保持します。
Parameters: SharedName: Type: String Default: hatena-handson
SharedName
というパラメータを宣言し、デフォルトは hatena-handson としています。スタック作成・更新時に任意の文字列に変更できます。
Resources
VPC
Resources: VPC: Type: AWS::EC2::VPC Properties: CidrBlock: 172.16.0.0/26 EnableDnsHostnames: true Tags: - Key: Name Value: !Sub ${SharedName}-vpc
CidrBlock
今回はサブネットの最小サイズ /28
( IP アドレス数 16 )で作成するため、VPCは /26
( IP アドレス数 16 × 4 = 64 )とします。
【参考】 VPC CIDR ブロック, Amazon VPC のよくある質問
EnableDnsHostnames
DNS ホスト名を有効化する場合は true
にします。(デフォルトは false
)
【参考】 VPC の DNS 属性 > VPC 内の DNS 属性
Tags
Name タグを設定しています。Value の !Sub
によって SharedName パラメータを指定しています。
InternetGateway
InternetGateway: Type: AWS::EC2::InternetGateway Properties: Tags: - Key: Name Value: !Sub ${SharedName}-igw InternetGatewayAttachment: Type: AWS::EC2::VPCGatewayAttachment Properties: VpcId: !Ref VPC InternetGatewayId: !Ref InternetGateway
InternetGateway を作成し、VPC にアタッチ。
InternetGatewayAttachment > Properties > VpcId, InternetGatewayId の !Ref
によって、本テンプレートで作成している VPC と InternetGateway リソースを参照しています。
PublicSubnet1a/1c
PublicSubnet1a: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.0/28 AvailabilityZone: ap-northeast-1a MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${SharedName}-public-1a-subnet PublicSubnet1c: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.16/28 AvailabilityZone: ap-northeast-1c MapPublicIpOnLaunch: true Tags: - Key: Name Value: !Sub ${SharedName}-public-1c-subnet
CidrBlock
サブネットの最小サイズ /28
( IP アドレス数 16 )で作成。
AvailabilityZone
東京リージョンのアベイラビリティーゾーン 1a と 1c に作成します。
MapPublicIpOnLaunch
当該サブネット内で起動されたインスタンスのパブリック IP アドレスを有効化する場合は true
にします。(デフォルトは false
)
PublicRouteTable
PublicRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Sub ${SharedName}-public-rtb PublicRoute: Type: AWS::EC2::Route DependsOn: InternetGatewayAttachment Properties: RouteTableId: !Ref PublicRouteTable DestinationCidrBlock: 0.0.0.0/0 GatewayId: !Ref InternetGateway
PublicRouteTable でルートテーブルを作成し、PublicRoute でテーブル内のルートを指定。
DependsOn
InternetGatewayAttachment リソース作成の後に PublicRoute リソースを作成(ルーティング設定)する依存関係を明示しています。
【参考】 AWS CloudFormation > DependsOn 属性
RouteTableId
当該ルートを所有するルートテーブルを指定。
DestinationCidrBlock
当該ルートの送信先。0.0.0.0/0
と記載することで、インターネットへ向かうトラフィックを指定します。
GatewayId
当該ルートのターゲット。InternetGateway リソースを参照しています。
PublicSubnetRouteTableAssociation1a/1c
PublicSubnetRouteTableAssociation1a: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet1a RouteTableId: !Ref PublicRouteTable PublicSubnetRouteTableAssociation1c: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PublicSubnet1c RouteTableId: !Ref PublicRouteTable
PublicSubnet1a/1c 各サブネットを PublicRouteTable リソース(ルートテーブル)に関連付けます。
PrivateSubnet1a/1c
PrivateSubnet1a: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.32/28 AvailabilityZone: ap-northeast-1a MapPublicIpOnLaunch: false Tags: - Key: Name Value: !Sub ${SharedName}-private-1a-subnet PrivateSubnet1c: Type: AWS::EC2::Subnet Properties: VpcId: !Ref VPC CidrBlock: 172.16.0.48/28 AvailabilityZone: ap-northeast-1c MapPublicIpOnLaunch: false Tags: - Key: Name Value: !Sub ${SharedName}-private-1c-subnet
プライベートサブネットでは、パブリック IP アドレスは有効化しないため、MapPublicIpOnLaunch を false
にします。
PrivateRouteTable
PrivateRouteTable: Type: AWS::EC2::RouteTable Properties: VpcId: !Ref VPC Tags: - Key: Name Value: !Sub ${SharedName}-private-rtb
プライベートサブネット用のルートテーブルでは、インターネットゲートウェイをターゲットとするルートは作成しません。
PrivateSubnetRouteTableAssociation1a/1c
PrivateSubnetRouteTableAssociation1a: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PrivateSubnet1a RouteTableId: !Ref PrivateRouteTable PrivateSubnetRouteTableAssociation1c: Type: AWS::EC2::SubnetRouteTableAssociation Properties: SubnetId: !Ref PrivateSubnet1c RouteTableId: !Ref PrivateRouteTable
パブリックサブネットと同様に、PrivateSubnet1a/1c 各サブネットを PrivateRouteTable リソース(ルートテーブル)に関連付けます。
最後に
- 簡易的な検証環境を構築する際にサクッと VPC とサブネットを作成できる
- ややこしい設定項目を可能な限り減らし、必要最低限のプロパティしか記載していない
そんな CloudFormation テンプレートがあったら便利ではないかと思い、作成しました。本記事がどなたかの AWS 活用のお役に立ちますように!