6. Terraform을 사용하여 커스텀 AMI에서 EC2 인스턴스 시작

발행: (2026년 2월 7일 오후 02:53 GMT+9)
4 분 소요
원문: Dev.to

I’m happy to translate the article for you, but I’ll need the full text of the post (the content you’d like translated) in order to do so. Could you please paste the article’s body here? Once I have the text, I’ll keep the source link unchanged at the top and provide a Korean translation while preserving all formatting, markdown, and code blocks.

실험실 정보

Nautilus DevOps 팀은 백업 및 확장을 위해 기존 EC2 인스턴스로부터 AMI를 생성해야 합니다.

  • 기존 EC2 인스턴스 이름: devops-ec2
  • 원하는 AMI 이름: devops-ec2-ami
  • 새 EC2 인스턴스 이름: devops-ec2-new (AMI에서 시작해야 함)

main.tf 파일을 업데이트하십시오 (별도의 .tf 파일을 만들지 말 것):

  1. 기존 인스턴스로부터 AMI를 프로비저닝합니다.
  2. 해당 AMI를 사용하여 새 EC2 인스턴스를 시작합니다.

outputs.tf 파일을 생성하여 다음을 출력하도록 합니다:

  • KKE_ami_id – 생성된 AMI의 ID.
  • KKE_new_instance_id – 새 EC2 인스턴스의 ID.

Lab Solutions

main.tf

# Step 1: Read the existing EC2 instance
data "aws_instance" "existing_ec2" {
  filter {
    name   = "tag:Name"
    values = ["devops-ec2"]
  }
}

# Step 2: Create AMI from existing EC2
resource "aws_ami_from_instance" "devops_ami" {
  name               = "devops-ec2-ami"
  source_instance_id = data.aws_instance.existing_ec2.id
}

# Step 3: Launch a new EC2 from the AMI
resource "aws_instance" "devops_ec2_new" {
  ami           = aws_ami_from_instance.devops_ami.id
  instance_type = "t2.micro"

  tags = {
    Name = "devops-ec2-new"
  }
}

outputs.tf

output "KKE_ami_id" {
  value = aws_ami_from_instance.devops_ami.id
}

output "KKE_new_instance_id" {
  value = aws_instance.devops_ec2_new.id
}

Terraform Commands (MANDATORY)

다음 명령을 순서대로 실행하십시오:

terraform init
terraform validate
terraform apply
# Type "yes" when prompted

Terraform apply output

Source:

Simple Step‑by‑Step Explanation (Why & What Happens)

What is an AMI?

An AMI (Amazon Machine Image) is a snapshot + blueprint of an EC2 instance. It includes:

  • Operating system
  • Installed software
  • Configuration
  • Disk data

Why create an AMI?

  • Backup an EC2 instance
  • Create identical servers quickly
  • Scale out efficiently
  • Enable disaster recovery

What the Terraform code does

  1. Find the existing EC2

    data "aws_instance" "existing_ec2" { … }

    Terraform reads the instance whose Name tag is devops-ec2. No new resources are created here.

  2. Create an AMI from that EC2

    resource "aws_ami_from_instance" "devops_ami" { … }

    Terraform tells AWS to freeze the running instance’s disk state and generate an AMI. This step may take a few minutes.

  3. Launch a new EC2 from the AMI

    resource "aws_instance" "devops_ec2_new" { … }

    Terraform creates a brand‑new instance using the AMI, resulting in the same OS, software, and configuration as the original.

How Terraform determines the correct order

Terraform builds a dependency graph:

existing EC2 → AMI → new EC2

Because the AMI resource depends on the existing instance and the new EC2 depends on the AMI, Terraform automatically executes the steps in the right sequence.

What happens during terraform apply

  1. Terraform reads the existing EC2.
  2. AWS creates the AMI.
  3. Terraform waits until the AMI is available.
  4. AWS launches the new EC2 from the AMI.
  5. Terraform records everything in the state file.
  6. The defined outputs display the AMI ID and the new instance ID.

Why the outputs matter

  • Confirm that the AMI was successfully created.
  • Confirm that the new EC2 instance was launched.
  • KodeKloud (and other graders) use these outputs for validation, saving you from manually checking the AWS console.

Common Mistakes to Avoid

  • Hard‑coding the instance ID.
  • Creating extra .tf files (the lab requires a single main.tf).
  • Launching the new EC2 before the AMI is ready.
  • Forgetting to define the required outputs.
  • Using incorrect output variable names (KKE_ami_id, KKE_new_instance_id).

리소스 및 다음 단계

크레딧
모든 실습은 다음에서 제공됩니다: KodeKloud

Back to Blog

관련 글

더 보기 »

AI가 OSS 스타를 죽일까?

AI 기반 개발이 가속화됨에 따라, 오픈 소스 소프트웨어는 불편한 역설에 직면하고 있습니다: 사용량은 증가하고 있지만 참여, 지속 가능성 및 커뮤니티 경제는…