{Terraform} S3/GCP

 

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/storage_bucket
https://cloud.google.com/storage/docs/terraform-create-bucket-upload-object?hl=ja
https://zenn.dev/tatsuyasusukida/articles/how-to-create-a-gcs-backet-with-terraform

 


-- 1. macにTerraformインストール

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew upgrade hashicorp/tap/terraform
terraform -help
terraform -version


-- 2. GCPプロジェクト作成

gcloud init
gcloud auth list

gcloud --version

gcloud projects create project01-9999999 \
--name="project01"

gcloud config list
gcloud config set project project01-9999999
gcloud config set compute/region asia-northeast1 --quiet
gcloud config set compute/zone asia-northeast1-a --quiet

 

gcloud beta billing accounts list
gcloud beta billing projects link project01-9999999 --billing-account=111111-111111-111111

gcloud services enable compute.googleapis.com --project project01-9999999

 

-- 3. tfファイル作成

vim main.tf

provider "google" {
  project = "project01-9999999"
  region = "asia-northeast1"
}


# Create new storage bucket in the US multi-region
# with standard storage

resource "google_storage_bucket" "bucket99999999" {
  name          = "bucket99999999"
  location      = "ASIA-NORTHEAST1"
  storage_class = "STANDARD"
  force_destroy = true

  uniform_bucket_level_access = true
  public_access_prevention = "enforced"
  
}

# Upload a text file as an object
# to the storage bucket

resource "google_storage_bucket_object" "a" {
  name         = "a.txt"
  source       = "/Users/test/99999999/a.txt"
  content_type = "text/plain"
  bucket       = google_storage_bucket.bucket99999999.id
}

output "bucket_name" {
  value = google_storage_bucket.bucket99999999.name
}

 

echo test > a.txt


-- 4. terraform 実行


gcloud auth application-default login
terraform init

terraform plan
terraform apply
terraform output

gcloud storage ls
gcloud storage ls gs://bucket99999999/*

 

-- 5. GCPクリーンアップ

terraform destroy


gcloud projects list

gcloud projects delete project01-9999999 \
--quiet