はじめに
Terraformを使用してGoogle Cloud Platform(GCP)のCloud Storageについて記載します。
(本記事対象者)
– Terraformを使用したGCP管理に興味のある方
(検証内容)
– Terraformを実行してCloud Storageのバケットを作成する。
– バケットに下記のオブジェクトをアップロードする。
— イメージ
— テキスト
(Terraform実行環境)
– 仮想化ソフトウェア : Oracle VM VirtualBox 5.2.4
– OS : CentOS 7.4
– Terraform: v0.11.2
– インターネット接続 : 可能
(テスト対象環境)
– クラウド環境: Google Compute Engine
– サービス : Google Cloud Storage
(参考情報)
URL: Terraform – google_storage_bucket_object
1.Google Cloud Storageのバケットを作成する
1.1.terraformのtfファイルを作成する。
# cat gcs.tf variable "cpath" { } variable "project" { } variable "region" { } provider "google" { credentials = "${file("${var.cpath}")}" project = "${var.project}" region = "${var.region}" } resource "google_storage_bucket" "COLDLINE" { name = "curry999904" storage_class = "COLDLINE" location = "asia-northeast1" } resource "google_storage_bucket_object" "picture" { name = "butterfly01" source = "sample.eps" bucket = "${google_storage_bucket.COLDLINE.name}" } resource "google_storage_bucket_object" "text" { name = "butterfly02" content = "aaabbbccc" bucket = "${google_storage_bucket.COLDLINE.name}" }
1.2.terraformの環境変数ファイルを作成する。
# cat config.tfvars cpath="/var/credentials/account.json" project="projcet-crossbar-999999" region="asia-northeast1"
1.3.Terraformを初期化する。
# terraform init Initializing provider plugins... - Checking for available provider plugins on https://releases.hashicorp.com... - Downloading plugin for provider "google" (1.4.0)... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.google: version = "~> 1.4" Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
1.4.Terraformの計画をたてる。
# terraform plan -out=tfplan -var-file=config.tfvars Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: + google_storage_bucket.COLDLINE id: force_destroy: "false" location: "ASIA-NORTHEAST1" name: "curry999904" project: self_link: storage_class: "COLDLINE" url: + google_storage_bucket_object.picture id: bucket: "curry999904" content_type: crc32c: detect_md5hash: "different hash" md5hash: name: "butterfly01" source: "sample.eps" storage_class: + google_storage_bucket_object.text id: bucket: "curry999904" content: "aaabbbccc" content_type: crc32c: detect_md5hash: "different hash" md5hash: name: "butterfly02" storage_class: Plan: 3 to add, 0 to change, 0 to destroy. ------------------------------------------------------------------------ This plan was saved to: tfplan To perform exactly these actions, run the following command to apply: terraform apply "tfplan"
1.5.Terraformを実行する。
# terraform apply "tfplan" google_storage_bucket.COLDLINE: Creating... force_destroy: "" => "false" location: "" => "ASIA-NORTHEAST1" name: "" => "curry999904" project: "" => "" self_link: "" => "" storage_class: "" => "COLDLINE" url: "" => "" google_storage_bucket.COLDLINE: Creation complete after 4s (ID: curry999904) google_storage_bucket_object.text: Creating... bucket: "" => "curry999904" content: "" => "aaabbbccc" content_type: "" => "" crc32c: "" => "" detect_md5hash: "" => "different hash" md5hash: "" => "" name: "" => "butterfly02" storage_class: "" => "" google_storage_bucket_object.picture: Creating... bucket: "" => "curry999904" content_type: "" => "" crc32c: "" => "" detect_md5hash: "" => "different hash" md5hash: "" => "" name: "" => "butterfly01" source: "" => "sample.eps" storage_class: "" => "" google_storage_bucket_object.text: Creation complete after 1s (ID: curry999904-butterfly02) google_storage_bucket_object.picture: Creation complete after 2s (ID: curry999904-butterfly01) Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
1.6.terraformの状態を確認する。
# terraform show google_storage_bucket.COLDLINE: id = curry999904 cors.# = 0 force_destroy = false labels.% = 0 location = ASIA-NORTHEAST1 name = curry999904 project = self_link = storage_class = COLDLINE url = gs://curry999904 versioning.# = 0 google_storage_bucket_object.picture: id = curry999904-butterfly01 bucket = curry999904 cache_control = content_disposition = content_encoding = content_language = content_type = application/octet-stream crc32c = NzUi7w== detect_md5hash = 3qWaTCGyUkCjOUnwQlTIJg== md5hash = 3qWaTCGyUkCjOUnwQlTIJg== name = butterfly01 source = sample.eps storage_class = COLDLINE google_storage_bucket_object.text: id = curry999904-butterfly02 bucket = curry999904 cache_control = content = aaabbbccc content_disposition = content_encoding = content_language = content_type = text/plain; charset=utf-8 crc32c = wHwIBQ== detect_md5hash = 0ar0dno8EKRzQHpOR7Atpg== md5hash = 0ar0dno8EKRzQHpOR7Atpg== name = butterfly02 storage_class = COLDLINE
2.Google Cloud Storageのバケットを変更する
2.1.terraformのtfファイルを修正する。
# cat gcs.tf variable "cpath" { } variable "project" { } variable "region" { } provider "google" { credentials = "${file("${var.cpath}")}" project = "${var.project}" region = "${var.region}" } resource "google_storage_bucket" "COLDLINE" { name = "curry999904" storage_class = "COLDLINE" location = "asia-northeast1" } resource "google_storage_bucket_object" "picture" { name = "butterfly01" source = "sample.eps" bucket = "${google_storage_bucket.COLDLINE.name}" } resource "google_storage_bucket_object" "text" { name = "butterfly02" content = "dddddddddddddddddd" bucket = "${google_storage_bucket.COLDLINE.name}" }
2.2.terraformの環境変数ファイルを作成する。
# cat config.tfvars cpath="/var/credentials/account.json" project="projcet-crossbar-999999" region="asia-northeast1"
2.3.Terraformを初期化する。
# terraform init Initializing provider plugins... - Checking for available provider plugins on https://releases.hashicorp.com... - Downloading plugin for provider "google" (1.4.0)... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.google: version = "~> 1.4" Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
2.4.Terraformの計画をたてる。
# terraform plan -out=tfplan -var-file=config.tfvars Refreshing Terraform state in-memory prior to plan... The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage. google_storage_bucket.COLDLINE: Refreshing state... (ID: curry999904) google_storage_bucket_object.picture: Refreshing state... (ID: curry999904-butterfly01) google_storage_bucket_object.text: Refreshing state... (ID: curry999904-butterfly02) ------------------------------------------------------------------------ An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: -/+ destroy and then create replacement Terraform will perform the following actions: -/+ google_storage_bucket_object.text (new resource required) id: "curry999904-butterfly02" => (forces new resource) bucket: "curry999904" => "curry999904" content: "aaabbbccc" => "dddddddddddddddddd" (forces new resource) content_type: "text/plain; charset=utf-8" => crc32c: "wHwIBQ==" => detect_md5hash: "0ar0dno8EKRzQHpOR7Atpg==" => "different hash" (forces new resource) md5hash: "0ar0dno8EKRzQHpOR7Atpg==" => name: "butterfly02" => "butterfly02" storage_class: "COLDLINE" => Plan: 1 to add, 0 to change, 1 to destroy. ------------------------------------------------------------------------ This plan was saved to: tfplan To perform exactly these actions, run the following command to apply: terraform apply "tfplan"
2.5.Terraformを実行する。
# terraform apply "tfplan" google_storage_bucket_object.text: Destroying... (ID: curry999904-butterfly02) google_storage_bucket_object.text: Destruction complete after 0s google_storage_bucket_object.text: Creating... bucket: "" => "curry999904" content: "" => "dddddddddddddddddd" content_type: "" => "" crc32c: "" => "" detect_md5hash: "" => "different hash" md5hash: "" => "" name: "" => "butterfly02" storage_class: "" => "" google_storage_bucket_object.text: Creation complete after 1s (ID: curry999904-butterfly02) Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
2.6.terraformの状態を確認する。
# terraform show google_storage_bucket.COLDLINE: id = curry999904 cors.# = 0 force_destroy = false labels.% = 0 location = ASIA-NORTHEAST1 name = curry999904 project = self_link = storage_class = COLDLINE url = gs://curry999904 versioning.# = 0 google_storage_bucket_object.picture: id = curry999904-butterfly01 bucket = curry999904 cache_control = content_disposition = content_encoding = content_language = content_type = application/octet-stream crc32c = NzUi7w== detect_md5hash = 3qWaTCGyUkCjOUnwQlTIJg== md5hash = 3qWaTCGyUkCjOUnwQlTIJg== name = butterfly01 source = sample.eps storage_class = COLDLINE google_storage_bucket_object.text: id = curry999904-butterfly02 bucket = curry999904 cache_control = content = dddddddddddddddddd content_disposition = content_encoding = content_language = content_type = text/plain; charset=utf-8 crc32c = zTdyag== detect_md5hash = pBy73t+FQ949imvKMfWfVA== md5hash = pBy73t+FQ949imvKMfWfVA== name = butterfly02 storage_class = COLDLINE
3.Google Cloud Storageのバケットを削除する
3.1.terraformのtfファイルを修正する。
# cat gcs.tf variable "cpath" { } variable "project" { } variable "region" { } provider "google" { credentials = "${file("${var.cpath}")}" project = "${var.project}" region = "${var.region}" } resource "google_storage_bucket" "COLDLINE" { name = "curry999904" storage_class = "COLDLINE" location = "asia-northeast1" } resource "google_storage_bucket_object" "picture" { name = "butterfly01" source = "sample.eps" bucket = "${google_storage_bucket.COLDLINE.name}" } resource "google_storage_bucket_object" "text" { name = "butterfly02" content = "dddddddddddddddddd" bucket = "${google_storage_bucket.COLDLINE.name}" }
3.2.terraformの環境変数ファイルを作成する。
# cat config.tfvars cpath="/var/credentials/account.json" project="projcet-crossbar-999999" region="asia-northeast1"
3.3.Terraformを初期化する。
# terraform init Initializing provider plugins... - Checking for available provider plugins on https://releases.hashicorp.com... - Downloading plugin for provider "google" (1.4.0)... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.google: version = "~> 1.4" Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
3.4.Terraformを実行する。
# terraform destroy -force -var-file=config.tfvars google_storage_bucket.COLDLINE: Refreshing state... (ID: curry999904) google_storage_bucket_object.picture: Refreshing state... (ID: curry999904-butterfly01) google_storage_bucket_object.text: Refreshing state... (ID: curry999904-butterfly02) google_storage_bucket_object.picture: Destroying... (ID: curry999904-butterfly01) google_storage_bucket_object.text: Destroying... (ID: curry999904-butterfly02) google_storage_bucket_object.text: Destruction complete after 0s google_storage_bucket_object.picture: Destruction complete after 0s google_storage_bucket.COLDLINE: Destroying... (ID: curry999904) google_storage_bucket.COLDLINE: Destruction complete after 1s Destroy complete! Resources: 3 destroyed.
3.5.terraformの状態を確認する。
# terraform show
まとめ
リファクタリングの実行は行っていません。
必要に応じてリファクタリングを実施してください。
Terraformを使用する方のお役にたてれば幸いです。
コメントを残す