brunch

You can make anything
by writing

C.S.Lewis

by Master Seo Aug 03. 2023

4. GCP-테라폼 문법 개념잡기



목표

테라폼 클라우드 환경설정하기

기본 네트워크 만들기

테라폼 블럭,프로바이더 이해하기

테라폼 init,plan,apply 이해하기

리소스 추가 이해하기

리소스 변경 이해하기

리소스 교체 이해하기

리소스 삭제 이해하기

리소스 종속성 이해하기

프로비저너로 파일 업로드,쉘 스크립트 실행, 소프트웨어 설치하기




<1> 테라폼 클라우드 환경설정하기

<2> 기본 네트워크 만들기

<3> 리소스 추가하기

<4>  리소스 변경하기

<5> 파괴하고 변경하기

<6> 리소스  종속성  사용하기

<7>  명시적 종속성

<8>  프로비저너 사용하기 , 소프트웨어 설치, provisioner



<1> 테라폼 클라우드 환경설정하기


1

구글클라우드 로그인


2

오른쪽위 Cloud Shell 실행

Cloud Shell에서 작업한다.


3

gcloud auth list

인증


4

gcloud config list project

프로젝트 ID 확인

프로젝트 ID를 복사해 놓는다.






<2> 기본 네트워크 만들기



1

 vi main.tf


terraform {

  required_providers {

    google = {

      source = "hashicorp/google"

    }

  }

}

provider "google" {

  version = "3.5.0"

  project = "<PROJECT_ID>"

  region  = "us-central1"

  zone    = "us-central1-c"

}

resource "google_compute_network" "vpc_network" {

  name = "terraform-network"

}



// <PROJECT_ID>  에  위에서 확인한 PROJECT_ID를 입력한다.



2

terraform init

// 테라폼 구성을 초기화 . 

// 테라폼을 이용해 리소스를 만들수 있도록 라이블러리 등을 받아온다.


3

terraform plan

테라폼 코드와 실제 리소스간 비교하여 변경되는 부분을 보여준다.

실제 생성되지는 않는다.


4

terraform apply

실제 리소스를 생성 또는 삭제시킨다.



5

콘솔에서 만들어진 리소스를 확인한다.

vpc 가서 확인하자.





<3> 리소스 추가하기



1

vm.tf   를만들어 가상서버를 추가해보자.



vi vm.tf


resource "google_compute_instance" "vm_instance" {

  name         = "terraform-instance"

  machine_type = "e2-micro"

  boot_disk {

    initialize_params {

      image = "debian-cloud/debian-11"

    }

  }

  network_interface {

    network = google_compute_network.vpc_network.name

    access_config {

    }

  }

}



terraform plan

terraform apply


콘솔에서 생성 확인




<4>  리소스 변경하기


1

tags  추가하기


resource "google_compute_instance" "vm_instance" {

  name         = "terraform-instance"

  machine_type = "e2-micro"

  tags         = ["web", "dev"]

  # ...

}



2

terraform plan

terraform apply 




<5> 파괴하고 변경하기


1

가상서버의 부팅 디스크를 변경해보자.

서버를 제거하고 다시 만들어진다.


boot_disk {

    initialize_params {

      image = "cos-cloud/cos-stable"

    }

  }



2

terraform plan

terraform apply 



3

리소스 모두 삭제하기

terraform destroy




<6> 리소스  종속성  사용하기


1

terraform apply

다시 네트워크와 서버 만들기


2

static ip 할당 하기


resource "google_compute_address" "vm_static_ip" {

  name = "terraform-static-ip"

}



3

terraform plan



4

네트워크 인터페이스 업데이트하기


vi vm.tf  에 아래 내용 추가.


network_interface {

    network = google_compute_network.vpc_network.self_link

    access_config {

      nat_ip = google_compute_address.vm_static_ip.address

    }

  }



5

terraform plan -out static_ip



6

terraform apply "static_ip"





<7>  명시적 종속성



# New resource for the storage bucket our application will use.


resource "google_storage_bucket" "example_bucket" {

  name     = "<UNIQUE-BUCKET-NAME>"

  location = "US"

  website {

    main_page_suffix = "index.html"

    not_found_page   = "404.html"

  }

}

# Create a new instance that uses the bucket

resource "google_compute_instance" "another_instance" {

  # Tells Terraform that this VM instance must be created only after the

  # storage bucket has been created.

  depends_on = [google_storage_bucket.example_bucket]

  name         = "terraform-instance-2"

  machine_type = "e2-micro"

  boot_disk {

    initialize_params {

      image = "cos-cloud/cos-stable"

    }

  }

  network_interface {

    network = google_compute_network.vpc_network.self_link

    access_config {

    }

  }

}



terraform plan

terraform apply





<8>  프로비저너 사용하기 , 소프트웨어 설치, provisioner


1

vm

resource "google_compute_instance" "vm_instance" {

  name         = "terraform-instance"

  machine_type = "e2-micro"

  tags         = ["web", "dev"]

  provisioner "local-exec" {

    command = "echo ${google_compute_instance.vm_instance.name}:  ${google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip} >> ip_address.txt"

  }

  # ...

}



// provisioner내에 블록이 추가됩니다 

resource. 여러 프로비저닝 단계를 정의하기 위해 여러 provisioner블록을 추가할 수 있습니다



2

terraform apply


 

3

terraform taint google_compute_instance.vm_instance

taint = 인스턴스를 다시 생성하도록 Terraform에 알리는 데 사용합니다


4

terraform apply



5

Verify everything worked by looking at the contents of the ip_address.txt file.

It contains the IP address, just as you asked.







다음 자료

https://brunch.co.kr/@topasvga/3388




https://brunch.co.kr/@topasvga/2419




감사합니다.


keyword
매거진의 이전글 3. 테라폼-GCP-모듈

작품 선택

키워드 선택 0 / 3 0

댓글여부

afliean
브런치는 최신 브라우저에 최적화 되어있습니다. IE chrome safari