コンテンツにスキップ

Terraform

Terraformは、標準化された構成構文を使用して異なるプロバイダーからサービスをデプロイすることを可能にするインフラストラクチャコードソフトウェアツールです。Terraform構成ファイルを作成する際には、手順を段階的に定義するのではなく、構成の最終状態を定義します。これにより、他のインフラストラクチャとともにトンネルを簡単にデプロイ、変更、管理できます。

このガイドでは、Terraformを使用して以下をデプロイします:

  • HTTPテストサーバーを実行するGoogle Cloud Project (GCP)の仮想マシン
  • インターネット上でサーバーを利用可能にするCloudflareトンネル
  • サーバーに接続できるユーザーを定義するCloudflare Accessポリシー

前提条件

以下の手順を完了するには、次のものが必要です:

1. Terraformをインストールする

お使いのオペレーティングシステムに応じたTerraformインストールガイドを参照してください。

2. gcloud CLIをインストールする

  1. gcloud CLIをインストールして、TerraformがGCPアカウントと対話できるようにします。

  2. 次のコマンドを実行してCLIで認証します:

    Terminal window
    gcloud auth application-default login

3. Cloudflare APIトークンを作成する

TerraformがCloudflareアカウントと対話できるようにするためにAPIトークンを作成します。最低限、トークンには次の権限が含まれている必要があります:

Permission typePermissionAccess level
AccountCloudflare TunnelEdit
AccountAccess: Apps and PoliciesEdit
ZoneDNSEdit

4. 構成ディレクトリを作成する

Terraform functions through a working directory that contains configuration files. You can store your configuration in multiple files or just one — Terraform will evaluate all of the configuration files in the directory as if they were in a single document.

  1. Create a folder for your Terraform configuration:

    Terminal window
    mkdir cloudflare-tf
  2. Change into the directory:

    Terminal window
    cd cloudflare-tf

5. Terraform構成ファイルを作成する

入力変数を定義する

The following variables will be passed into your GCP and Cloudflare configuration.

  1. In your configuration directory, create a .tf file:

    Terminal window
    touch variables.tf
  2. Open the file in a text editor and copy and paste the following:

    # GCP variables
    variable "gcp_project_id" {
    description = "Google Cloud Platform (GCP) project ID"
    type = string
    }
    variable "zone" {
    description = "Geographical zone for the GCP VM instance"
    type = string
    }
    variable "machine_type" {
    description = "Machine type for the GCP VM instance"
    type = string
    }
    # Cloudflare variables
    variable "cloudflare_zone" {
    description = "Domain used to expose the GCP VM instance to the Internet"
    type = string
    }
    variable "cloudflare_zone_id" {
    description = "Zone ID for your domain"
    type = string
    }
    variable "cloudflare_account_id" {
    description = "Account ID for your Cloudflare account"
    type = string
    sensitive = true
    }
    variable "cloudflare_email" {
    description = "Email address for your Cloudflare account"
    type = string
    sensitive = true
    }
    variable "cloudflare_token" {
    description = "Cloudflare API token created at https://dash.cloudflare.com/profile/api-tokens"
    type = string
    sensitive = true
    }

変数に値を割り当てる

  1. In your configuration directory, create a .tfvars file:

    Terminal window
    touch terraform.tfvars

    Terraform will automatically use these variables if the file is named terraform.tfvars, otherwise the variable file will need to be manually passed in.

  2. Add the following variables to terraform.tfvars. Be sure to modify the example with your own values.

    cloudflare_zone = "example.com"
    cloudflare_zone_id = "023e105f4ecef8ad9ca31a8372d0c353"
    cloudflare_account_id = "372e67954025e0ba6aaa6d586b9e0b59"
    cloudflare_email = "user@example.com"
    cloudflare_token = "y3AalHS_E7Vabk3c3lX950F90_Xl7YtjSlzyFn_X"
    gcp_project_id = "testvm-123"
    zone = "us-central1-a"
    machine_type = "e2-medium"

Terraformプロバイダーを構成する

インフラストラクチャをプロビジョニングするために使用されるプロバイダーを宣言する必要があります。

  1. 構成ディレクトリ内に.tfファイルを作成します:

    Terminal window
    touch providers.tf
  2. providers.tfに次のプロバイダーを追加します。randomプロバイダーはトンネルの秘密を生成するために使用されます。

    terraform {
    required_providers {
    cloudflare = {
    source = "cloudflare/cloudflare"
    version = ">= 4.39.0"
    }
    google = {
    source = "hashicorp/google"
    }
    random = {
    source = "hashicorp/random"
    }
    }
    required_version = ">= 0.13"
    }
    # プロバイダー
    provider "cloudflare" {
    api_token = var.cloudflare_token
    }
    provider "google" {
    project = var.gcp_project_id
    }
    provider "random" {
    }

Cloudflareリソースを構成する

