ページ

2015年4月20日

CloudformationでVPCを作ってみる練習、1回目:VPCの生成とリソースタイプ


最初の一歩は、VPCをcloudformationで作成してみます。






CloudformationでVPCをつくってみます。


一番簡単なVPCだけをつくるJSONファイル

シンプルにVPCを作るだけのJSONファイルです。 

Parameterは、CloudFormationを実行する際に引数として渡すことができるようにするための設定です。

VPCのネットワークアドレスを指定するとVPCをつくります。


{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "AWS CloudFormation Sample VPC",

  "Parameters" : {

    "VPCCIDR" : {
      "Description" : "VPC CIDIR",
      "Type" : "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default" : "0.0.0.0/0",
      "AllowedPattern" : "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription" : "must be a valid CIDR range of the form x.x.x.x/x."
    }
  },

  "Resources" : {

    "VPC" : {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : { "Ref" : "VPCCIDR" },
        "EnableDnsSupport" : "true",
        "EnableDnsHostnames" : "true",
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    }
  }
  
}

VPCはできるようになりましたけど、面白くないですからちょっと追加します。


利用できるインスタンスタイプを制限します


VPC内にEC2インスタンスを生成する際にいろいろなタイプができると思いがけず作成してしまうことがあるかもしれません。

そこで、間違って時間単価が高いインスタンスが作成できないようにしておきます。

実験ですから、t2.micro,t2.small,t2.mediumの3つだけのインスタンス・タイプしかできないようにするにはParameterを部分を追加すると制限がかけられます。

