4
4

More than 5 years have passed since last update.

[AWS]CloudFormationを初めて触ってみたゾ?[VPC構築]

Posted at

最近忙しく書いていませんでした。
現在、AWSソリューションアーキテクトスペシャリストの勉強なんぞしています。
今度サンプル問題の考察なんかもあげようかなーって考えてます。


今日は初めてCloudFormationを触ってみたのでそのメモとしての投稿です。
なんでかって?
AWSの勉強してて、どんなもんかは知ってても実際に触ったことがなかったので
そして自動化ツールは覚えてなんぼだと思ってます。


CloudFormationを始めるために

CloudFormationリファレンス
ほぼここを見れば問題ありません。
AWSCLIで掴んだ雰囲気でなんとかなりました。
cli勉強している時に結構頑張って「jq」やってたのでJSONと仲良くなれてたのが良かった。

CloudFormationを試す流れ

  1. JSONフォーマットのテンプレートを作ります
  2. (とりあえず今回は)ローカルマシンに mytemplate.json として保存します
  3. awscliを用いてコマンドラインから設定を走らせます。

前提としてAWSCLIが使用できる環境が必要になります。
また、IAMで使用するサービスに対して適切に権限が与えられている必要があります。

JSONテンプレート

今回は自作しました。CloudFormationリファレンスのサンプルをすこし弄りつつ、ほぼそのまま使用しています。

今回の構成

プレゼンテーション1.jpg

mytemplate.json

{
  "AWSTemplateFormatVersion" : "2010-09-09",
   "Resources" : {
      "myVPC" : {
         "Type" : "AWS::EC2::VPC",
         "Properties" : {
            "CidrBlock" : "10.0.0.0/16",
          "EnableDnsSupport" : "true ",
          "EnableDnsHostnames" : "true ",
            "InstanceTenancy" : "default",
            "Tags" : [ {"Key" : "Name", "Value" : "myVPC"} ]
          }
      },
    "myInternetGateway" : {
         "Type" : "AWS::EC2::InternetGateway",
         "Properties" : {
            "Tags" : [ {"Key" : "Name", "Value" : "myInternetGateway"}]
          }
    },
    "AttachGateway" : {
        "Type" : "AWS::EC2::VPCGatewayAttachment",
        "Properties" : {
          "VpcId" : { "Ref" : "myVPC" },
          "InternetGatewayId" : { "Ref" : "myInternetGateway" }
   }
},
    "VPC1Subnet01" : {
         "Type" : "AWS::EC2::Subnet",
         "Properties" : {
            "VpcId" : { "Ref" : "myVPC" },
            "Description" : "publicSubnet-a",
            "CidrBlock" : "10.0.1.0/24",
            "AvailabilityZone" : "ap-northeast-1a",
            "Tags" : [ { "Key" : "Name", "Value" : "VPC1Subnet1" } ]
            }
    },
    "VPC1Subnet02" : {
         "Type" : "AWS::EC2::Subnet",
         "Properties" : {
            "VpcId" : { "Ref" : "myVPC" },
            "Description" : "publicSubnet-b",
            "CidrBlock" : "10.0.10.0/24",
            "AvailabilityZone" : "ap-northeast-1c",
            "Tags" : [ { "Key" : "Name", "Value" : "VPC1Subnet2" } ]
            }
    },
    "VPC1Subnet03" : {
         "Type" : "AWS::EC2::Subnet",
         "Properties" : {
            "VpcId" : { "Ref" : "myVPC" },
            "Description" : "privateSubnet-a",
            "CidrBlock" : "10.0.3.0/24",
            "AvailabilityZone" : "ap-northeast-1a",
            "Tags" : [ { "Key" : "Name", "Value" : "VPC1Subnet3" } ]
            }
    },
    "VPC1Subnet04" : {
         "Type" : "AWS::EC2::Subnet",
         "Properties" : {
            "VpcId" : { "Ref" : "myVPC" },
            "Description" : "privateSubnet-b",
            "CidrBlock" : "10.0.30.0/24",
            "AvailabilityZone" : "ap-northeast-1c",
            "Tags" : [ { "Key" : "Name", "Value" : "VPC1Subnet4" } ]
            }
    },
    "publicRouteTable" : {
         "Type" : "AWS::EC2::RouteTable",
         "Properties" : {
            "VpcId" : { "Ref" : "myVPC" },
            "Tags" : [ { "Key" : "Name", "Value" : "VPC1PublicRouteTable" } ]
          }
    },
    "privateRouteTable" : {
         "Type" : "AWS::EC2::RouteTable",
         "Properties" : {
            "VpcId" : { "Ref" : "myVPC" },
            "Tags" : [ { "Key" : "Name", "Value" : "VPC1PrivateRouteTable" } ]
          }
    },
    "publicRoute" : {
         "Type" : "AWS::EC2::Route",
         "Properties" : {
            "RouteTableId" : { "Ref" : "publicRouteTable" },
            "DestinationCidrBlock" : "0.0.0.0/0",
            "GatewayId" : { "Ref" : "myInternetGateway" }
          }
      }
  }
}

ちょこっとだけポイント解説
AWSTemplateFormatVersion:おまじない
Resourcesに作るAWSリソースを書いていく
{ "Ref" : "myVPC" }組み込みの関数。Valueで書いたやつを参照してくれます

いざ実践

・・・っていってもどうやって使うんだよ?
ここを読んでみる

これは本当にAWSCLIなんで安心しますね。

コマンド
aws cloudformation create-stack --stack-name myteststack --template-body "`cat ./mytemplate.json`"

これで実際に動かすとなんとできちゃった!!
初めて触ってみましたがなんとか動きました♪
VPC周りはできたのでその上に乗っける部分を追加で試してみます。


参考

コマンドはこちらの記事を参考にさせていただきました。非常にわかりやすかったです。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4