티스토리 뷰

카테고리 없음

terraform 작성`

채희태 2025. 4. 8. 21:00
728x90

테라폼 다운

wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform

이거 따라치면 됨

 

aws configure 도 해줘야함

 

맨 위에 provider로 aws 계정 세팅해주는게 중요함

provider "aws" {
  region     = "ap-northeast-2"
  access_key = "AKIA2YIB77DNOESES4RW"
  secret_key = "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"
}

resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "main"
  }
}

# 서브넷 생성 (public)
resource "aws_subnet" "public01" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"

  availability_zone = "ap-northeast-2a"

  tags = {
    Name = "public_subnet01"
  }
}

resource "aws_subnet" "public02" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"

  availability_zone = "ap-northeast-2b"

  tags = {
    Name = "public_subnet02"
  }
}

# 서브넷 생성 (private)
resource "aws_subnet" "private01" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.3.0/24"

  availability_zone = "ap-northeast-2a"

  tags = {
    Name = "private_subnet01"
  }
}

resource "aws_subnet" "private02" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.4.0/24"

  availability_zone = "ap-northeast-2b"


  tags = {
    Name = "private_subnet02"
  }
}

# 인터넷 게이트웨이 생성
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "main"
  }
}

# 보안그룹 생성
resource "aws_security_group" "remote" {
  vpc_id = aws_vpc.main.id
  name = "remote"
  description = "remote"

  # 인바운드 규칙 설정
  ingress {
    from_port = 22              # 인바운드 시작 포트
    to_port = 22                # 인바운드 끝나는 포트
    protocol = "tcp"            # 사용할 프로토콜
    description = "ssh"         # 자세한 프로토콜 종류
    cidr_blocks = ["0.0.0.0/0"] # 허용할 IP 범위
  }
ingress {
    from_port = -1              # 인바운드 시작 포트
    to_port = -1             # 인바운드 끝나는 포트
    protocol = "icmp"            # 사용할 프로토콜
    description ="icmp"         # 자세한 프로토콜 종류
    cidr_blocks = ["0.0.0.0/0"] # 허용할 IP 범위
  }

  
  # 아웃바운드 규칙 설정
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "web" {
  vpc_id = aws_vpc.main.id
  name = "web"
  description = "web"

  # 인바운드 규칙 설정
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    description = "http"
    cidr_blocks = ["0.0.0.0/0"]
  }
  
  ingress {
    from_port   = 443
    to_port     = 443
    protocol    = "tcp"
    description = "https"
    cidr_blocks = ["0.0.0.0/0"]
  }

  # 아웃바운드 규칙 설정
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# 라우팅 테이블 생성
resource "aws_route_table" "rt" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }

  tags = {
    Name = "rt"
  }
}

# 라우팅 테이블과 서브넷 관계 설정
resource "aws_route_table_association" "public01" {
  subnet_id      = aws_subnet.public01.id
  route_table_id = aws_route_table.rt.id
}

resource "aws_route_table_association" "public02" {
  subnet_id      = aws_subnet.public02.id
  route_table_id = aws_route_table.rt.id
}

resource "aws_route_table_association" "private01" {
  subnet_id      = aws_subnet.private01.id
  route_table_id = aws_route_table.rt.id
}

resource "aws_route_table_association" "private02" {
  subnet_id      = aws_subnet.private02.id
  route_table_id = aws_route_table.rt.id
}

나머진 주석이랑 명령어보고 알아서~

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2026/02   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
글 보관함
250x250