T101-Study-4Week

가시다님과 스터디를 한지도 5번째 이번엔 테라폼이다. 오늘 블로그를 쓰게된건 중간과제를 설명하기 위해서다. 바로 본론으로 들어간다. 내 GIT 이다 https://github.com/Cloud-Linuxer/T101/tree/main/4week variable "availability_zone" { description = "Seoul region availability zone" type = list default = ["ap-northeast-2a", "ap-northeast-2b", "ap-northeast-2c", "ap-northeast-2d"] } variable "subnet_numbers" { type = list default = [10, 20, 30, 40] } variable "az_count" { type = list default = ["A", "B", "C", "D"] } ```bash 나의 Variables는 이런식으로 구성되어있다. 모든 타입을 List로 선언하여 사용한다. 5주차에 할 테라폼 의 반복문을 사용하기 위한 형태다. 가장 중요한 부분은 subnet_numbers 부분이다. 10, 20, 30, 40 이 핵심이다. ```bash resource "aws_subnet" "pub-common" { count = "${length(var.availability_zone)}" vpc_id = "${aws_vpc.default.id}" cidr_block = [ for num in var.subnet_numbers: cidrsubnet(aws_vpc.default.cidr_block, 8, num) ][count.index] availability_zone = "${element(var.availability_zone, count.index)}" tags = { Name = "Linuxer-Dev-Pub-Common-${element(var.az_count, count.index)}" } } ```bash 이 코드만 봐서는 이게 무엇을 뜻하는지 한눈에 보기 어렵다. 그럼 하나씩 설명하겠다. 하시코프에서는 cidrsubnet 이라는 Function 을 지원한다. 이 함수를 통해서 나는 /16비트의 서브넷을 24비트로 자를거다. 간단히 보여주자면 이렇다 ```bash terraform console > cidrsubnet("10.0.0.0/16",8,10) "10.0.10.0/24" > cidrsubnet("10.0.0.0/16",8,20) "10.0.20.0/24" > cidrsubnet("10.0.0.0/16",8,30) "10.0.30.0/24" > cidrsubnet("10.0.0.0/16",8,40) "10.0.40.0/24" ```bash for로 list 에 담긴 subnet_numbers를 가져다가 CIDR 을 반환한다. 위처럼 24비트의 4개 서브넷이다. 위와같이 24비트로 나뉜 4개의 서브넷을 테라폼은 생성한다. 위의 리소스선언 한줄로 Subnet 4개를 생성하는 것이다. 서울 리전의 4개 AZ를 모두 사용하고, A zone은 10대역대 B Zone은 20대역대 C Zone은 30대역 D Zone은 40 대역인것이다. 이렇게 사용하면 장점이 있다. 한개의 존이 문제가 생긴것을 파악하기 쉽고, 아이피 대역대 만으로 서비스의 역할을 파악할수 있는 장점이 있는 것이다. 처음엔 리스트로 서브넷 선언도 모두 입력해서 하나의 리소스 선언으로 모든 서브넷을 생성하려했지만 그렇게 사용할 경우 리스트가 변경되면 모든 서브넷이 영향을 받는 이슈가 있어서 각 서브넷별 리소스 선언을 하는 방향으로 수정했다.

November 12, 2022 · 2 min · 📁 AWS

gcp terraform-3 vpc-nat create

vpc-nat 를 연결하기로 했다. 어젠 subnet 을 만들었고 오늘은 망분리 환경을 구성하기 위해 nat 를 넣었다. main.tf resource “google_compute_subnetwork” “us-central1-subnet” { name = “${local.name_suffix}-us-central1-subnet” ip_cidr_range = “10.2.0.0/16” region = “us-central1” network = “${google_compute_network.vpc.self_link}” } resource “google_compute_router” “us-central1-router” { name = “${local.name_suffix}-us-central1-router” region = google_compute_subnetwork.us-central1-subnet.region network = google_compute_network.vpc.self_link } resource “google_compute_address” “address” { count = 2 name = “${local.name_suffix}-nat-manual-ip-${count.index}” region = google_compute_subnetwork.us-central1-subnet.region } resource “google_compute_router_nat” “nat_manual” { name = “${local.name_suffix}-us-central1-router-nat” router = google_compute_router.us-central1-router.name region = google_compute_router.us-central1-router.region nat_ip_allocate_option = “MANUAL_ONLY” nat_ips = google_compute_address.address.*.self_link ...

January 5, 2020 · 1 min · 📁 GCP · 🏷️ gcp, terraform, nat

gcp-terrafrom-2 with VPC create

이전 포스팅에서 cloud shell 을 이용해서 terraform 을 사용하는 방법을 포스팅 했다. 이번에는 VPC 를 생성하는 방법을 포스팅 하기로 하였다. https://www.terraform.io/docs/providers/google/r/compute_subnetwork.html 다음 docs 를 참고하였다. resource name -실제 vpc name -에 대문자가 들어가면 Error: Error creating Network: googleapi: Error 400: Invalid value for field ‘resource.name’ 에러가 발생한다 참고하자. 이걸 몰라서 한참..테스트를 했다. main.tf resource “google_compute_subnetwork” “us-central1-subnet” { name = “${local.name_suffix}-us-central1-subnet” ip_cidr_range = “10.2.0.0/16” region = “us-central1” network = google_compute_network.linuxer-VPC.self_link } resource “google_compute_subnetwork” “europe-west1-subnet” { name = “${local.name_suffix}-europe-west1-subnet” ip_cidr_range = “10.3.0.0/16” region = “europe-west1” network = google_compute_network.linuxer-VPC.self_link } resource “google_compute_network” “linuxer-VPC” { name = “${local.name_suffix}-vpc” auto_create_subnetworks = false } ...

January 4, 2020 · 2 min · 📁 GCP

gcp-terrafrom-1 with google cloud shell

gcp 스터디를 위해서 테라폼의 사용을 익히려고 한다. 그러기 위해선 먼저 테라폼을 gcp cloudshell 에서 사용하는것이 우선이라 생각했다. 테라폼으로 aws내 에선 테스트 경험이 있으므로 gcp 의 네트워크 구성과 환경에 맞춰서 테라폼을 설정하는 방법을 익혀야 했다. gcp 에서 테라폼을 사용하려면 cloudshell 을 사용하는 방법과 인스턴스를 생성하여 사용하는 방법 아니면 클라이언트 PC에서 사용하는 방법 이렇게 3가지가 있는데 나는 cloudshell 을 매우 좋아하므로 cloudshell 로 진행 할것이다. 먼저 cloud shell 에서 테라폼을 사용하려면 몇가지 단계를 거쳐야 했다. ...

January 4, 2020 · 3 min · 📁 GCP · 🏷️ gcp, cloudshell, terraform