[A-00082]TerraformでGoogleCloudStorageを作成する

・GCSを作成する

GCP関連のネタです。TerraformでGCP上にGCS(GoogleCloudStorage)を作成する方法を記載します。

気をつけて欲しいのはGCSは世界中で一意性のある名前でしか作成できない事です。

provider "google" {
  project     = "<project-id>"
  region      = "asia-northeast1"
}

resource "google_storage_bucket" "sample_gcs_srns" {
  name = "gcs1_srns"
  location = "asia-northeast1"
  force_destroy = true
}

上記のコードでGCSを作成した時に一意性がないと下記のようにエラーが出ます。

googleapi: Error 409: The requested bucket name is not available. The bucket namespace is shared by all users of the system. Please select a different name and try again., conflict
anonymous@cloudshell:~/test (xxxxxx)$ terraform init
anonymous@cloudshell:~/test (xxxxxx)$ terraform plan
anonymous@cloudshell:~/test (xxxxxx)$ terraform apply

成功すると下記のとおり、GCPモニターからGCSが作成されている事がわかります。

・ローカルからGCSにファイルをアップロードしてみる

適当なテストファイルを作成し、それをgcsにアップロードしてみます。

gcloudの認証を通してない人は下記のコマンドを事前に実行して認証を通してください。

gcloud auth login

下記がgcsへのファイルを移動するコマンドです。基本はシェルと同じです。

MacBook-Pro:gcs_test$ gcloud storage mv ./test.txt gs://gcs1_srns
WARNING:  Python 3.5-3.7 will be deprecated on August 8th, 2023. Please use Python version 3.8 and up.

If you have a compatible Python interpreter installed, you can use it by setting
the CLOUDSDK_PYTHON environment variable to point to it.

Copying file://./test.txt to gs://gcs1_srns/test.txt
  Completed files 1/1 | 0B     

次にlsコマンドでgcs上に何があるのかを確認します。

MacBook-Pro:gcs_test$ gcloud storage ls gs://gcs1_srns
WARNING:  Python 3.5-3.7 will be deprecated on August 8th, 2023. Please use Python version 3.8 and up.

If you have a compatible Python interpreter installed, you can use it by setting
the CLOUDSDK_PYTHON environment variable to point to it.

gs://gcs1_srns/test.txt

GCSコンソール上を確認すると上記のようにファイルが上がっているのがわかります。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*