[A-00021]GCPでTerraformを使用する。

GoogleCloudPlatformでTerraformを利用する方法を記載する。

GCPではデフォルトでTerraformが利用可能である。画面右上にある[Cloud Shell]ボタンを押下してCloudシェルコンソールを表示する。

・Terraformが有効であるかを確認する。

anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ terraform -v
Terraform v1.4.6
on linux_amd64 

・テスト用フォルダを作成する。

anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ mkdir test
anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ chmod 7777
anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ cd ./test

次にterraformで使用するスクリプトファイルを作成する。

拡張子は[.tf]である事に注意

・main.tf(viで編集・作成)

anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ touch main.tf
anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ chmod 777 main.tf 
anonymous@cloudshell:~/test (xxx-xxxxx-xxxxxx)$ vi main.tf

// VPCリソース
resource "google_compute_network" "vpc" {
  name = "my-vpc"
  auto_create_subnetworks = false
  mtu = 1496
}

terraformではまず使用するcurrent-directoryを初期化する必要がある。

下記のコマンドを実行する。

・current-directoryを初期化する。

anonymous@cloudshell$ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/google...
- Installing hashicorp/google v4.64.0...
- Installed hashicorp/google v4.64.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.
anonymous@cloudshell$ 

次に実行対象ファイルのリソースを計算する。

・リソース量を計算・文法チェックする。

anonymous@cloudshell$ terraform plan

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:

  # google_compute_network.vpc will be created
  + resource "google_compute_network" "vpc" {
      + auto_create_subnetworks                   = false
      + delete_default_routes_on_create           = false
      + gateway_ipv4                              = (known after apply)
      + id                                        = (known after apply)
      + internal_ipv6_range                       = (known after apply)
      + mtu                                       = 1496
      + name                                      = "my-vpc"
      + network_firewall_policy_enforcement_order = "AFTER_CLASSIC_FIREWALL"
      + project                                   = "my-project"
      + routing_mode                              = (known after apply)
      + self_link                                 = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now. 

最後に実行命令を行う。

・applyコマンドで環境構築を実行する。

anonymous@cloudshell$ terraform apply


Terraform will perform the following actions:

  # google_compute_network.vpc will be created
  + resource "google_compute_network" "vpc" {
      + auto_create_subnetworks                   = false
      + delete_default_routes_on_create           = false
      + gateway_ipv4                              = (known after apply)
      + id                                        = (known after apply)
      + internal_ipv6_range                       = (known after apply)
      + mtu                                       = 1496
      + name                                      = "my-vpc"
      + network_firewall_policy_enforcement_order = "AFTER_CLASSIC_FIREWALL"
      + project                                   = (known after apply)
      + routing_mode                              = (known after apply)
      + self_link                                 = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

google_compute_network.vpc: Creating...
google_compute_network.vpc: Still creating... [10s elapsed]
google_compute_network.vpc: Still creating... [20s elapsed]
google_compute_network.vpc: Creation complete after 23s [id=projects/xxxxxx/global/networks/my-vpc]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

gcpコンソールから[VPCネットワーク]を確認すると下記のように[default]とは別にVPCが作成されているはずである。

使用して用が済んだら環境を撤去する。下記のdestoyコマンドを用いる。

・環境を撤去する。

anonymous@cloudshell$ terraform destroy
Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

google_compute_network.vpc: Destroying... [id=projects/xxxxxx/global/networks/my-vpc]
google_compute_network.vpc: Still destroying... [id=projects/xxxxxx/global/networks/my-vpc, 10s elapsed]
google_compute_network.vpc: Destruction complete after 12s

Destroy complete! Resources: 1 destroyed.

コメントを残す

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

*