EBS를 생성하고 EC2에 연결하는 코드를 CloudFormation으로 작성

EBS를 생성하고 EC2에 연결하는 코드를 CloudFormation으로 작성

Clock Icon2022.03.22 14:13

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

안녕하세요 클래스메소드 김재욱(Kim Jaewook) 입니다. 이번에는 EBS를 생성하고 EC2에 연결하는 코드를 CloudFormation으로 작성해 봤습니다.

AWS Volume

먼저 EBS를 생성하기 위해 CloudFormation에서 Type: AWS::ec2::Volume 을 사용합니다.

Type: AWS::EC2::Volume
Properties: 
  AutoEnableIO: Boolean
  AvailabilityZone: String
  Encrypted: Boolean
  Iops: Integer
  KmsKeyId: String
  MultiAttachEnabled: Boolean
  OutpostArn: String
  Size: Integer
  SnapshotId: String
  Tags: 
    - Tag
  Throughput: Integer
  VolumeType: String

Type: AWS::ec2::Volume은 다음과 같은 속성을 가지고 있습니다.

이번 블로그에서는 간단히 Size와 AvailabilityZone, VolumeType 만을 써서 EBS를 생성하도록 하겠습니다.

속성에 대한 자세한 내용은 아래 링크를 참고해 주세요.

# Create EBS Volume
  SecondVolume:
    Type: AWS::EC2::Volume
    Properties:
      Size: 100
      AvailabilityZone: !GetAtt TestEC2.AvailabilityZone
      VolumeType: gp3
  ThirdVolume:
    Type: AWS::EC2::Volume
    Properties:
      Size: 2000
      AvailabilityZone: !GetAtt TestEC2.AvailabilityZone
      VolumeType: gp3

간단하게 Size와 AvailabilityZone, VolumeType을 사용해서 2개의 EBS Volume을 생성했습니다.

VolumeAttachment

이제 EBS Volume을 생성 했으면, EC2에 연결할 필요가 있습니다.

Type: AWS::EC2::VolumeAttachment
Properties: 
  Device: String
  InstanceId: String
  VolumeId: String

EBS Volume을 연결할 때는 Type: AWS::EC2::VolumeAttachment를 사용합니다.

Device는 xvda,  xvdb와 같은 Device 이름을 뜻합니다.

InstanceId는 연결할 EC2 인스턴스의 Id입니다.

마지막으로 VolumeId는 위에서 생성한 EBS Volume의 Id를 뜻합니다. 자세한 내용은 아래 링크를 참고해 주세요.

# Attached EBS Volume
  SecondVolumeMountPoint:
    Type: AWS::EC2::VolumeAttachment
    Properties:
      InstanceId: !Ref TestEC2
      VolumeId: !Ref SecondVolume
      Device: xvdf
  ThirdVolumeMountPoint:
    Type: AWS::EC2::VolumeAttachment
    Properties:
      InstanceId: !Ref TestEC2
      VolumeId: !Ref ThirdVolume
      Device: xvdg

CloudFormation에서 위와 같이 코드를 작성해줍니다.

결과 확인

AWS 콘솔에서 확인해 보면 xvdf, xvdg 두개의 EBS Volume가 정상적으로 생성된 것을 확인할 수 있습니다.

본 블로그 게시글을 읽고 궁금한 사항이 있으신 분들은 kis2702@naver.com로 보내주시면 감사하겠습니다.

참고

この記事をシェアする

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.