Terraform으로 GCS 버킷 및 BigQuery 테이블 빌드
로컬 및 클라우드의 두 가지 사용 방법이 있습니다. 다음은 Google Cloud Platform(GCP)에서 인프라를 구축하기 위해 로컬에 설치하고 사용하는 방법에 대한 설명입니다.
초기 설치: Terraform 및 Google Cloud SDK
Terraform을 설치하려면 해당webpage.에서 제공되는 운영 체제에 적합한 가이드를 선택하십시오.
Terraform 설치를 완료하면 GCP 계정도 있어야 하고 프로젝트를 시작해야 합니다. Terraform을 진행하는 동안 프로젝트의 ID는 중요합니다.

다음 단계는 GCP 프로젝트에 액세스하고 제어하기 위한 키를 얻는 것입니다. GCP 헤더의 풀다운 메뉴에서 방금 만든 프로젝트를 선택하고 다음으로 이동합니다.
Navigation Menu >> IAM & Admin >> Service Accounts >> Create Service Account
다음 단계를 따르십시오.
그러면 서비스 계정 목록에 새 계정이 표시됩니다. 작업 >> 키 관리 >> 키 추가 >> JSON을 클릭하여 로컬 컴퓨터에 키를 다운로드합니다.
다음 단계는 제공된 간단한 지침here.에 따라 Google Cloud SDK를 로컬 시스템에 설치하는 것입니다.
그런 다음 터미널(아래는 GNU/Linux 예시임)을 열어 다음 지침에 따라 다운로드한 키(Json 파일)와 연결하도록 로컬 시스템의 환경 변수를 설정합니다.
export GOOGLE_APPLICATION_CREDENTIALS=/--path to your JSON---/XXXXX-dadas2a4cff8.json
gcloud auth application-default login
그러면 해당 Google 계정을 선택하기 위해 브라우저로 리디렉션됩니다. 이제 로컬 SDK에는 클라우드 서비스에 도달하고 구성하기 위한 자격 증명이 있습니다. 그러나 이러한 초기 인증이 있으면 빌드하려는 GCP 서비스, 즉 Google Cloud Storage(GCP) 및 BigQuery에 특정한 서비스 계정 권한을 여전히 수정해야 합니다.
Navigation Menu >> IAM & Admin >> IAM
다음과 같이 권한을 편집할 프로젝트를 선택합니다.

다음 단계는 다음 링크를 따라 프로젝트에 대한 API를 활성화하는 것입니다.
(API를 사용 설정하는 동안 GCP 계정과 프로젝트 이름에 주의하세요.)
Terraform으로 GCP 서비스 구축
필요한 설치(Teraform 및 Google Cloud SDK) 및 인증을 완료하면 로컬 머신에서 Terraform을 통해 이 두 GCP 서비스를 빌드할 준비가 되었습니다. 기본적으로 설치를 구성하려면
main.tf
및 variables.tf
두 개의 파일이 필요합니다. 전자는 후자에 제공된 변수와 관련하여 GCP 서비스를 생성하기 위해 아래 제공된 코드가 필요합니다(다음 코드 스니펫).# The code below is from https://github.com/DataTalksClub/data-engineering-zoomcamp/tree/main/week_1_basics_n_setup/1_terraform_gcp
# --------------------------------------------------
terraform {
required_version = ">= 1.0"
backend "local" {}
google = {
source = "hashicorp/google"
}
}
}
provider "google" {
project = var.project
region = var.region
// credentials = file(var.credentials) # Use this if you do not want to set env-var GOOGLE_APPLICATION_CREDENTIALS
}
# Data Lake Bucket
# Ref: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket
resource "google_storage_bucket" "data-lake-bucket" {
name = "${local.data_lake_bucket}_${var.project}" # Concatenating DL bucket & Project name for unique naming
location = var.region
# Optional, but recommended settings:
storage_class = var.storage_class
uniform_bucket_level_access = true
versioning {
enabled = true
}
lifecycle_rule {
action {
type = "Delete"
}
condition {
age = 30 // days
}
}
force_destroy = true
}
# DWH
# Ref: https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/bigquery_dataset
resource "google_bigquery_dataset" "dataset" {
dataset_id = var.BQ_DATASET
project = var.project
location = var.region
}
variables.tf
에 대한 코드:# The code below is from https://github.com/DataTalksClub/data-engineering-zoomcamp/tree/main/week_1_basics_n_setup/1_terraform_gcp
# The comments are added by the author
locals {
data_lake_bucket = "BUCKET_NAME" # Write a name for the GCS bucket to be created
}
variable "project" {
description = "Your GCP Project ID" # Don't write anything here: it will be prompted during installation
}
variable "region" {
description = "Region for GCP resources. Choose as per your location: https://cloud.google.com/about/locations"
default = "europe-west6" # Pick a data center location in which your services will be located
type = string
}
variable "storage_class" {
description = "Storage class type for your bucket. Check official docs for more info."
default = "STANDARD"
}
variable "BQ_DATASET" {
description = "BigQuery Dataset that raw data (from GCS) will be written to"
type = string
default = "Dataset_Name" # Write a name for the BigQuery Dataset to be created
}
위에 주어진 파일이 폴더에 있으면 실행할 차례입니다. Terraform CLI에 대한 몇 가지 주요 명령:
주요 명령:
init: 다음 명령에 필요한 폴더와 파일을 추가하여 디렉토리를 준비합니다
유효성 검사: 유효한 경우 기존 구성을 확인합니다
계획: 지정된 구성에 대해 계획된 변경 사항을 표시합니다
적용: 지정된 구성에 대한 인프라를 생성합니다
파괴: 기존 인프라를 파괴합니다
init, plan 및 apply 명령은 다음 출력을 제공합니다(단축됨).
x@y:~/-----/terraform$ **terraform init**
Initializing the backend...
Successfully configured the backend "local"! Terraform will automatically
use this backend unless the backend configuration changes.
.
.
.
x@y:~/-----/terraform$ **terraform plan**
var.project
Your GCP Project ID
Enter a value: xxx-yyy # write yor GCP project ID here
x@y:~/-----/terraform$ **terraform apply**
var.project
Your GCP Project ID
Enter a value: xxx-yyy # write yor GCP project ID here
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following
symbols:
+ create
Terraform will perform the following actions:
.
.
.
위의 세 가지 간단한 코드를 실행하면 GCP 계정에 새로운 GCS 버킷과 BigQuery 테이블이 표시됩니다.
Reference
이 문제에 관하여(Terraform으로 GCS 버킷 및 BigQuery 테이블 빌드), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/cemkeskin84/building-gcs-bucket-and-bigquery-tables-with-terraform-4hf4텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)