6. Terraform을 사용하여 커스텀 AMI에서 EC2 인스턴스 시작
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 파일을 만들지 말 것):
- 기존 인스턴스로부터 AMI를 프로비저닝합니다.
- 해당 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

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
-
Find the existing EC2
data "aws_instance" "existing_ec2" { … }Terraform reads the instance whose
Nametag isdevops-ec2. No new resources are created here. -
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.
-
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
- Terraform reads the existing EC2.
- AWS creates the AMI.
- Terraform waits until the AMI is available.
- AWS launches the new EC2 from the AMI.
- Terraform records everything in the state file.
- 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
.tffiles (the lab requires a singlemain.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 Learning Labs
- 추가 심층 탐구: Whispering Cloud Insights – 추가 기술 기사
- 토론에 참여하기: DEV Community
크레딧
모든 실습은 다음에서 제공됩니다: KodeKloud