techtsubame’s blog

備忘録であり、何が起きても責任は取りません

AWS Systems Manager準備

参考

pages.awscloud.com


CloudFormation

スタック

作成

メモ

Cloudformation

Parameters

VPCCidr: VPC設定

PublicSubnetCidr: パブリックサブネット設定

PrivateSubnetCidr: プライベートサブネット設定

Resources

VPC: VPCを作成し設定はVPC設定を参照する

PublicSubnet: パブリックサブネットを作成し、サブネットはパブリックサブネット設定を参照、アベイラビリティーは関数によって取得する

PrivateSubnet: プライベートサブネットを作成し、サブネットはプライベートサブネット設定を参照、アベイラビリティーは関数によって取得する

EIP: ElasticIPを作成

InternetGateway: インターネットゲートウェイを作成する

AttachGateway: 作成したInternetGatewayをVPCにアタッチする

  • インターネットゲートウェイの画面の詳細にて状態がAttachedであること
  • VPCのリソースマップで確認できること

NATGateway: EIPの割り当てIDを取得しパブリック接続として設定しサブネットはPublicSubnetを参照する

RouteTableforPublic: VPCを参照しパブリック用のルートテーブルを作成する

RouteTableforPrivate: VPCを参照しプライベート用のルートテーブルを作成する

RouteForPublic: AttachGatewayの作成後に作成したRouteTableforPublicを参照しDestinationCidrBlockで全トラフィックをインターネットゲートウェイに向けるためにInternetGatewayを参照する

RouteForPrivate: NATGatewayの作成後に作成したRouteTableforPrivateを参照しDestinationCidrBlockで全トラフィックをNatゲートウェイに向けるためにNATGatewayを参照する

SubnetRouteTableAssociation1: サブネットとしてPublicSubnet、ルートテーブルとしてRouteTableforPublicを指定しサブネットをルートテーブルに関連付ける

SubnetRouteTableAssociation2: サブネットとしてPrivateSubnet、ルートテーブルとしてRouteTableforPrivateを指定しサブネットをルートテーブルに関連付ける

EC2SecurityGroup: VPC、を指定しセキュリティグループを作成

詳細説明

パラメータ 意味 記載例 記載例説明
EnableDnsSupport VPCAmazon 提供の DNS サーバーを介した DNS 解決策をサポートするかどうかを決定 true サポートする
EnableDnsHostnames VPC がパブリック IP アドレスを持つインスタンスへのパブリック DNS ホスト名の割り当てをサポートするかどうかを決定 true サポートをする
AvailabilityZone アベイラビリティーゾーンを指定 { "Fn::Select" : [ "0", { "Fn::GetAZs" : { "Ref" : "AWS::Region" }}]} CloudFormationを設定するRegionを取得しGetAZs 関数でAZをアルファベット順にリストした配列を返し、select関数でリストの0番目(最初)の要素を返す
MapPublicIpOnLaunch インスタンスがパブリック IPv4 アドレスを受け取るかどうかを true パブリックIPv4アドレスを割り当てる
AllocationId 割り当てIDを指定 { "Fn::GetAtt" : [ "EIP" , "AllocationId"] } EIPの割り当てIDを取得しGetAtt関数で属性の値を取得し設定する
ConnectivityType Natゲートウェイがパブリック接続かプライベート接続のどちらをサポートするかを設定 publlic パブリック接続のサポートをする

cloudformationの定義を試しにデザイナーで表示

cloudformationの定義

