CLOUD/IaC

[Terraform/AWS] AWS 네트워크 생성 (VPC/Subnet/Internet Gateway/Routing Table)

alsruds 2023. 3. 30. 11:41

♡ 미리보기  

1. terraform 폴더 > vpc 폴더 생성 : vscode에서 불러오기
2. main.tf 파일 생성
3. terraform code 로 vpc 생성
          > terraform init
          > terraform apply
          > terraform destroy


 

 

● 기본 설정

2023.03.30 - [분류 전체보기] - [Terraform] 시작하기

 

[Terraform] 시작하기

♡ 미리보기 ♡ 1. 테라폼 홈페이지에서 다운로드 2. 경로 설정 3. aws configure : key 등록 4. vs code 에서 hasicope terraform 다운로드 ● 윈도우 환경에 설치 https://www.terraform.io/downloads ● cmd 창에서 편리하

alsrudalsrudalsrud.tistory.com

 

main.tf

provider "aws" {
  region  = "ap-northeast-2"
}

// vpc 생성
resource "aws_vpc" "terraform_vpc" {
    cidr_block = "200.200.0.0/16"
    enable_dns_hostnames = true
    tags = {
        Name = "terraform_vpc"
    }
}

// subnet 생성
resource "aws_subnet" "tvpc_sub1" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.10.0/24"
    availability_zone = "ap-northeast-2a"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub1"
    }
}
resource "aws_subnet" "tvpc_sub2" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.20.0/24"
    availability_zone = "ap-northeast-2b"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub2"
    }
}
resource "aws_subnet" "tvpc_sub3" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.30.0/24"
    availability_zone = "ap-northeast-2c"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub3"
    }
}
resource "aws_subnet" "tvpc_sub4" {
    vpc_id = aws_vpc.terraform_vpc.id   //[resource].[resource name]
    cidr_block = "200.200.40.0/24"
    availability_zone = "ap-northeast-2d"
    map_public_ip_on_launch = true
    tags = {
        Name = "tvpc_sub4"
    }
}

// internet gateway 생성
resource "aws_internet_gateway" "terraform_ig" {
    vpc_id = aws_vpc.terraform_vpc.id
    tags = {
        Name = "terraform_ig"
    }
}

// routing table 생성
resource "aws_default_route_table" "terraform_route" {
    default_route_table_id = aws_vpc.terraform_vpc.default_route_table_id

    route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.terraform_ig.id
    }
    tags = {
        Name = "terraform_route"
    }
}

 

 실행

terraform init
terraform apply

 

확인

 

삭제

terraform destory