ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [51일차] Terraform & Full Stack 애플리케이션 구성
    교육/코드스테이츠 2023. 2. 9. 01:23

    VPC 구축하기

    아키텍처에 부합하는 인스턴스를 구축하기전 가상의 프라이빗 네트워크 망을 구축한다.

     

    •  Provider Version

    사용할 프로바이더의 버전을 명시한다.

    ~> 4.16 :: 4.16 버전보다 높은 버전을 사용할 것을 명시함.

    terraform {
      required_providers {
        aws = {
          source  = "hashicorp/aws"
          version = "~> 4.16"
        }
      }
    
      required_version = ">= 1.3.7"
    }
    
    provider "aws" {
      region = "ap-northeast-2"
    }

     

    • vpc 

    CIDR 블록 - 10.0.0.0/16

    tags를 통해 vpc의 이름을 저장했다.

    # VPC
    resource "aws_vpc" "test_vpc" {
        cidr_block = "10.0.0.0/16"
    
        tags = {
            Name = "test_vpc"
        }
    }

     

    • Subnet

    두개의 Public Subnet과 Private Subnet을 만든다.

    리전은 a, c로 설정했다.

    a, b로 할 경우 오류가 발생했다.

    # 퍼블릭 서브넷
    resource "aws_subnet" "public-subnet1" {
        vpc_id = aws_vpc.test_vpc.id
        cidr_block = "10.0.1.0/24"
        availability_zone = "ap-northeast-2a"
    
        tags = {
            Name = "Public Subnet1"}
    }
    
    resource "aws_subnet" "public-subnet2" {
        vpc_id = aws_vpc.test_vpc.id
        cidr_block = "10.0.2.0/24"
        availability_zone = "ap-northeast-2c"
        
        tags = {
            Name = "Public-subnet2"}
    }
    
    #프라이빗 서브넷
    resource "aws_subnet" "private-subnet1" {
        vpc_id = aws_vpc.test_vpc.id
        cidr_block = "10.0.3.0/24"
        availability_zone = "ap-northeast-2a"
        
        tags = {
            Name = "Private-subnet1"}
    }
    
    resource "aws_subnet" "private-subnet2" {
        vpc_id = aws_vpc.test_vpc.id
        cidr_block = "10.0.4.0/24"
        availability_zone = "ap-northeast-2c"
    
        tags = {
            Name = "private-subnet2"}
    }

     

    • Internet Gateway

    Default로 생성 된다,

    # 인터넷 게이트웨이
    resource "aws_internet_gateway" "igw" {
        vpc_id = aws_vpc.test_vpc.id
        
        tags = {
            Name = "igw"
        }
    }

     

    • Nat Gatway

    private subnet에 인스턴스가 외부망과 통신하려면 필요한 gatway이다.

    특징: public subnet안에 있어야 한다.

    resource "aws_nat_gateway" "nat" {
        allocation_id = aws_eip.nat.id
        subnet_id     = aws_subnet.public-subnet1.id
        tags = {
            Name = "aws_nat_gateway"
        }
    }

     

    • Elastic IP Address

    고정 아이피로 일반적인 인스턴스에 퍼블릭 IP는 계속해서 바뀌어 Nat Gatway에 고정으로 넣어줄 IP가 필요하다 .

    # NAT 게이트웨이 탄력적 IP
    resource "aws_eip" "nat" {
        vpc = true
    }

     

    • Route table

    외부로 통신하기 위해 IGW와 연결해준다.

    # 라우팅 테이블
    resource "aws_route_table" "public-RTB1" {
        vpc_id = aws_vpc.test_vpc.id
        route {
            cidr_block = "0.0.0.0/0"
            gateway_id = aws_internet_gateway.igw.id   
        }
    
        tags = {
            Name = "public-RTB1"} 
    }
    
    resource "aws_route_table" "private-RTB1" {
        vpc_id = aws_vpc.test_vpc.id
        route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_nat_gateway.nat.id    
      }
    
        tags = {
          Name = "private-RTB1"}
    }

     

    • 라우트 테이블과 서브넷 연결
    resource "aws_route_table_association" "public_subnet_asso1" {
      subnet_id      = aws_subnet.public-subnet1.id
      route_table_id = aws_route_table.public-RTB1.id
    }
    
    resource "aws_route_table_association" "public_subnet_asso2" {
      subnet_id      = aws_subnet.public-subnet2.id
      route_table_id = aws_route_table.public-RTB1.id
    }
    
    resource "aws_route_table_association" "private_subnet_asso1" {
      subnet_id      = aws_subnet.private-subnet1.id
      route_table_id = aws_route_table.private-RTB1.id
    }
    
    resource "aws_route_table_association" "private_subnet_asso2" {
      subnet_id      = aws_subnet.private-subnet2.id
      route_table_id = aws_route_table.private-RTB1.id
    }

     

    • Security groups

    인스턴스가 통신하기 위한 인바운드 규칙 8080, 80, 22포트가 적용된 보안그룹을 생성한다.

    RDS mysql 통신을 위해서 인바운드 3306을 오픈한 프라이빗 DB보안그룹을 생성한다.

    resource "aws_security_group" "web-sg" {
        name = "web-sg"
        description = "allow 22, 8080, 80"
        vpc_id = aws_vpc.test_vpc.id
        ingress = [{
          description = ""
          ipv6_cidr_blocks = []
          prefix_list_ids = []
          security_groups = []
          cidr_blocks = [ "0.0.0.0/0" ]
          from_port = 22
          protocol = "tcp"
          self = false
          to_port = 22
          
        } ,{
          description = ""
          ipv6_cidr_blocks = []
          prefix_list_ids = []
          security_groups = []
          cidr_blocks = [ "0.0.0.0/0" ]
          from_port = 8080
          protocol = "tcp"
          self = false
          to_port = 8080
          
        } ,{
          description = ""
          ipv6_cidr_blocks = []
          prefix_list_ids = []
          security_groups = []      
          cidr_blocks = [ "0.0.0.0/0" ]
          from_port = 80
          protocol = "tcp"
          self = false
          to_port = 80}]
        
        egress {
            from_port = 0
            to_port = 0
            protocol = "-1"
            cidr_blocks = ["0.0.0.0/0"]
        }
    }
    
    # db-sg
    resource "aws_security_group" "db-sg" {
        name = "db-sg"
        description = "allow 22, 80, 443, 3306"
        vpc_id = aws_vpc.test_vpc.id
        ingress {
            from_port = 22
            to_port = 22
            protocol = "tcp"
            cidr_blocks = ["0.0.0.0/0"]
        }
        ingress {
            from_port = 80
            to_port = 80
            protocol = "tcp"
            cidr_blocks = ["0.0.0.0/0"]
        }
        ingress {
            from_port = 443
            to_port = 443
            protocol = "tcp"
            cidr_blocks = ["0.0.0.0/0"]
        }
        ingress {
            from_port = 3306
            to_port = 3306
            protocol = "tcp"
            cidr_blocks = ["0.0.0.0/0"]
        }
        egress {
            from_port = 0
            to_port = 0
            protocol = "-1"
            cidr_blocks = ["0.0.0.0/0"]
        }
    }

     

Designed by Tistory.