次の構成は、Cloudflareアカウントの設定を変更します。

  1. 構成ディレクトリ内に.tfファイルを作成します:

    Terminal window
    touch Cloudflare-config.tf
  2. Cloudflare-config.tfに次のリソースを追加します:

    # トンネルのための64文字の秘密を生成します。
    # `random_password`を使用することで、結果は機密として扱われ、コンソール出力には表示されません。参照:https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password
    resource "random_password" "tunnel_secret" {
    length = 64
    }
    # GCP VMのための新しいローカル管理トンネルを作成します。
    resource "cloudflare_tunnel" "auto_tunnel" {
    account_id = var.cloudflare_account_id
    name = "Terraform GCP tunnel"
    secret = base64sha256(random_password.tunnel_secret.result)
    }
    # http_app.${var.cloudflare_zone}をトンネルにルーティングするCNAMEレコードを作成します。
    resource "cloudflare_record" "http_app" {
    zone_id = var.cloudflare_zone_id
    name = "http_app"
    content = "${cloudflare_tunnel.auto_tunnel.cname}"
    type = "CNAME"
    proxied = true
    }
    # トンネルの設定を作成します。
    resource "cloudflare_tunnel_config" "auto_tunnel" {
    tunnel_id = cloudflare_tunnel.auto_tunnel.id
    account_id = var.cloudflare_account_id
    config {
    ingress_rule {
    hostname = "${cloudflare_record.http_app.hostname}"
    service = "http://httpbin:8080"
    origin_request {
    connect_timeout = "2m0s"
    access {
    required = true
    team_name = "myteam"
    aud_tag = [cloudflare_access_application.http_app.aud]
    }
    }
    }
    ingress_rule {
    service = "http_status:404"
    }
    }
    }
    # 誰が接続できるかを制御するAccessアプリケーションを作成します。
    resource "cloudflare_access_application" "http_app" {
    zone_id = var.cloudflare_zone_id
    name = "Access application for http_app.${var.cloudflare_zone}"
    domain = "http_app.${var.cloudflare_zone}"
    session_duration = "1h"
    }
    # アプリケーションのためのAccessポリシーを作成します。
    resource "cloudflare_access_policy" "http_policy" {
    application_id = cloudflare_access_application.http_app.id
    zone_id = var.cloudflare_zone_id
    name = "Example policy for http_app.${var.cloudflare_zone}"
    precedence = "1"
    decision = "allow"
    include {
    email = [var.cloudflare_email]
    }
    }

    これらのリソースについて詳しく学ぶには、Cloudflareプロバイダーのドキュメントを参照してください。

GCPリソースを構成する

次の構成は、GCP仮想マシンの仕様を定義し、起動時に実行されるスタートアップスクリプトを作成します。

  1. 構成ディレクトリ内に.tfファイルを作成します:

    Terminal window
    touch GCP-config.tf
  2. GCP-config.tfに次の内容を追加します:

    # GCP VMのOSを選択します。
    data "google_compute_image" "image" {
    family = "ubuntu-minimal-2004-lts"
    project = "ubuntu-os-cloud"
    }
    # GCP VMインスタンスをセットアップします。
    resource "google_compute_instance" "origin" {
    name = "test"
    machine_type = var.machine_type
    zone = var.zone
    tags = []
    boot_disk {
    initialize_params {
    image = data.google_compute_image.image.self_link
    }
    }
    network_interface {
    network = "default"
    access_config {
    // 一時的IP
    }
    }
    // インスタンスを一時的にするためのオプション設定
    scheduling {
    preemptible = true
    automatic_restart = false
    }
    // Terraform変数を受け取るスタートアップスクリプトを実行するようにVMを構成します。
    metadata_startup_script = templatefile("./install-tunnel.tpl",
    {
    tunnel_token = cloudflare_tunnel.auto_tunnel.tunnel_token
    })
    }

スタートアップスクリプトを作成する

次のスクリプトは、cloudflaredをインストールし、トンネルのための権限と構成ファイルを作成し、サービスとしてトンネルを実行するように設定します。この例では、接続性をテストするために使用できる軽量HTTPアプリケーションもインストールします。

  1. 構成ディレクトリ内にTerraformテンプレートファイルを作成します:

    Terminal window
    touch install-tunnel.tftpl
  2. テキストエディタでファイルを開き、次のbashスクリプトをコピーして貼り付けます:

    Terminal window
    # CloudflareトンネルとDockerリソースをインストールするためのスクリプト
    # Docker構成
    cd /tmp
    sudo apt-get install software-properties-common
    # このOSのためのdockerリポジトリを取得
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
    # OSが更新され、dockerがインストールされます
    sudo apt update -y && sudo apt upgrade -y
    sudo apt install docker docker-compose -y
    # HTTPBinアプリケーションを追加し、localhost:8080で実行します。
    cat > /tmp/docker-compose.yml << "EOF"
    version: '3'
    services:
    httpbin:
    image: kennethreitz/httpbin
    restart: always
    container_name: httpbin
    ports:
    - 8080:80
    cloudflared:
    image: cloudflare/cloudflared:latest
    restart: always
    container_name: cloudflared
    command: tunnel run --token ${tunnel_token}
    EOF
    cd /tmp
    sudo docker-compose up -d

6. Terraformをデプロイする

To deploy the configuration files:

  1. Initialize your configuration directory:

    Terminal window
    terraform init
  2. Preview everything that will be created:

    Terminal window
    terraform plan
  3. Apply the configuration:

    Terminal window
    terraform apply

GCPインスタンスとトンネルがオンラインになるまでに数分かかる場合があります。新しいトンネル、Accessアプリケーション、およびAccessポリシーはZero Trustで確認できます。新しいDNSレコードはCloudflareダッシュボードで利用可能です。

7. 接続をテストする

  1. ネットワーク > トンネルで、トンネルがアクティブであることを確認します。

  2. アクセス > アプリケーションで、CloudflareのメールがAccessポリシーによって許可されていることを確認します。

  3. 任意のデバイスからブラウザを開き、http_app.<cloudflare_zone>(例:http_app.example.com)にアクセスします。

    最近ログインしていない場合、Accessログインページが表示されます。

  4. Cloudflareのメールでログインします。

    HTTPBinのホームページが表示されるはずです。