{
  
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "This template is for 'AWS Hands-on for Beginners Systems Manager Hands-on'.",

  "Parameters" : {
  
    "VPCCidr" : {
      "Type" : "String",
      "Default" : "10.0.0.0/16",
      "Description" : "VPCCidr"
    },
    "PublicSubnetCidr" : {
      "Type" : "String",
      "Default" : "10.0.0.0/24",
      "Description" : "PublicSubnetCidr"
    },
    "PrivateSubnetCidr" : {
      "Type" : "String",
      "Default" : "10.0.1.0/24",
      "Description" : "PrivateSubnetCidr"
    }
    
  },

  "Resources" : {

    "VPC" : {
      "Type" : "AWS::EC2::VPC",
      "Properties" : {
        "CidrBlock" : { "Ref" : "VPCCidr"},
        "EnableDnsSupport" : true,
        "EnableDnsHostnames" : true,
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-vpc" } ]
      }
    },

    "PublicSubnet" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "CidrBlock" : { "Ref" : "PublicSubnetCidr"},
        "AvailabilityZone" : { "Fn::Select" : [ "0", { "Fn::GetAZs" : { "Ref" : "AWS::Region" }}]},
        "MapPublicIpOnLaunch" : true,
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-public-subnet" } ]
      }
    },
    
    "PrivateSubnet" : {
      "Type" : "AWS::EC2::Subnet",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "CidrBlock" : { "Ref" : "PrivateSubnetCidr"},
        "AvailabilityZone" : { "Fn::Select" : [ "1", { "Fn::GetAZs" : { "Ref" : "AWS::Region" }}]},
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-private-subnet" } ]
      }
    },
    
    "EIP" : {
      "Type" : "AWS::EC2::EIP",
      "Properties" : {
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-eip" } ]
      }
    },
    
    "InternetGateway" : {
      "Type" : "AWS::EC2::InternetGateway",
      "Properties" : {
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-igw" } ]
      }
    },

    "AttachGateway" : {
       "Type" : "AWS::EC2::VPCGatewayAttachment",
       "Properties" : {
         "VpcId" : { "Ref" : "VPC" },
         "InternetGatewayId" : { "Ref" : "InternetGateway" }
       }
    },
    
    "NATGateway" : {
      "Type" : "AWS::EC2::NatGateway",
      "Properties" : {
        "AllocationId" : { "Fn::GetAtt" : [ "EIP" , "AllocationId"] },
        "ConnectivityType" : "public",
        "SubnetId" : { "Ref" : "PublicSubnet" }
      }
    },

    "RouteTableforPublic" : {
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
        "VpcId" : {"Ref" : "VPC"},
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-public-route-table" } ]
      }
    },
    
    "RouteTableforPrivate" : {
      "Type" : "AWS::EC2::RouteTable",
      "Properties" : {
        "VpcId" : {"Ref" : "VPC"},
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-private-route-table" } ]
      }
    },
    
    "RouteForPublic" : {
      "Type" : "AWS::EC2::Route",
      "DependsOn" : "AttachGateway",
      "Properties" : {
        "RouteTableId" : { "Ref" : "RouteTableforPublic" },
        "DestinationCidrBlock" : "0.0.0.0/0",
        "GatewayId" : { "Ref" : "InternetGateway" }
      }
    },

    "RouteForPrivate" : {
      "Type" : "AWS::EC2::Route",
      "DependsOn" : "NATGateway",
      "Properties" : {
        "RouteTableId" : { "Ref" : "RouteTableforPrivate" },
        "DestinationCidrBlock" : "0.0.0.0/0",
        "NatGatewayId" : { "Ref" : "NATGateway" }
      }
    },

    "SubnetRouteTableAssociation1" : {
      "Type" : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties" : {
        "SubnetId" : { "Ref" : "PublicSubnet" },
        "RouteTableId" : { "Ref" : "RouteTableforPublic" }
      }
    },
      
    "SubnetRouteTableAssociation2" : {
      "Type" : "AWS::EC2::SubnetRouteTableAssociation",
      "Properties" : {
        "SubnetId" : { "Ref" : "PrivateSubnet" },
        "RouteTableId" : { "Ref" : "RouteTableforPrivate" }
      }
    },

    "EC2SecurityGroup" : {
      "Type" : "AWS::EC2::SecurityGroup",
      "Properties" : {
        "VpcId" : { "Ref" : "VPC" },
        "GroupDescription" : "No Inbound Rule Security Group",
        "GroupName" : "h4b-ec2-sg",
        "Tags" : [ {"Key" : "Name", "Value" : "h4b-ec2-sg" } ]
      }
    }


  }

}