ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • aws 기초 네트워크 설정하기 - 2
    infra 2023. 2. 21. 17:57

    aws에서 vpc 관련 설정을 설정 할 때 스크린 샷이다

    요즘엔 이런 ui가 잘 되어 있어서 바로 클릭해서 만들 수 있는게 편하다

     

    이제부터가 본론인데 이런 내용은 쉬운데 왜 스샷을 찍어서 올리느냐이다 위의 콘솔내용을 비교하면서 terraform에 적용할 것이기 때문이다  앞으로의 내용도 terraform 코드가 동봉 될 것이며 1달전 부터 공부한 내용들이 들어갈 것이다 시작해보자

     

    일단 vpc를 만들기 위한 변수들을 설정한다.

    variable "aws_region" {
      type        = string
      default     = "ap-northeast-2"
    }
    
    variable "app_name" {
      type        = string
      description = "Application Name"
    }
    
    variable "app_environment" {
      type        = string
      description = "Application Environment"
    }
    #
    variable "vpc_cidr" {
      description = "The CIDR block for the VPC."
      default = "172.18.0.0/16"
    }
    
    variable "private_subnets" {
      type        = list(string)
      description = "List of private subnets"
      default     = ["172.18.16.0/20","172.18.32.0/20","172.18.48.0/20"]
    }
    
    variable "public_subnets" {
      type        = list(string)
      description = "List of private subnets"
      default     = ["172.18.64.0/20","172.18.80.0/20","172.18.96.0/20"]
    }
    
    
    variable "availability_zones" {
      type        = list(string)
      description = "List of availability zones"
      default = ["ap-northeast-2a","ap-northeast-2b","ap-northeast-2c"]
    }

    vpc 설정

    resource "aws_vpc" "vpc" {
      cidr_block = var.vpc_cidr
      instance_tenancy = "default"
      enable_dns_support = "true"
      enable_dns_hostnames = "true"
      tags = {
        Name        = "${var.app_name}-vpc"
        Environment = var.app_environment
      }
    }

    subnet 설정

    resource "aws_subnet" "public" {
      vpc_id                  = aws_vpc.vpc.id
      cidr_block              = element(var.public_subnets, count.index)
      availability_zone       = element(var.availability_zones, count.index)
      count                   = length(var.public_subnets)
      map_public_ip_on_launch = true
    
      tags = {
        Name        = "${var.app_name}-subnet-public-${count.index + 1}"
        Environment = var.app_environment
      }
    }
    
    resource "aws_subnet" "private" {
      vpc_id                  = aws_vpc.vpc.id
      cidr_block              = element(var.private_subnets, count.index)
      availability_zone       = element(var.availability_zones, count.index)
      count                   = length(var.private_subnets)
      map_public_ip_on_launch = false
    
      tags = {
        Name        = "${var.app_name}-subnet-private-${count.index + 1}"
        Environment = var.app_environment
      }
    }

    서브넷 같은 경우는 개수만큼 for문을 돌려서 중복을 제거 했다

     

    nat gateway

    resource "aws_eip" "nat_eip" {
      vpc        = true
    }
    
    # NAT
    resource "aws_nat_gateway" "nat" {
      allocation_id = aws_eip.nat_eip.id
      subnet_id     = aws_subnet.public[0].id
    
      tags = {
        Name        = "${var.app_name}-nat"
        Environment = var.app_environment
      }
    }

    nat gateway는 외부로 나가는 것은 가능하게 하되 외부에서의 접근은 얘를 거치게 되고 따라서 public ip를 쥐어준다 이걸 elastic ip와 연결을 하게된다


    igw는 vpc와 인터넷 연결을 가능하게 한다

    resource "aws_internet_gateway" "ig" {
      vpc_id = aws_vpc.vpc.id
      tags = {
        Name        = "${var.app_environment}-igw"
        Environment = var.app_environment
      }
    }

     

Designed by Tistory.