일단 여긴 스킵하겠다. 다른데도 많잖아
.
├── main.tf
├── modules
│ ├── EC2
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ └── VPC
│ ├── main.tf
│ ├── outputs.tf
│ └── variables.tf
├── outputs.tf
├── provider.tf
├── terraform.tfvars
└── variables.tf
파일 구조는 가독성 있게, 분류하기 위함일 뿐, 큰 의미는 없다.
다 합쳐도 무방.
간단한 아키텍쳐면 모든 내용이 main으로 들어가는 듯 하다.
output "vpc_id" {
description = "생성된 VPC의 ID"
value = aws_vpc.이름.id
}
output "public_subnets" {
description = "생성된 공개 서브넷의 ID 목록"
value = aws_subnet.public[*].id
}
output "private_subnets" {
description = "생성된 사설 서브넷의 ID 목록"
value = aws_subnet.private[*].id
}
output "vpc_cidr_block" {
description = "VPC의 CIDR 블록"
value = aws_vpc.이름.cidr_block
}
Terraform 프로바이더 설정을 정의합니다.
사용할 프로바이더(예: AWS, Azure, GCP)와 그 버전을 지정합니다.
Terraform 자체의 버전 요구사항을 정의할 수 있습니다.
# provider.tf
terraform {
required_version = ">= 1.5.7"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}
Terraform 프로젝트에서 변수 값을 설정하는 중요한 파일입니다.
변수에 실제 값을 할당합니다.
환경별 설정이나 사용자 지정 값을 저장합니다.
variables.tf 에서 정의된 변수들의 값을 지정함.
일반적으로 git에 넣지 않는다.
# AWS 리전
aws_region = "ap-northeast-2"
# VPC 설정
vpc_cidr = "192.168.0.0/16"
public_subnet_cidrs = ["192.168.0.0/26", "192.168.0.64/26"]
private_subnet_cidrs = ["192.168.0.128/26", "192.168.0.192/26"]
azs = ["ap-northeast-2a", "ap-northeast-2c"]
# EC2 인스턴스 설정
instance_type = "t2.micro"
ami_id = "ami-0e18fe6ecdad223e5" # Amazon Linux 3 AMI ID (서울 리전 기준)
# 태그
project_name = "terraform-project"
environment = "development"
# variables.tf in root directory
variable "aws_region" {
description = "AWS region"
type = string
default = "ap-northeast-2"
}
variable "vpc_cidr" {
description = "CIDR block for VPC"
type = string
default = "10.0.0.0/16"
}
variable "subnet_cidr" {
description = "CIDR block for subnet"
type = string
default = "10.0.1.0/24"
}
variable "availability_zone" {
description = "Availability Zone"
type = string
default = "ap-northeast-2a"
}
variable "ami_id" {
description = "AMI ID for EC2 instance"
type = string
default = "ami-0e18fe6ecdad223e5"
}
variable "instance_type" {
description = "EC2 instance type"
type = string
default = "t2.micro"
}
# modules/vpc/main.tf
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
enable_dns_hostnames = true
tags = {
Name = "main-vpc"
}
}
resource "aws_subnet" "main" {
vpc_id = aws_vpc.main.id
cidr_block = var.subnet_cidr
availability_zone = var.az
map_public_ip_on_launch = true
tags = {
Name = "main-subnet"
}
}
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = {
Name = "main-igw"
}
}
resource "aws_route_table" "main" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = {
Name = "main-route-table"
}
}
resource "aws_route_table_association" "main" {
subnet_id = aws_subnet.main.id
route_table_id = aws_route_table.main.id
}
variables.tf, tfvars 등 파일들은 현재 경로에서만 사용됨.
"모듈"에서는 각자 별도의 파일들이 필요함.
Terraform Module
├── main.tf
├── variables.tf
├── outputs.tf
└── README.md
terraform init
terraform apply
`
`
`
`
module.ec2.aws_instance.main: Still creating... [20s elapsed]
module.ec2.aws_instance.main: Still creating... [30s elapsed]
module.ec2.aws_instance.main: Creation complete after 32s [id=i-0d4e3ed7d4e3952f6]
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
Outputs:
instance_public_ip = "13.124.167.45"
vpc_id = "vpc-06643acdeabac6fec"
terrafrom destroy