iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🛠️

Building a GCP Environment with Terraform

に公開

Overview

This article covers the preparation and configuration for managing Google Cloud Platform (GCP) infrastructure using Terraform.

Tools Used

  • Note: Detailed explanations of Terraform or Terragrunt are not covered in this article.

Preparation

Getting Started with the Google provider | Guides | hashicorp/google | Terraform Registry

We will use gcloud auth to generate a credential file to be utilized when executing Terraform. To do this, we first create a Docker environment where both gcloud and Terraform can be used.

# https://hub.docker.com/r/hashicorp/terraform
FROM hashicorp/terraform:1.1.8 as terraform

FROM google/cloud-sdk:377.0.0-alpine

COPY --from=terraform /bin/terraform /usr/local/bin/terraform

RUN apk --update add \
      vim

# https://github.com/gruntwork-io/terragrunt/releases
RUN curl -L "https://github.com/gruntwork-io/terragrunt/releases/download/v0.36.7/terragrunt_linux_amd64" -o /usr/local/bin/terragrunt \
    && chmod +x /usr/local/bin/terragrunt
  • Please change the versions as appropriate.
    In the example above, multi-stage builds are used to copy the Terraform executable into the google/cloud-sdk image for use.

By the way, the repository for the gcloud Dockerfile can be found here:
GoogleCloudPlatform/cloud-sdk-docker: Docker image with all the components of the Google Cloud SDK

Next, create the docker-compose.yml as follows:

version: '3.8'
services:
  infra:
    build: .
    image: xxxx/infra
    container_name: "xxxx-infra"
    volumes:
      - ./:/infrastructure
      - gcloud_config:/root/.config/gcloud
    working_dir: /infrastructure
    entrypoint: bash

volumes:
  gcloud_config:

Account Login and Credential Creation

We will perform various tasks inside the container.

$ docker-compose run --rm infra
$ gcloud auth login --no-launch-browser

↑ A URL will be issued; open it in a web browser, log in with the corresponding account, and then enter the authentication code.

$ gcloud projects list

↑ Check if you are correctly logged in with the right account (a list of projects under the account's management will be displayed).

$ gcloud auth application-default login --no-launch-browser

↑ Execute credential creation. The actual file will be generated under /root/.config/gcloud.
In this case, it is mounted to the gcloud_config volume.

Managing tfstate files in GCP Cloud Storage

First, create a Cloud Storage bucket to store the tfstate.

$ gsutil mb -l asia-northeast1 gs://xxx-tfstate
$ gsutil versioning set on gs://xxx-tfstate
$ gsutil lifecycle set /infrastructure/tfstate_lifecycle.json gs://xxx-tfstate

tfstate_lifecycle.json content is as follows:

{
  "rule":
  [
    {
      "action": {"type": "Delete"},
      "condition": {"numNewerVersions": 5}
    }
  ]
}

↑ I set it up to manage versions and delete older ones.

Samples

That's all for the preparation. Since we're here, let's look at some samples for actually creating resources in GCP.

Sample for creating a CloudSQL instance

This sample case assumes the creation of a CloudSQL instance with a private IP, and creating two access users: one for CloudSQL Proxy and another for Cloud Run access.

# Sample for creating a CloudSQL instance using a private IP
resource "google_sql_database_instance" "master" {
  name             = "xxxxx-db-master"
  database_version = "${var.database_version}"
  region           = "${var.region}"

  settings {
    tier      = "${var.tier}"
    disk_type = "${var.disk_type}"
    disk_size = "${var.disk_size}"

    ip_configuration {
      ipv4_enabled    = false
      require_ssl     = true
      private_network = "${var.private_network}"
      allocated_ip_range = "${var.allocated_ip_range}"
    }
  }
}

# User for CloudSQL Proxy
resource "google_sql_user" "proxy_user" {
  name     = "${var.proxy_user}"
  instance = "${google_sql_database_instance.master.name}"
  host     = "cloudsqlproxy~%"
  password = "${var.proxy_password}"
}

# User for Cloud Run access
resource "google_sql_user" "cloud_run_user" {
  name     = "${var.cloud_run_user}"
  instance = "${google_sql_database_instance.master.name}"
  host     = "%"
  password = "${var.cloud_run_password}"
}

Sample for creating a Cloud Storage bucket

In this sample case, we assume the creation of a bucket where the project owner and a specific service account are granted the roles/storage.admin and roles/storage.objectAdmin roles respectively.

resource "google_storage_bucket" "storage" {
  name          = "xxxx-storage"
  location      = "${var.location}"
  storage_class = "${var.storage_class}"
  uniform_bucket_level_access = true
  force_destroy = true
}

data "google_iam_policy" "role_binding" {
  binding {
    role = "roles/storage.admin"

    members = [
      "projectOwner:{Project ID}",
    ]
  }

  binding {
    role = "roles/storage.objectAdmin"

    members = [
      "serviceAccount:${var.allowed_service_account}",
    ]
  }
}

resource "google_storage_bucket_iam_policy" "policy_binding" {
  bucket      = google_storage_bucket.storage.name
  policy_data = data.google_iam_policy.role_binding.policy_data
}

Reference URLs

Discussion