본문 바로가기

퍼블릭 클라우드

Terraform으로 AWS Subnet 생성하기

반응형

테라폼 서브넷 생성

VPC 생성 이후, Public / Private Subnet을 구성하는 방법입니다.

1. 사전 조건

VPC 생성 완료

Terraform Provider 설정 완료

2. Subnet 구성

vim subnet.tf
#################################
# Public Subnet (WEB)
#################################

resource "aws_subnet" "public_a" {
  vpc_id                  = aws_vpc.sangchul_vpc11.id
  cidr_block              = "10.11.3.0/24"
  availability_zone       = "us-east-1a"
  map_public_ip_on_launch = true

  tags = {
    Name        = "sangchul_vpc11-public-a"
    Environment = var.env
    Role        = "public-subnet"
    Service     = "network"
  }
}

resource "aws_subnet" "public_c" {
  vpc_id                  = aws_vpc.sangchul_vpc11.id
  cidr_block              = "10.11.4.0/24"
  availability_zone       = "us-east-1c"
  map_public_ip_on_launch = true

  tags = {
    Name        = "sangchul_vpc11-public-c"
    Environment = var.env
    Role        = "public-subnet"
    Service     = "network"
  }
}

#################################
# Private Subnet (DB)
#################################

resource "aws_subnet" "private_a" {
  vpc_id            = aws_vpc.sangchul_vpc11.id
  cidr_block        = "10.11.13.0/24"
  availability_zone = "us-east-1a"

  tags = {
    Name        = "sangchul_vpc11-private-a"
    Environment = var.env
    Role        = "private-subnet"
    Service     = "network"
  }
}

resource "aws_subnet" "private_c" {
  vpc_id            = aws_vpc.sangchul_vpc11.id
  cidr_block        = "10.11.14.0/24"
  availability_zone = "us-east-1c"

  tags = {
    Name        = "sangchul_vpc11-private-c"
    Environment = var.env
    Role        = "private-subnet"
    Service     = "network"
  }
}
728x90

3. 실행 절차

3.1 실행 계획 확인 (권장)

terraform plan -out=tfplan

3.2 Subnet 생성

terraform apply tfplan

또는

terraform apply
aws_vpc.sangchul_vpc11: Refreshing state... [id=vpc]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

...

Plan: 4 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

aws_subnet.sangchul_vpc11-sb14: Creating...
aws_subnet.sangchul_vpc11-sb13: Creating...
aws_subnet.sangchul_vpc11-sb4: Creating...
aws_subnet.sangchul_vpc11-sb3: Creating...
aws_subnet.sangchul_vpc11-sb14: Creation complete after 3s [id=subnet]
aws_subnet.sangchul_vpc11-sb13: Creation complete after 3s [id=subnet]
aws_subnet.sangchul_vpc11-sb4: Creation complete after 4s [id=subnet]
aws_subnet.sangchul_vpc11-sb3: Creation complete after 4s [id=subnet]

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

4. 생성 결과 확인

terraform show

또는 AWS CLI

aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=$(terraform output -raw vpc_id)"

5. Output 설정 (권장)

vim outputs.tf
output "public_subnets" {
  value = [
    aws_subnet.public_a.id,
    aws_subnet.public_c.id
  ]
}

output "private_subnets" {
  value = [
    aws_subnet.private_a.id,
    aws_subnet.private_c.id
  ]
}

 

728x90
반응형