# Terraform 経由でのデプロイ

以下の内容を、Terraformでデプロイする他の標準設定と併せて環境に追加する必要があります。

### ホスト

`fastly_service_vcl` リソースに2つの[Host/Backend](https://docs.netacea.com/jp/netacea-puraguin/fastly/installation-and-configuration/..#host)変数を追加する必要があります：

```
  # Netacea backends 
  backend {
    name = "MitSvc"
    address = "geo-mitigations.netacea.net"
    use_ssl = true
    override_host = "geo-mitigations.netacea.net"
    ssl_cert_hostname = "geo-mitigations.netacea.net"
    ssl_sni_hostname = "geo-mitigations.netacea.net"
    port = "443"
    request_condition = "Netacea Forward Request"
  }

  backend {
    name = "CaptchaAssets"
    address = "assets.ntcacdn.net"
    use_ssl = true
    override_host = "assets.ntcacdn.net"
    ssl_cert_hostname = "assets.ntcacdn.net"
    ssl_sni_hostname = "assets.ntcacdn.net"
    port = "443"
    request_condition = "Netacea Forward Request"
  }

  # Prevent Netacea backends from being selected as default
  condition {
    name = "Netacea Forward Request"
    statement = "false"
    type = "REQUEST"
  }
```

### ディクショナリ

`fastly_service_vcl` リソースにディクショナリを追加する必要があります：

```
 # Defines the Netacea dictionary
 dictionary {
    name = "netacea_edge_config"
  }
```

ディクショナリ内の変数を設定するには、`fastly_service_dictionary` リソースを使用します：

{% hint style="info" %}
Netaceaと高度な設定について協議していない場合、変数を定義する必要はありません。
{% endhint %}

```
# Define the config values
  items = {
    api_key: var.netacea_api_key
    secret_key: var.netacea_secret_key
    encryption_key: var.netacea_encryption_key
    integration_mode: "MITIGATE"
    cookie_name: "cookieName"
    captcha_cookie_name: "cCookieName"
  }

  # Find the netacea_edge_config dictionary
  for_each = {
    for d in fastly_service_vcl.example.dictionary : d.name => d if d.name == "netacea_edge_config"
  }

  # Link config values to the netacea_edge_config dictionary
  service_id = fastly_service_vcl.example.id
  dictionary_id = each.value.dictionary_id

  # Declare that dictionary values must be managed by Terraform.
  # Changes to this dictionary applied in any other way will be overwritten.
  manage_items = true

```

`fastly_service_dictionary` リソースの詳細については、[Fastly Provider docs](https://registry.terraform.io/providers/fastly/fastly/latest/docs/resources/service_dictionary_items)を参照してください。

また、環境内で以下の変数が定義されていることを確認する必要があります：

```
variable "netacea_api_key" {
  description = "Defines which Netacea Datastream is called"
  type = string
  sensitive = true
}

variable "netacea_secret_key" {
  description = "Netacea Datastream Secret"
  type = string
  sensitive = true
}

variable "netacea_encryption_key" {
  description = "Encrypts Netacea cookie"
  type = string
  sensitive = true
}
```

変数には、関連する[Netacea Datastream](https://docs.netacea.com/jp/netacea-puraguin/accessing-your-integration-settings)に記載されている値を設定する必要があります。値の設定方法の詳細については[こちら](https://developer.hashicorp.com/terraform/language/values/variables)を参照してください。

### VCLスニペット

Netaceaから7つの[VCLスニペット](https://docs.netacea.com/jp/netacea-puraguin/fastly/installation-and-configuration/..#vcl-snippets)が別途提供されます。ファイルはVCLスニペットを格納する該当ディレクトリに追加してください。

また、`fastly_service_vcl` リソースでこれらを参照する必要があります：

```
snippet {
    name     = "netacea_integration_init"
    type     = "init"
    content  = file("./netacea/netacea_init.vcl")
    priority = 45
  }

  snippet {
    name     = "netacea_integration_recv"
    type     = "recv"
    content  = file("./netacea/netacea_recv.vcl")
    priority = 45
  }

  snippet {
    name     = "netacea_integration_recv_configure"
    type     = "recv"
    content  = file("./netacea/netacea_recv_configure.vcl")
    priority = 46
  }

  snippet {
    name     = "netacea_integration_error"
    type     = "error"
    content  = file("./netacea/netacea_error.vcl")
    priority = 45
  }

  snippet {
    name     = "netacea_integration_deliver"
    type     = "deliver"
    content  = file("./netacea/netacea_deliver.vcl")
    priority = 45
  }
  
    snippet {
    name     = "netacea_integration_pass"
    type     = "pass"
    content  = file("./netacea/netacea_pass.vcl")
    priority = 45
  }
  
    snippet {
    name     = "netacea_integration_miss"
    type     = "miss"
    content  = file("./netacea/netacea_miss.vcl")
    priority = 45
  }
  
```

{% hint style="info" %}
各contentフィールドを、Netacea VCLが格納されているディレクトリに更新してください。
{% endhint %}

## デプロイ

これでNetacea統合をデプロイする準備が整いました。

`fastly_service_vcl` リソースの詳細については、[Fastly provider docs](https://registry.terraform.io/providers/fastly/fastly/latest/docs/resources/service_vcl)を参照してください。
