2&>1

AWSとかGCPとかGolangとかとか

TerraformでGCPを試す

GCP環境向けに作ってみる

0.前提

・すでにGCPアカウントを持っている
・プロジェクト作成済

1.APIの有効化
  GCPコンソール画面から
  「APIサービス」→「ライブラリ」
   以下を検索し有効化されていればOK。されてなければ有効化
   Google Compute Engine API

2. credentialファイル の準備
   「APIとサービス」→「認証情報」→「認証情報を作成」→「サービスアカウントキー

以下の内容で作成

作成したものをローカルにダウンロードしてterraform実行サーバにアップする

3.環境変数追加

  その前に先程のcredentialファイルを作業フォルダに移動
認証情報を追加する

export GOOGLE_CLOUD_KEYFILE_JSON={{credentialファイルへのパス}}

4.コードを書く

[root@terraform terraform_gcp]# vi instance.tf
provider "google" {
  project = "{{作成するプロジェクトID}}"
  region  = "us-central1"
  zone    = "us-central1-c"
}

resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "f1-micro"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-9"
    }
  }

  network_interface {
    # A default network is created for all GCP projects
    network       = "${google_compute_network.vpc_network.self_link}"
    access_config = {
    }
  }
}

resource "google_compute_network" "vpc_network" {
  name                    = "terraform-network"
  auto_create_subnetworks = "true"
}

5.コードを実行

[root@terraform terraform_gcp]# terraform apply

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_compute_instance.vm_instance
      id:                                                  <computed>
      boot_disk.#:                                         "1"
      boot_disk.0.auto_delete:                             "true"
      boot_disk.0.device_name:                             <computed>
      boot_disk.0.disk_encryption_key_sha256:              <computed>
      boot_disk.0.initialize_params.#:                     "1"
      boot_disk.0.initialize_params.0.image:               "debian-cloud/debian-9"
      boot_disk.0.initialize_params.0.size:                <computed>
      boot_disk.0.initialize_params.0.type:                <computed>
      can_ip_forward:                                      "false"
      cpu_platform:                                        <computed>
      create_timeout:                                      <computed>
      deletion_protection:                                 "false"
      guest_accelerator.#:                                 <computed>
      instance_id:                                         <computed>
      label_fingerprint:                                   <computed>
      machine_type:                                        "f1-micro"
      metadata_fingerprint:                                <computed>
      name:                                                "terraform-instance"
      network_interface.#:                                 "1"
      network_interface.0.access_config.#:                 "1"
      network_interface.0.access_config.0.assigned_nat_ip: <computed>
      network_interface.0.access_config.0.nat_ip:          <computed>
      network_interface.0.access_config.0.network_tier:    <computed>
      network_interface.0.address:                         <computed>
      network_interface.0.name:                            <computed>
      network_interface.0.network:                         "${google_compute_network.vpc_network.self_link}"
      network_interface.0.network_ip:                      <computed>
      network_interface.0.subnetwork_project:              <computed>
      project:                                             <computed>
      scheduling.#:                                        <computed>
      self_link:                                           <computed>
      tags_fingerprint:                                    <computed>
      zone:                                                <computed>

  + google_compute_network.vpc_network
      id:                                                  <computed>
      auto_create_subnetworks:                             "true"
      gateway_ipv4:                                        <computed>
      name:                                                "terraform-network"
      project:                                             <computed>
      routing_mode:                                        <computed>
      self_link:                                           <computed>


Plan: 2 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_network: Creating...
  auto_create_subnetworks: "" => "true"
  gateway_ipv4:            "" => "<computed>"
  name:                    "" => "terraform-network"
  project:                 "" => "<computed>"
  routing_mode:            "" => "<computed>"
  self_link:               "" => "<computed>"
google_compute_network.vpc_network: Still creating... (10s elapsed)
google_compute_network.vpc_network: Still creating... (20s elapsed)
google_compute_network.vpc_network: Still creating... (30s elapsed)
google_compute_network.vpc_network: Creation complete after 38s (ID: terraform-network)
google_compute_instance.vm_instance: Creating...
  boot_disk.#:                                         "" => "1"
  boot_disk.0.auto_delete:                             "" => "true"
  boot_disk.0.device_name:                             "" => "<computed>"
  boot_disk.0.disk_encryption_key_sha256:              "" => "<computed>"
  boot_disk.0.initialize_params.#:                     "" => "1"
  boot_disk.0.initialize_params.0.image:               "" => "debian-cloud/debian-9"
  boot_disk.0.initialize_params.0.size:                "" => "<computed>"
  boot_disk.0.initialize_params.0.type:                "" => "<computed>"
  can_ip_forward:                                      "" => "false"
  cpu_platform:                                        "" => "<computed>"
  create_timeout:                                      "" => "<computed>"
  deletion_protection:                                 "" => "false"
  guest_accelerator.#:                                 "" => "<computed>"
  instance_id:                                         "" => "<computed>"
  label_fingerprint:                                   "" => "<computed>"
  machine_type:                                        "" => "f1-micro"
  metadata_fingerprint:                                "" => "<computed>"
  name:                                                "" => "terraform-instance"
  network_interface.#:                                 "" => "1"
  network_interface.0.access_config.#:                 "" => "1"
  network_interface.0.access_config.0.assigned_nat_ip: "" => "<computed>"
  network_interface.0.access_config.0.nat_ip:          "" => "<computed>"
  network_interface.0.access_config.0.network_tier:    "" => "<computed>"
  network_interface.0.address:                         "" => "<computed>"
  network_interface.0.name:                            "" => "<computed>"
  network_interface.0.network:                         "" => "https://www.googleapis.com/compute/v1/projects/infra-team/global/networks/terraform-network"
  network_interface.0.network_ip:                      "" => "<computed>"
  network_interface.0.subnetwork_project:              "" => "<computed>"
  project:                                             "" => "<computed>"
  scheduling.#:                                        "" => "<computed>"
  self_link:                                           "" => "<computed>"
  tags_fingerprint:                                    "" => "<computed>"
  zone:                                                "" => "<computed>"
google_compute_instance.vm_instance: Still creating... (10s elapsed)
google_compute_instance.vm_instance: Still creating... (20s elapsed)
google_compute_instance.vm_instance: Creation complete after 30s (ID: terraform-instance)

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

とりあえずインスタンスを作成するまでは完了。

credentialファイルの作成後サーバにコピペしたときにコピペミスしててエラーとなってたのにはまった。。

ちゃんとアップロードしないと失敗の原因になる(白目

触りも触り程度の内容なので次回はもっと難しいことしたい