【AWS】CloudformationでSSH接続可能なEC2作成

1. 使用するサービス

(AWS)

  • Cloudformation
  • InternetGateway
  • SecurityGroup
  • EC2


2. 概要

今回は改めて基本に立ち帰り、SSHで外部接続出来るEC2の作成をCloudformationのテンプレートで説明しようかと思います。
構成は単純で、パブリックサブネットに対してVPCにアタッチされたインターネットゲートウェイにルートテーブルが設定され、かつEC2をパブリックサブネットに配置し、セキュリティグループに22番ポートを空けた物を設定して終わりです。
秘密鍵は作成済の物を使用する設定です。

3. 実装

以下、テンプレートコピペでいけるはずです。

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs EC2
Parameters:

  Project:
    Description: "Project Name"
    Type: String
    Default: test

  Stage:
    Description: "Environment stage"
    Type: String
    Default: dev

Resources:

  VPC:
    Type: AWS::EC2::VPC
    Properties: 
      CidrBlock: 10.0.0.0/16
      Tags:
      - Key: "Name"
        Value: !Sub "${Project}-${Stage}-Vpc"

  InternetSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.0.0/28
      AvailabilityZone: "ap-northeast-1c"
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub "${Project}-${Stage}-internetgateway-subnet"

  InternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: !Sub "${Project}-${Stage}-internetgateway"
          
  InternetRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref VPC
      Tags:
        - Key: Name
          Value: !Sub "${Project}-${Stage}-internetgateway-route"
          
  VPCGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref VPC
      InternetGatewayId: !Ref InternetGateway
      
  InternetSubnetRouteTableAssociation:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref InternetRouteTable
      SubnetId: !Ref InternetSubnet
      
  InternetRoute:
    Type: AWS::EC2::Route
    Properties:
      DestinationCidrBlock: 0.0.0.0/0
      RouteTableId: !Ref InternetRouteTable
      GatewayId: !Ref InternetGateway
    DependsOn: VPCGatewayAttachment

  EC2SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: EC2 SecurityGroup
      VpcId: !Ref VPC
      Tags: 
        - Key: "Name"
          Value: !Sub "${Project}-${Stage}-workbench"
      
  EC2SecurityGroupIngress:
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: !Ref EC2SecurityGroup
      IpProtocol: tcp
      FromPort: '22'
      ToPort: '22'
      CidrIp: 0.0.0.0/0

  EC2:
    Type: AWS::EC2::Instance
    Properties:
      DisableApiTermination: 'false'
      InstanceInitiatedShutdownBehavior: stop
      ImageId: ami-01748a72bed07727c
      InstanceType: t2.micro
      KeyName: testKey #作成済のキーペア名を入力
      Monitoring: 'false'
      Tags:
        - Key: Name
          Value: !Sub "${Project}-${Stage}-workbench" 
      NetworkInterfaces:
        - DeleteOnTermination: 'true'
          Description: Primary network interface
          DeviceIndex: 0
          SubnetId: !Ref InternetSubnet
          GroupSet:
            - !Ref EC2SecurityGroup
            
            
  EC2EIP:
    Type: AWS::EC2::EIP
    Properties:
      InstanceId: !Ref EC2

4. おわりに

以上で、インターネットから接続出来るEC2の作成完了です。
何かインストールしたければ、ユーザデータにパッケージインストールの設定をすればいいと思います。
今回の用途的には外部からの踏み台として使用していて、EC2のセキュリティグループからのMYSQL接続を許可してRDSの接続等に使用していました。