Cloud Formation を使って VPC~EC2 まで構築する

手順

Cloud Formation で VPC ~ EC2 を構築してみます。
「~」としていますが、中身は単純でパブリックサブネット内に EC2 を立てて、インターネットにつなげるようにしているだけです。

  1. テンプレートを作成する。
  2. スタックを実行する。

参考

前提

  • キーペアが作成してある事。

テンプレートを作成する

yaml 形式でテンプレートを記述してみます。

  1. hoge.yaml とか適当な名前でファイルを作成する。
  2. 作成したファイルに次の記述を記載する。
AWSTemplateFormatVersion: "2010-09-09"
Description: "CloudFormation Template Test"
# Setting key and IP
Parameters:
  KeyName:
    Description: The EC2 Key Pair to allow SSH access to the instance
    Type: "AWS::EC2::KeyPair::KeyName"
  MyIP:
    Description: IP address allowed to access EC2
    Type: String
Resources:
  # VPC
  HogeVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: "10.0.0.0/16"
      EnableDnsSupport: "true"
      EnableDnsHostnames: "true"
      InstanceTenancy: "default"
      Tags:
        - Key: Name
          Value: "hoge-vpc"
  # InternetGateway
  HogeInternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: "hoge-igw"
  HogeAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref HogeVPC
      InternetGatewayId: !Ref HogeInternetGateway
  # Subnet
  HogePrivateSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: "ap-northeast-1a"
      CidrBlock: "10.0.0.0/24"
      VpcId: !Ref HogeVPC
      Tags:
        - Key: Name
          Value: "hoge-private-subnet"
  HogePublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      AvailabilityZone: "ap-northeast-1c"
      CidrBlock: "10.0.1.0/24"
      VpcId: !Ref HogeVPC
      Tags:
        - Key: Name
          Value: "hoge-public-subnet"
  # RouteTable
  HogePublicRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref HogeVPC
      Tags:
        - Key: Name
          Value: !Sub hoge-public-routetable
  HogePublicRoute:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref HogePublicRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref HogeInternetGateway
  # Subnet to attach
  HogePublicSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      SubnetId: !Ref HogePublicSubnet
      RouteTableId: !Ref HogePublicRouteTable
  # EC2
  EC2:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-00d101850e971728d
      KeyName: !Ref KeyName
      InstanceType: t2.micro
      NetworkInterfaces: # assign public IP
        - AssociatePublicIpAddress: "true"
          DeviceIndex: "0"
          SubnetId: !Ref HogePublicSubnet
          GroupSet:
            - !Ref EC2SG
      UserData: !Base64 | # coding commands what you want
        #!/bin/bash
        sudo yum install -y git
      Tags:
        - Key: Name
          Value: hogeEC2
  # SecurityGroup
  EC2SG:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupName: hoge-sg
      GroupDescription: Allow SSH and HTTP access only MyIP
      VpcId: !Ref HogeVPC
      SecurityGroupIngress:
        # http
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: !Ref MyIP
        # ssh
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: !Ref MyIP
Outputs:
  EC2PublicIP:
    Value: !GetAtt EC2.PublicIp
    Description: Public IP of EC2 instance

スタックを実行する

  1. Cloud Formation と検索して「Cloud Formation」をクリックする。
  2. 「スタックの作成」をクリックする。
  3. 「テンプレートの準備完了」を選択する。
  4. 「テンプレートファイルのアップロード」を選択する。
  5. 「ファイルの選択」をクリックし、作成したファイルをアップロードする。
  6. 「次へ」をクリックする。
  7. 「スタックの名前」に「hoge-stack」と入力する。
  8. 「KeyName」で作成済みのキーを選択する。
  9. 「MyIP」に SSH を許可する IP を入力する。
  10. 「次へ」をクリックする。
  11. 「タグ」で、キーを「Name」値を「hoge-stack」と入力する。
  12. 「次へ」をクリックする。
  13. 確認したら「スタックの作成」をクリックする。

SSH する

  1. 「出力」タブをクリックし「値」に記載がある IP を確認する。
  2. 下記のコードを ssh の config に追加して接続する。
Host hogeEC2
    User ec2-user
    HostName [確認したIP]
    Port 22
    IdentityFile ~/.ssh/[作成したキーペアの pem ファイル名]
YouTube