こんな感じで追加します:

  "Parameters" : {

    "InstanceType" : {
      "Description" : "WebServer EC2 instance type",
      "Type" : "String",
      "Default" : "m2.micro",
      "AllowedValues" : [ "t2.micro", "t2.small", "t2.medium"],
      "ConstraintDescription" : "must be a valid EC2 instance type."
    },

ルーティング・テーブル、インターネット・ゲートウェイ、サブネットを追加


AWSのマネジメント・コンソールでVPCのところをみると、サブネットやネットワークACLなどがありますのでネットワーク構成を追加していきましょう。


先ほどのインスタンス・タイプを制限することに加えて、サブネットを作成、インターネットゲートウェイの作成とアタッチ、ルーティング・テーブルの作成とアタッチも追加します。

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "AWS CloudFormation Sample",

  "Parameters" : {

    "InstanceType" : {
      "Description" : "EC2 instance type",
      "Type" : "String",
      "Default" : "m2.micro",
      "AllowedValues" : [ "t2.micro", "t2.small", "t2.medium"],
      "ConstraintDescription" : "must be a valid EC2 instance type."
    },

    "VPCCIDR" : {
      "Description" : "VPC CIDIR",
      "Type" : "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default" : "0.0.0.0/0",
      "AllowedPattern" : "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription" : "must be a valid CIDR range of the form x.x.x.x/x."
    },
    
    "SubnetCIDR" : {
      "Description" : "Subnet CIDIR",
      "Type" : "String",
      "MinLength": "9",
      "MaxLength": "18",
      "Default" : "0.0.0.0/0",
      "AllowedPattern" : "(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/(\\d{1,2})",
      "ConstraintDescription" : "must be a valid CIDR range of the form x.x.x.x/x."
    }
  },

  "Resources" : {

    "VPC" : {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : { "Ref" : "VPCCIDR" },
        "EnableDnsSupport" : "true",
        "EnableDnsHostnames" : "true",
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "PublicSubnet" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "CidrBlock" : { "Ref" : "SubnetCIDR" },
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "InternetGateway" : {
      "Type" : "AWS::EC2::InternetGateway",
      "Properties" : {
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "GatewayToInternet" : {
       "Type" : "AWS::EC2::VPCGatewayAttachment",
       "Properties" : {
         "VpcId" : { "Ref" : "VPC" },
         "InternetGatewayId" : { "Ref" : "InternetGateway" }
       }
    },

    "PublicRouteTable" : {
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "PublicRoute" : {
      "Type" : "AWS::EC2::Route",
      "DependsOn" : "GatewayToInternet",
      "Properties" : {
        "RouteTableId" : { "Ref" : "PublicRouteTable" },
        "DestinationCidrBlock" : "0.0.0.0/0",
        "GatewayId" : { "Ref" : "InternetGateway" }
      }
    },

    "PublicSubnetRouteTableAssociation" : {
      "Type" : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties" : {
        "SubnetId" : { "Ref" : "PublicSubnet" },
        "RouteTableId" : { "Ref" : "PublicRouteTable" }
      }
    }
  }
}

作りたいのは、複数のサブネットを作りたいので


どうしようかと考えて、「ネットワークは組み込んじゃえ!」とうことで選ぶのは「テスト」と「ターゲット」でVPCを作るようにしてみました。

キーワードを選ぶと、ネットワークを組み立てるようにするのは・・・Mappingを使えばいいので追加してみました

ひとまず、Mappingの動きだけわかるようにインスタンス・タイプははずしています

サブネットが複数になると、ルーティング・テーブルも複数必要になりますのでこの後追加していきます。

サブネットは、プライベート・サブネットとパブリック・サブネットの2つを作成します。


{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "AWS CloudFormation Sample Template Simple VPC.",

  "Parameters" : {
    "TargetSystem" : {
      "Type" : "String",
      "Default" : "Test",
      "AllowedValues" : [ "Test", "Target" ],
      "ConstraintDescription" : ""
    }
   },

  "Mappings" : {
     "VPCConfig" : {
       "VPCCidr" : { "Test" : "10.181.0.0/16", "Target" : "10.81.0.0/16" }
     },
    "SubnetConfig" : {
      "PublicCidr"     : { "Test" : "10.181.254.0/24", "Target" : "10.81.254.0/24" },
      "PrivateCidr"    : { "Test" : "10.181.80.0/24" , "Target" : "10.81.80.0/24"  }
    }
  },

  "Resources" : {
    "VPC" : {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : { "Fn::FindInMap" : [ "VPCConfig", "VPCCidr", { "Ref" : "TargetSystem" } ] },
        "EnableDnsSupport" : "true",
        "EnableDnsHostnames" : "true",
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "PublicSubnet" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "CidrBlock" : { "Fn::FindInMap" : [ "SubnetConfig", "PublicCidr", { "Ref" : "TargetSystem" } ] },
        "Tags" : [
          { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
          { "Key" : "Network", "Value" : "Public" }
        ]
      }
    },

    "PrivateSubnet" : {
       "Type" : "AWS::EC2::Subnet",
       "Properties" : {
         "VpcId" : { "Ref" : "VPC" },
         "CidrBlock" : { "Fn::FindInMap" : [ "SubnetConfig", "PrivateCidr", { "Ref" : "TargetSystem" } ] },
         "Tags" : [
           { "Key" : "Application", "Value" : { "Ref" : "AWS::StackId" } },
           { "Key" : "Network", "Value" : "Private" }
         ]
       }
    }
  },

  "Outputs" : {
    "VPC" : {
      "Description" : "VPC CIDR",
      "Value" : { "Fn::FindInMap" : [ "VPCConfig", "VPCCidr", { "Ref" : "TargetSystem" } ] }
    },
    "Public"  : {
      "Description" : "Public Subnet CIDR",
      "Value" : { "Fn::FindInMap" : [ "SubnetConfig", "PublicCidr", { "Ref" : "TargetSystem" } ] }
    },
    "Private" : {
      "Description" : "Private Subnet CIDR",
      "Value" : { "Fn::FindInMap" : [ "SubnetConfig", "PrivateCidr", { "Ref" : "TargetSystem" } ] }
    }
  }
}


今回はここまで・・・