1) Google Cloud Project – Service Account
giờ kiểm tra lại chúng ta có thêm 1 project vừa mới tạo
Để tìm ra phần service account gồm 2 cách
Các 1 tìm kiềm
Cách 2 click step by step.
2) Google Provider & version
Google Provider
Infrastructure provision inside GCP from Terraform
Terraform has multiple provider with different cloud
https://registry.terraform.io/providers/hashicorp/google/latest/docs
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.85.0"
}
}
}
provider "google" {
# Configuration options
}
cd thư mục bạn vừa tạo và có file main.tg
rồi chạy câu lệnh terraform init
3) Connect Terraform with GCP
3.1) usernamepassword
Chúng ta cần copy phần như ảnh
File main.tf sẽ có 1 chút thay đổi:
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.85.0"
}
}
}
provider "google" {
# Configuration options
project = "terraform-gcp-346216"
region = "us-central1"
zone = "us-central1-a"
}
resource google_storage_bucket "GCS1"{
name = "bucket-from-tf-up-nimtechnology"
}
bạn chọn User mà bạn dụng Học GCP ấy
gcloud auth list
Sau đó mình chạy lệnh terraform thì lỗi
Mình chạy login lại
gcloud auth application-default login
XOng rồi thì bạn xoá luôn nhé
3.2) Cloud Shell VM
Để console có nhiều không gian hơn:PS1=$
mkdir GCS
cd GCS/
nano main.tf
gcloud auth list
terraform apply
3.3) Service Account
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.85.0"
}
}
}
provider "google" {
# Configuration options
project = "terraform-gcp-326702"
region = "us-central1"
zone = "us-central1-a"
credentials = "keys.json"
}
resource google_storage_bucket "GCS1"{
name = "bucket-from-tf-using-sa-nimtechnology"
}
terraform init
terraform plan
terraform apply
Bạn sẽ gặp lỗi này:Error: googleapi: Error 403: terraform-gcp@terraform-gcp-346216.iam.gserviceaccount.com does not have storage.buckets.create access to the Google Cloud project., forbidden
Lúc này chúng ta cần add thêm role