1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

AWS_Cloudformationで環境構築してみた②

Last updated at Posted at 2021-03-14

前回の記事
https://qiita.com/shinichi_yoshioka/items/487662c0749fe7b1ca8c

前回の続きで、Networkレイヤは作成済。
以下のシンプルな構成で、今回はEC2インスタンスとセキュリティグループをCloudFormationで作成します。
CloudFormation構成図.png

テンプレートの作成

①CloudFormationのymlを作成するにあたって、CloudFormationのRain(CLI実行ツール)を使ってテンプレート(EC2用ymlとセキュリティグループ用yml)を出力する。
Rainの使い方の詳細は以下のURLを参照。
https://dev.classmethod.jp/articles/aws-cloudformation-rain/

②出力されたymlそれぞれをクロススタック参照するようにパラメータを入れる。
Secutiry.yml

AWSTemplateFormatVersion: "2010-09-09"

Description: Template generated by rain

Resources:
  MySecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH from ALL
      SecurityGroupEgress:
        - Description: Outbound
          CidrIp: 0.0.0.0/0
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          #CidrIpv6: CHANGEME # Optional
          #DestinationPrefixListId: CHANGEME # Optional
          #DestinationSecurityGroupId: CHANGEME # Optional

      SecurityGroupIngress:
        - Description: Inbound
          CidrIp: 0.0.0.0/0
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          #CidrIpv6: CHANGEME # Optional
          #SourcePrefixListId: CHANGEME # Optional
          #SourceSecurityGroupId: CHANGEME # Optional
          #SourceSecurityGroupName: CHANGEME # Optional
          #SourceSecurityGroupOwnerId: CHANGEME # Optional
          
      Tags:
        - Key: Name
          Value: TEST-SG
      VpcId: vpc-0af10c6c61fb430cc

Outputs:
  MySecurityGroupGroupId:
    Value: !GetAtt MySecurityGroup.GroupId
    Export:
      Name: TEST-SGfromCF

パラメータメモ
SecurityGroupEgress: セキュリティグループのアウトバウンド
SecurityGroupIngress: セキュリティグループのインバウンド
Outputs: GetAtt関数を用いてMySecurityGroupのGroupIdを「TEST-SGfromCF」の名前タグをつけてエクスポートする

Application.yml (EC2インスタンス作成)

AWSTemplateFormatVersion: "2010-09-09"

Description: Template generated by rain

Resources:
  MyInstance:
    Type: AWS::EC2::Instance
    Properties:
      KeyName: Secret-key
      AvailabilityZone: ap-northeast-1a
      ImageId: ami-0f27d081df46f326c
      InstanceType: t2.micro
      NetworkInterfaces:
        - AssociatePublicIpAddress: true
          #DeleteOnTermination: false # Optional
          #Description: CHANGEME # Optional
          DeviceIndex: "0"
          GroupSet:
            - !ImportValue TEST-SGfromCF
          SubnetId: subnet-068305cddc05d01bd
      #SecurityGroupIds:
        #- !ImportValue TEST-SGfromCF
      #SecurityGroups:
        #- !ImportValue TEST-SGfromCF
      #SubnetId: subnet-068305cddc05d01bd
      Tags:
        - Key: Name
          Value: TEST-EC2_fromCF
      #UserData: CHANGEME # Optional
      #Volumes:
        #- Device: CHANGEME
          #VolumeId: CHANGEME

パラメータメモ
KeyName:秘密鍵の名前(.pemは不要)
ImageId: ami-0f27d081df46f326c ←AmazonLinux
AssociatePublicIpAddress: true ←パブリックIPアドレスを自動で割り当てる
DeviceIndex: "0" ←自動でパブリックIPアドレスを割り当てる際は"0"にする
GroupSet: Import関数でセキュリティグループを参照

スタックを作成

マネージメントコンソール- **[CloudFormation]から[スタックの作成]**をクリックし、
Application.ymlとSecutiry.ymlをアップロードして、スタックを作成する。
Security-Layer.PNG
Application-Layer.PNG
イベントタブのステータスがCREATE_COMPLETEになっていれば、正常に作成されたことを確認できる。
作成したEC2にセキュリティグループが適用され、EC2に秘密鍵を使ってSSH接続できることを確認した。

EC2.PNG

最後に

EC2のセキュリティグループに関して紛わしい部分があるので、以下にメモを残しておきます。
①NetworkInterfaces配下のGroupSet・・・EC2インスタンス起動時に新しくネットワークインターフェースを作成する場合にセキュリティグループのIDを参照する際に使用
②SecurityGroups・・・デフォルトVPCのセキュリティグループを作成する際に使用
③SecurityGroupIds・・・推奨。参照するセキュリティグループ(AWS::EC2::SecurityGroup)を作成して、それを参照する際に使用

謎.PNG


この記事はAWS初学者を導く体系的な動画学習サービス
「AWS CloudTech」の課題カリキュラムで作成しました。
https://aws-cloud-tech.com

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?