brunch

2. 테라폼 -GCP-네트워크,서버 추가,정보 변경

by Master Seo


Infrastructure as Code with Terraform


<1> 기본 정보 확인

<2> 네트워크 생성

<3> 서버 추가

<4> 테그 추가 해보기

<5> 부트 디스크 이미지 변경 하기

<6> 리소스 종속성

<7> 버킷과 버킷 리소스를 사용해 인스턴스 만든기

<8> 프로비저닝 도구를 사용




<1> 기본 정보 확인


1

클라우드 쉘 실행


2

어카운트 정보 확인

gcloud auth list

승인


ACTIVE: *

ACCOUNT: topasvga0802@proton.me


3

프로젝트 정보 확인

gcloud config list project


[core]

project = seismic-catbird-358207





<2> 네트워크 생성



1

콘솔에서 네트워크 정보 확인


10 default.png


2

클라우드 쉘에서 네트워크 만들어보자



vi main.tf


terraform {

required_providers {

google = {

source = "hashicorp/google"

}

}

}

provider "google" {

version = "3.5.0"

project = "seismic-catbird-358207"

region = "us-central1"

zone = "us-central1-c"

}

resource "google_compute_network" "vpc_network" {

name = "terraform-network"

}


// 이름이 terraform-network 이다.



3

terraform init



4

terraform apply

yes



5

콘솔에서 다시 확인

네트워크

20 테리폼.png



6

terraform show




<3> 서버 추가


1

리소스 추가하기

컴퓨트 인스턴스를 추가해보자



vi main.tf


// 인스턴스 이름 = 테라폼 인스턴스


resource "google_compute_instance" "vm_instance" {

name = "terraform-instance"

machine_type = "f1-micro"

boot_disk {

initialize_params {

image = "debian-cloud/debian-11"

}

}

network_interface {

network = google_compute_network.vpc_network.name

access_config {

}

}

}



3

terraform apply

Enter a value: yes


google_compute_instance.vm_instance: Creating...

google_compute_instance.vm_instance: Still creating... [10s elapsed]

google_compute_instance.vm_instance: Creation complete after 11s [id=projects/qwiklabs-gcp-02-e9c1c581c936/zones/us-central1-c/instances/terraform-instance]


Apply complete! Resources: 1 added, 0 changed, 0 destroyed.



4

콘솔에 가서 확인

40 테라폼 인스턴스.png



<4> 테그 추가 해보기


1

terraform show

테그 없는것 확인



2

기존 파일에 테그 내용만 추가 ~


vi main.tf


resource "google_compute_instance" "vm_instance" {

name = "terraform-instance"

machine_type = "f1-micro"

tags = ["web", "dev"]

# ...

}



변경 1개

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.


네트워크 태그


3

적용

terraform apply



4

terraform show

태그 있는것 화인




<5> 부트 디스크 이미지 변경 하기


1



main.tf 에서 디스크 이미지 수정


boot_disk {

initialize_params {

image = "cos-cloud/cos-stable"

}

}




2

terraform apply


3

콘솔에서 확인하기



4

제거

terraform destroy

yes


5

결과?

디폴트 네트워크는 존재한다.




<6> 리소스 종속성


리소스 종속성과 리소스 매개변수를 사용한 리소스에 대한 정보를 다른 리소스와 공유하는 법


1

terraform apply

네트워크와 vm 이 새로 만들어진다.



2

공인 아이피 할당


vi mai.tf


resource "google_compute_address" "vm_static_ip" {

name = "terraform-static-ip"

}



3

terraform plan


4

네트워크 인터페이스 설정


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> 버킷과 버킷 리소스를 사용해 인스턴스 만든기


terraform {

required_providers {

google = {

source = "hashicorp/google"

}

}

}

provider "google" {

version = "3.5.0"

project = "qwiklabs-gcp-01-71b122d8e557"

region = "us-central1"

zone = "us-central1-c"

}

resource "google_compute_network" "vpc_network" {

name = "terraform-network"

}

resource "google_compute_instance" "vm_instance" {

name = "terraform-instance"

machine_type = "f1-micro"

tags = ["web", "dev"]

boot_disk {

initialize_params {

image = "cos-cloud/cos-stable"

}

}

network_interface {

network = google_compute_network.vpc_network.self_link

access_config {

nat_ip = google_compute_address.vm_static_ip.address

}

}

}

resource "google_compute_address" "vm_static_ip" {

name = "terraform-static-ip"

}

# 새로운 리소스를 만든다. 새로운 버킷

resource "google_storage_bucket" "example_bucket" {

name = "gcp-tts-dev-bucket"

location = "US"

website {

main_page_suffix = "index.html"

not_found_page = "404.html"

}

}

# 버킷 리소스를 사용해 새로운 인스턴스를 만든다. 의존성이 있는 인스턴스 2이다.

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 = "f1-micro"

boot_disk {

initialize_params {

image = "cos-cloud/cos-stable"

}

}

network_interface {

network = google_compute_network.vpc_network.self_link

access_config {

}

}

}


// 종속성 depends_on = [google_storage_bucket.example_bucket]






<8> 프로비저닝 도구를 사용


Terraform은 프로비저닝 도구를 사용하여 파일을 업로드하고, 셸 스크립트를 실행하거나, 구성 관리 도구와 같은 다른 소프트웨어를 설치 및 트리거합니다.


terraform {

required_providers {

google = {

source = "hashicorp/google"

}

}

}

provider "google" {

version = "3.5.0"

project = "qwiklabs-gcp-01-71b122d8e557"

region = "us-central1"

zone = "us-central1-c"

}

resource "google_compute_network" "vpc_network" {

name = "terraform-network"

}

resource "google_compute_instance" "vm_instance" {

name = "terraform-instance"

machine_type = "f1-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"

}

boot_disk {

initialize_params {

image = "cos-cloud/cos-stable"

}

}

network_interface {

network = google_compute_network.vpc_network.self_link

access_config {

nat_ip = google_compute_address.vm_static_ip.address

}

}

}

resource "google_compute_address" "vm_static_ip" {

name = "terraform-static-ip"

}


# 새로운 리소스를 만든다. 새로운 버킷

resource "google_storage_bucket" "example_bucket" {

name = "gcp-tts2-dev-bucket>"

location = "US"

website {

main_page_suffix = "index.html"

not_found_page = "404.html"

}

}

# 버킷 리소스를 사용해 새로운 인스턴스를 만든다. 의존성이 있는 인스턴스 2이다.

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 = "f1-micro"

boot_disk {

initialize_params {

image = "cos-cloud/cos-stable"

}

}

network_interface {

network = google_compute_network.vpc_network.self_link

access_config {

}

}

}





참고 자료


https://registry.terraform.io/

https://www.terraform.io/language/providers/requirements


google compute instance

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_instance




다음은

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





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


감사합니다.


매거진의 이전글1. 테라폼-GCP-서버 1대 만들기