Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

JINIers

220901_Automating Infrastructure on Google Cloud with Terraform: Challenge Lab 본문

GCP/Qwiklabs

220901_Automating Infrastructure on Google Cloud with Terraform: Challenge Lab

JINIers 2022. 9. 2. 14:59

Terraform을 사용하여 Google Cloud에서 인프라 자동화: Challenge Lab

test 주제

  1. 기존 인프라를 terraform 구성으로 가져오기
  2. terraform 모듈 빌드 및 참조
  3. 구성에 원격 백엔드 추가
  4. terraform 레지스트리의 모듈 사용 및 구현
  5. 인프라 재프로비저닝,  파괴 및 업데이트
  6. 생성한 리소스 간의 연결 테스트

참고 동영상 : https://youtu.be/eY7pYGX2MTo


작업 1. 구성파일 생성

아래의 파일 및 디렉토리를 touch, mkdir 이용 생성

main.tf
variables.tf
modules/
└── instances
    ├── instances.tf
    ├── outputs.tf
    └── variables.tf
└── storage
    ├── storage.tf
    ├── outputs.tf
    └── variables.tf

 

variables.tf 내용 추가

variable "region" {
  default = "us-east1"
}
variable "zone" {
  default = "us-east1-c"
}
variable "project_id" {
  default = "프로젝트 ID 넣어"
}

* 이걸 modules/instances/variables.tf, modules/storage/variables.tf 파일에 내용 복붙


main.tf 내용 작성

terraform {
  required_providers {
    google = {
        source = "hashicorp/google"
        version = "3.55.0"
    }
  }
}

provider "google" {
  project     = var.project_id
  region      = var.region
  zone        = var.zone
}

module "instances" {
    source = "./modules/instances"
}

 

terraform 초기화

terraform init

작업 2. 인프라 가져오기

modules/instances/instances.tf 리소스 작성

# tf-instance-1
resource "google_compute_instance" "tf-instance-1" {
    name = "tf-instance-1"
    machine_type = "n1-standard-1"
    zone = var.zone
    boot_disk {
      initialize_params{
          image = "debian-cloud/debian-10"
      }
    }

    network_interface {
      network = "default"
    }
    
    metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}

# tf-instance-2
resource "google_compute_instance" "tf-instance-2" {
    name = "tf-instance-2"
    machine_type = "n1-standard-1"
    zone = var.zone
    boot_disk {
      initialize_params{
          image = "debian-cloud/debian-10"
      }
    }

    network_interface {
      network = "default"
    }
    
    metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}

기존 인스턴스와 일치하도록 리소스를 작성한다.

 

terraform import 명령어 이용 모듈로 가져오기

terraform import module.instances.google_compute_instance.tf-instance-1 인스턴스ID
terraform import module.instances.google_compute_instance.tf-instance-2 인스턴스ID

 

변경사항 적용 및 업데이트

terraform plan
terraform apply
yes

작업 3. 원격 백엔드 구성

module/storage/storage.tf 에 버킷 리소스 작성

resource "google_storage_bucket" "storage-bucket" {
  name = "tf-bucket-19646"
  location = "US"
  force_destroy = true
  uniform_bucket_level_access = true
}


main.tf에 내용 추가(스토리지 모듈)

terraform {
  required_providers {
    google = {
        source = "hashicorp/google"
        version = "3.55.0"
    }
  }
}

provider "google" {
  project     = var.project_id
  region      = var.region
  zone        = var.zone
}

module "instances" {
    source = "./modules/instances"
}

module "storage" {
    source = "./modules/storage"
}


모듈 및 변경사항 초기화

terraform init
terraform apply
yes


main.tf 에 백엔드 추가

terraform {
    backend "gcs" {
      bucket = "tf-bucket-19646"
      prefix = "terraform/state"
    }
  required_providers {
    google = {
        source = "hashicorp/google"
        version = "3.55.0"
    }
  }
}

provider "google" {
  project     = var.project_id
  region      = var.region
  zone        = var.zone
}

module "instances" {
    source = "./modules/instances"
}

module "storage" {
    source = "./modules/storage"
}

 

기존 상태 데이터를 새 백엔드에 복사할 지 여부 확인

terraform init
yes

작업 4. 인프라 수정 및 업데이트


modules/instances/instances.tf 에 인스턴스 추가 및 머신타입 변경

