コンテンツにスキップ

Terraformを使用してアプリケーションを公開する

このガイドでは、Cloudflare Terraformプロバイダーを使用して、プライベートアプリケーションを迅速に公開し、保護する方法を説明します。以下の例では、既存のCloudflareトンネルに新しいパブリックホスト名ルートを追加し、cloudflaredがアプリケーションへのトラフィックをプロキシする方法を構成し、Cloudflare Accessでアプリケーションを保護します。

前提条件

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

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

2. プロバイダーと変数を宣言する

.tfファイルを作成し、以下の例をコピー&ペーストします。APIトークン、アカウントおよびゾーン情報、トンネルIDを入力してください。

トンネルIDを見つける

  1. Zero Trustに移動し、Networks > Tunnelsを選択します。
  2. トンネル名を選択します。
  3. トンネルIDをコピーします。
terraform {
required_providers {
cloudflare = {
source = "cloudflare/cloudflare"
version = "~> 4.0"
}
}
}
provider "cloudflare" {
api_token = "<API-TOKEN>"
}
variable "account_id" {
default = "<ACCOUNT-ID>"
}
variable "zone_id" {
default = "<ZONE-ID>"
}
variable "zone_name" {
default = "mycompany.com"
}
variable "tunnel_id" {
default = "<TUNNEL-ID>"
}

3. Cloudflareリソースを構成する

Terraform構成に以下のリソースを追加します。

Cloudflareトンネルにパブリックホスト名ルートを追加する

cloudflare_tunnel_configリソースを使用して、アプリケーションをパブリックDNSレコードにマッピングするインバウンドルールを作成します。この例では、localhost:8080app.mycompany.comで利用可能にし、接続タイムアウトを設定し、Access JWT検証を有効にします。

resource "cloudflare_tunnel_config" "example_config" {
account_id = var.account_id
tunnel_id = var.tunnel_id
config {
ingress_rule {
hostname = "app.${var.zone_name}"
service = "http://localhost:8080"
origin_request {
connect_timeout = "2m0s"
access {
required = true
team_name = "myteam"
aud_tag = [cloudflare_access_application.example_app.aud]
}
}
}
ingress_rule {
# リクエストが以前のホスト名のいずれにも一致しない場合、`404`ステータスコードで応答します。
service = "http_status:404"
}
}
}

Accessアプリケーションを作成する

cloudflare_access_applicationリソースを使用して、アプリケーションをCloudflare Accessに追加します。

resource "cloudflare_access_application" "example_app" {
zone_id = var.zone_id
name = "Example application"
domain = "app.${var.zone_name}"
type = "self_hosted"
session_duration = "24h"
auto_redirect_to_identity = false
}

Accessポリシーを作成する

cloudflare_access_policyリソースを使用して、アプリケーションを保護するポリシーを作成します。以下のポリシーは、あなたのアイデンティティプロバイダーを通じて認証されたユーザーのみがアクセスできるようにします。

resource "cloudflare_access_policy" "example_policy" {
application_id = cloudflare_access_application.example_app.id
zone_id = var.zone_id
name = "Example policy"
precedence = "1"
decision = "allow"
include {
login_method = ["<IDP-UUID>"]
}
}

4. 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

ユーザーは、パブリックURLにアクセスし、Cloudflare Accessで認証することでプライベートアプリケーションにアクセスできるようになります。新しいトンネルルート、Accessアプリケーション、およびAccessポリシーはZero Trustで確認できます。新しいDNSレコードはCloudflareダッシュボードに表示されます。