개념 공부/(DevOps) 01. IaC

[terraform] EC2 EBS 증설

hoonii2 2024. 6. 27. 11:49

1. 개요

terraform 을 통한 ec2 root device ebs 증설 진행 및 downtime 발생 여부를 확인합니다.

 

2. terraform 설정

data "aws_ami" "ami_ubuntu" {
  most_recent = true
  owners      = ["099720109477"]

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  filter {
    name   = "architecture"
    values = ["x86_64"]
  }
}

resource "aws_key_pair" "hoon_key_pair" {
  key_name   = "hoonKey"
  public_key = file(var.MY_PUBLIC_KEY_PATH)
}

resource "aws_instance" "hoon_ec2" {
  ami           = data.aws_ami.ami_ubuntu.id
  instance_type = "t2.micro"

  key_name = aws_key_pair.hoon_key_pair.key_name

  vpc_security_group_ids = [aws_security_group.allow_ssh.id]

  subnet_id = aws_subnet.hoon_subnet_az1.id

  root_block_device { # 코드 추가 ( 현재 기본 값인 8 GiB 로 동작 중 )
    volume_size = 10
  }

  tags = {
    CreatedBy = "terraform"
  }
}

terraform 을 통해 관리 중인 ec2 에 "root_block_device" 구문을 추가해 크기를 8 -> 10 GiB 로 증설합니다.

 

3. terraform apply 후 downtime 확인

#!/bin/bash

a=1
while :
do
        echo "testing " $a
        a=$(expr $a + 1)
        sleep 1
done

ssh 를 통해 기존 동작 중인 ec2 접속 후 위 스크립트를 미리 실행하여 위 (2) 코드의 terraform apply 를 통한 ebs 증설이 downtime 을 발생시키는지 확인합니다.

 

...
testing  46
testing  47
testing  48
testing  49
testing  50
testing  51
testing  52
...

terraform apply 수행 후 downtime 발생하지 않음을 확인합니다.

 

4. 증설 확인

기존 8GiB 에서 10 GiB 로 증설됨을 확인합니다.

 

root@ip-192-168-0-121:~# lsblk
NAME     MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop0      7:0    0 25.2M  1 loop /snap/amazon-ssm-agent/7993
loop1      7:1    0 55.7M  1 loop /snap/core18/2823
loop2      7:2    0 63.9M  1 loop /snap/core20/2318
loop3      7:3    0   87M  1 loop /snap/lxd/28373
loop4      7:4    0 38.8M  1 loop /snap/snapd/21759
xvda     202:0    0   10G  0 disk # 총 용량 10G 증가 확인
├─xvda1  202:1    0  7.9G  0 part /
├─xvda14 202:14   0    4M  0 part
└─xvda15 202:15   0  106M  0 part /boot/efi

ec2 의 "xvda" 디스크 용량이 10G 로 증설됨을 확인합니다.

 

이후 파티션 1의 용량을 확장시켜 증설한 스토리지를 사용할 수 있습니다.

 

5. 참고

terraform 구문

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance#ebs-ephemeral-and-root-block-devices

 

Terraform Registry

 

registry.terraform.io

 

ebs 파티션 증설 방안

https://docs.aws.amazon.com/ebs/latest/userguide/recognize-expanded-volume-linux.html

 

Extend the file system after resizing an EBS volume - Amazon EBS

The following instructions walk you through the process of extending XFS and Ext4 file systems for Linux. For information about extending a different file system, see its documentation.

docs.aws.amazon.com