0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CloudFormationを用いて、EC2インスタンスの立ち上がり迄を確認する。

Last updated at Posted at 2025-05-05

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

image.png
「スタックの作成」を押下

image.png
「テンプレートファイルのアップロード」を選択。
上記のテンプレートファイルをアップロードし、「次へ」を選択

image.png
スタック名をつけて「次へ」を選択。
そのまま進んでスタック作成を完了させる。

image.png
スタック作成が完了、EC2の立ち上げに必要な下記が作成されたことが確認できた。

VPC(/16)
サブネット(/24)
インターネットゲートウェイ
ルートテーブル
セキュリティグループ(SSH:22番を開放)
キーペア
EC2インスタンス(Amazon Linux 2)

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?