# tf-instance-1 → 머신타입 ~-2로 수정
resource "google_compute_instance" "tf-instance-1" {
    name = "tf-instance-1"
    machine_type = "n1-standard-2"
    zone = var.zone
    boot_disk {
      initialize_params{
          image = "debian-cloud/debian-10"
      }
    }

    network_interface {
      network = "default"
    }
    
    metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}

# tf-instance-2
resource "google_compute_instance" "tf-instance-2" {
    name = "tf-instance-2"
    machine_type = "n1-standard-2"
    zone = var.zone
    boot_disk {
      initialize_params{
          image = "debian-cloud/debian-10"
      }
    }

    network_interface {
      network = "default"
    }
    
    metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}

# tf-instance-3 추가
resource "google_compute_instance" "tf-instance-3" {
    name = "tf-instance-909585"
    machine_type = "n1-standard-2"
    zone = var.zone
    boot_disk {
      initialize_params{
          image = "debian-cloud/debian-10"
      }
    }

    network_interface {
      network = "default"
    }
    
    metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}


변경사항 초기화 및 적용

terraform init
terraform apply
yes

 


작업 5. 자원 오염 및 파괴

instance-3 오염

terraform taint module.instances.google_compute_instance.tf-instance-3

 

구성 초기화 및 적용

terraform init
terraform apply
yes

 

[instance-3 파괴]

 

modules/instances/instances.tf 에 리소스 변경

# tf-instance-3 내용 삭제
resource "google_compute_instance" "tf-instance-3" {
    name = "tf-instance-909585"
    machine_type = "n1-standard-2"
    zone = var.zone
    boot_disk {
      initialize_params{
          image = "debian-cloud/debian-10"
      }
    }

    network_interface {
      network = "default"
    }
    
    metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}

 

변경사항 적용

terraform apply

 


작업 6. 레지스트리의 모듈 사용

main.tf에 network 모듈 내용 추가

module "vpc" {
    source  = "terraform-google-modules/network/google"
    version = "~> 3.4.0"

    project_id   = "qwiklabs-gcp-01-2f310067cbcf"
    network_name = "tf-vpc-169902"
    routing_mode = "GLOBAL"

    subnets = [
        {
            subnet_name           = "subnet-01"
            subnet_ip             = "10.10.10.0/24"
            subnet_region         = "us-east1"
        },
        {
            subnet_name           = "subnet-02"
            subnet_ip             = "10.10.20.0/24"
            subnet_region         = "us-east1"
            subnet_private_access = "true"
            subnet_flow_logs      = "true"
            description           = "This subnet has a description"
        }
    ]
}

 

초기화 및 적용

terraform init
terraform apply
yes

 

[구성 리소스 업데이트]


modules/instances/instances.tf 내용 수정(network 수정, subnet 추가)

#tf-instance-1
resource "google_compute_instance" "tf-instance-1" {
  name = "tf-instance-1"
  machine_type = "n1-standard-2"
  zone = var.zone
  boot_disk {
    initialize_params{
        image = "debian-cloud/debian-10"
    }
  }

  network_interface {
    network = "tf-vpc-169902"
    subnetwork = "subnet-01"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}


#tf-instance-2
resource "google_compute_instance" "tf-instance-2" {
  name = "tf-instance-2"
  machine_type = "n1-standard-2"
  zone = var.zone
  boot_disk {
    initialize_params{
        image = "debian-cloud/debian-10"
    }
  }

  network_interface {
    network = "tf-vpc-169902"
    subnetwork = "subnet-02"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
 allow_stopping_for_update = true
}

 

테라폼 초기화 및 적용

terraform init
terraform apply
yes

 


작업 7. 방화벽 구성

main.tf 방화벽 내용추가

resource "google_compute_firewall" "tf-firewall" {
  name = "tf-firewall"
  network = "projects/프로젝트ID 입력/global/networks/tf-vpc-169902"
  allow {
      protocol = "tcp"
      ports = ["80"]
  }
  source_tags = ["web"]
  source_ranges = ["0.0.0.0/0"]
}

 

테라폼 초기화 및 적용

terraform init
terraform apply
yes

 

'GCP > Qwiklabs' 카테고리의 다른 글

Google Cloud HA VPN 구성하기  (0) 2023.03.20
220901_Interact with Terraform Modules  (0) 2022.09.01
220831_Infrastructure as Code with Terraform  (0) 2022.08.31
220830_Terraform Fundamentals  (0) 2022.08.31
220704_Kubernetes Engine: Qwik Start  (0) 2022.07.04
Comments