cloud Formationになれる目的で、EC2インスタンスを立ち上げる所までを設定してみた。
テンプレートを作る(My-Network.yml)
AWSTemplateFormatVersion: '2010-09-09'
Description: "Create My Network"
Resources:
# ------------------------------------------------------------#
# VPCの作成
# ------------------------------------------------------------#
MyVPC:
Type: "AWS::EC2::VPC"
Properties:
CidrBlock: 10.0.0.0/16
EnableDnsSupport: 'true'
EnableDnsHostnames: 'true'
Tags:
- Key: Name
Value: -Vpc
# ------------------------------------------------------------#
# Subnetの作成
# ------------------------------------------------------------#
MySubnet01:
Type: "AWS::EC2::Subnet"
Properties:
VpcId: !Ref MyVPC
AvailabilityZone: ap-northeast-1a
CidrBlock: 10.0.0.0/24
Tags:
- Key: Name
Value: -Vpc1-Subnet01
#=================================
# インターネットゲートウェイの作成
#=================================
myInternetGateway:
Type: "AWS::EC2::InternetGateway"
Properties:
Tags:
- Key: Name
Value: techpit
myAttachGateway:
Type: "AWS::EC2::VPCGatewayAttachment"
Properties:
InternetGatewayId: !Ref myInternetGateway
VpcId: !Ref MyVPC
#=================================
# ルートテーブルの作成
#=================================
myRouteTable01:
Type: "AWS::EC2::RouteTable"
Properties:
Tags:
- Key: Name
Value: techpit01
VpcId: !Ref MyVPC
myRouteTableAssociation01a:
Type: "AWS::EC2::SubnetRouteTableAssociation"
Properties:
RouteTableId: !Ref myRouteTable01
SubnetId: !Ref MySubnet01
myRoute01:
Type: "AWS::EC2::Route"
Properties:
DestinationCidrBlock: 0.0.0.0/0
GatewayId: !Ref myInternetGateway
RouteTableId: !Ref myRouteTable01
#=================================
# Security Groupの作成
#=================================
SSHSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable SSH access via port 22
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 22
ToPort: 22
CidrIp: 0.0.0.0/0 // 許可したいIPアドレスを指定する事
HTTPSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: HTTP access port 80
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: 80
ToPort: 80
CidrIp: 0.0.0.0/0 // 許可したいIPアドレスを指定する事
#=================================
# キーペアの作成とEC2インスタンスの作成
#=================================
NewKeyPair:
Type: 'AWS::EC2::KeyPair'
Properties:
KeyName: MyKeyPair
Ec2Instance:
Type: 'AWS::EC2::Instance'
Properties:
ImageId: ami-XXXXXXXXXX // 自身のAMI ID
SubnetId: !Ref MySubnet01
KeyName: !Ref NewKeyPair
「テンプレートファイルのアップロード」を選択。
上記のテンプレートファイルをアップロードし、「次へ」を選択
スタック名をつけて「次へ」を選択。
そのまま進んでスタック作成を完了させる。
スタック作成が完了、EC2の立ち上げに必要な下記が作成されたことが確認できた。
VPC(/16)
サブネット(/24)
インターネットゲートウェイ
ルートテーブル
セキュリティグループ(SSH:22番を開放)
キーペア
EC2インスタンス(Amazon Linux 2)