[AWS] Multi-AZ構成時のVPC CloudFormation Template

Multi-AZのVPC環境を構成する、CloudFormationのTemplateを作成しました。
以下の順でMulti-AZ構成で必要となる典型的なNW環境が構築されます。

・VPC作成
・IGW作成・アタッチ
・Subnets作成
・NAT Gatewayの作成
・RouteTable作成

構成
====

![vpc.png](https://qiita-image-store.s3.amazonaws.com/0/111300/6ea2a1d1-ae8f-42e8-d06e-2d3601376fd7.png)

VPCのCIDR、AZは構築時のParameterにて任意に設定可能です。
また、任意のリージョンで利用可能にしています。

使い方
====

・下記にあるTemplateを保存し、CloudFormationの"CreateStack"からスタックの作成を開始
・Parametersで以下を入力
　- Multi-AZで使用するAZを2つ選択（AZ1/AZ2）
　- VPCCIDRに作成したいネットワークアドレスを入力（10.* または 172.16 〜 172.31 または 192.168）
・作成完了まで待つ（NatGatewayで少々時間がかかります）

![スクリーンショット 2016-05-24 23.17.39.png](https://qiita-image-store.s3.amazonaws.com/0/111300/cae40fe3-4a5b-0cc5-98a4-4ebad83d274d.png)

Template
====

````
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "VPC Template For Multi-AZ",
  "Parameters": {
    "AZ1" : {
        "Description" : "input Availability Zone 1",
        "Type"        : "AWS::EC2::AvailabilityZone::Name"
    },
    "AZ2" : {
        "Description" : "input Availability Zone 2",
        "Type"        : "AWS::EC2::AvailabilityZone::Name"
    },
    "VPCCIDR": {
      "AllowedPattern" : "^(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])",
      "Default"        : "10.0",
      "Description"    : "VPC CIDR (*.*.0.0/16)",
      "Type"           : "String"
    }
  },
  "Resources": {
    "vpc00": {
      "Type"       : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock"          : { "Fn::Join": [ "", [ { "Ref": "VPCCIDR" }, ".0.0/16" ] ] },
        "InstanceTenancy"    : "default",
        "EnableDnsSupport"   : "true",
        "EnableDnsHostnames" : "false",
        "Tags"               : [ { "Key": "Name", "Value": "TestVPC" } ]
      }
    },
    "eip0001": {
      "Type"       : "AWS::EC2::EIP",
      "Properties" : { "Domain" : "vpc" }
    },
    "eip0002": {
      "Type"       : "AWS::EC2::EIP",
      "Properties" : { "Domain" : "vpc" }
    },
    "subnetPub1": {
      "Type"       : "AWS::EC2::Subnet",
      "Properties" : {
        "CidrBlock"        : { "Fn::Join": [ "", [ { "Ref": "VPCCIDR" }, ".0.0/24" ] ] },
        "AvailabilityZone" : { "Ref" : "AZ1" },
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Public-Subnet-1" } ]
      }
    },
    "subnetPub2": {
      "Type"       : "AWS::EC2::Subnet",
      "Properties" : {
        "CidrBlock"        : { "Fn::Join": [ "", [ { "Ref": "VPCCIDR" }, ".1.0/24" ] ] },
        "AvailabilityZone" : { "Ref" : "AZ2" },
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Public-Subnet-2" } ]
      }
    },
    "subnetPrv1": {
      "Type"       : "AWS::EC2::Subnet",
      "Properties" : {
        "CidrBlock"        : { "Fn::Join": [ "", [ { "Ref": "VPCCIDR" }, ".2.0/24" ] ] },
        "AvailabilityZone" : { "Ref" : "AZ1" },
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Private-Subnet-1" } ]
      }
    },
    "subnetPrv2": {
      "Type"       : "AWS::EC2::Subnet",
      "Properties" : {
        "CidrBlock"        : { "Fn::Join": [ "", [ { "Ref": "VPCCIDR" }, ".3.0/24" ] ] },
        "AvailabilityZone" : { "Ref" : "AZ2" },
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Private-Subnet-2" } ]
      }
    },
    "Nat1": {
      "Type"       : "AWS::EC2::NatGateway",
      "Properties" : {
        "AllocationId"     : { "Fn::GetAtt" : ["eip0001", "AllocationId"] },
        "SubnetId"         : { "Ref" : "subnetPub1" }
      },
      "DependsOn"  : "eip0001"
    },
    "Nat2": {
      "Type"       : "AWS::EC2::NatGateway",
      "Properties" : {
        "AllocationId"     : { "Fn::GetAtt" : ["eip0002", "AllocationId"] },
        "SubnetId"         : { "Ref" : "subnetPub2" }
      },
      "DependsOn"  : "eip0002"
    },
    "IGW": {
      "Type"       : "AWS::EC2::InternetGateway",
      "Properties" : {
        "Tags"             : [ { "Key": "Name", "Value": "Test-IG" } ]
      }
    },
    "RouteTablePub1": {
      "Type"           : "AWS::EC2::RouteTable",
      "Properties"     : {
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Public-RT-A" } ]
      }
    },
    "RouteTablePub2": {
      "Type"           : "AWS::EC2::RouteTable",
      "Properties"     : {
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Public-RT-C" } ]
      }
    },
    "RouteTablePrv1": {
      "Type"           : "AWS::EC2::RouteTable",
      "Properties"     : {
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Private-RT-A" } ]
      }
    },
    "RouteTablePrv2": {
      "Type"           : "AWS::EC2::RouteTable",
      "Properties"     : {
        "VpcId"            : { "Ref": "vpc00" },
        "Tags"             : [ { "Key": "Name", "Value": "Private-RT-C" } ]
      }
    },
    "gw1": {
      "Type"           : "AWS::EC2::VPCGatewayAttachment",
      "Properties"     : {
        "VpcId"             : { "Ref": "vpc00" },
        "InternetGatewayId" : { "Ref": "IGW" }
      }
    },
    "subnetRoutePub1": {
      "Type"           : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties"     : {
        "RouteTableId"     : { "Ref": "RouteTablePub1" },
        "SubnetId"         : { "Ref": "subnetPub1" }
      }
    },
    "subnetRoutePub2": {
      "Type"           : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties"     : {
        "RouteTableId"     : { "Ref": "RouteTablePub2" },
        "SubnetId"         : { "Ref": "subnetPub2" }
      }
    },
    "subnetRoutePrv1": {
      "Type"           : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties"     : {
        "RouteTableId"     : { "Ref": "RouteTablePrv1" },
        "SubnetId"         : { "Ref": "subnetPrv1" }
      }
    },
    "subnetRoutePrv2": {
      "Type"           : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties"     : {
        "RouteTableId"     : { "Ref": "RouteTablePrv2" },
        "SubnetId"         : { "Ref": "subnetPrv2" }
      }
    },
    "routePub101": {
      "Type"           : "AWS::EC2::Route",
      "Properties"     : {
        "DestinationCidrBlock"  : "0.0.0.0/0",
        "RouteTableId"          : { "Ref": "RouteTablePub1" },
        "GatewayId"             : { "Ref": "IGW" }
      },
      "DependsOn"      : "gw1"
    },
    "routePub201": {
      "Type"           : "AWS::EC2::Route",
      "Properties"     : {
        "DestinationCidrBlock"  : "0.0.0.0/0",
        "RouteTableId"          : { "Ref": "RouteTablePub2" },
        "GatewayId"             : { "Ref": "IGW" }
      },
      "DependsOn"      : "gw1"
    },
    "routePrv101": {
      "Type"           : "AWS::EC2::Route",
      "Properties"     : {
        "DestinationCidrBlock"  : "0.0.0.0/0",
        "RouteTableId"          : { "Ref": "RouteTablePrv1" },
        "NatGatewayId"          : { "Ref" : "Nat1" }
      },
      "DependsOn"      : [ "Nat1" , "subnetRoutePrv1" ]
    },
    "routePrv201": {
      "Type"           : "AWS::EC2::Route",
      "Properties"     : {
        "DestinationCidrBlock"  : "0.0.0.0/0",
        "RouteTableId"          : { "Ref": "RouteTablePrv2" },
        "NatGatewayId"          : { "Ref" : "Nat2" }
      },
      "DependsOn"      : [ "Nat2" , "subnetRoutePrv2" ]
    },
    "dchpOpt": {
      "Type"           : "AWS::EC2::DHCPOptions",
      "Properties"     : {
        "DomainName"           : "ec2.internal.com",
        "DomainNameServers"    : [ "AmazonProvidedDNS"]
      }
    },
    "dchpAssoc": {
      "Type"           : "AWS::EC2::VPCDHCPOptionsAssociation",
      "Properties"     : {
        "VpcId"            : { "Ref": "vpc00" },
        "DhcpOptionsId"    : {"Ref" : "dchpOpt" }
      },
      "DependsOn"      : "dchpOpt"
    }
  }
}
````