Skip to content

Instantly share code, notes, and snippets.

@hrmsk66
Last active July 12, 2024 05:05
Show Gist options
  • Save hrmsk66/3f59e935565a2843839a03fde29c5b69 to your computer and use it in GitHub Desktop.
Save hrmsk66/3f59e935565a2843839a03fde29c5b69 to your computer and use it in GitHub Desktop.
ACL を Terraform で管理する

既存の Fastly VCL サービスの ACL を Terraform で管理するための手順を記載します。

前提条件

  • 環境変数 FASTLY_API_KEY に対象サービスへの変更権限をもつ API トークンがセットされていること
  • terraform バイナリのバージョンが 1.5 以降であること

1. Fastly API で ACL の ID を確認する

curl -s https://api.fastly.com/service/<service-id>/version/<version>/acl -H "fastly-key: <token>" | jq .

この出力例では WmvnU24Eu7uYjzQwLHvZ56 が ACL の ID です。

[
  {
    "version": "2",
    "deleted_at": null,
    "service_id": "GAehIx2mU69NoLSfNILve4",
    "created_at": "2024-07-11T05:18:22Z",
    "updated_at": "2024-07-11T05:18:22Z",
    "id": "WmvnU24Eu7uYjzQwLHvZ56",
    "name": "block_list"
  }
]

2. リソースを import するための TF ファイルをつくる

import ブロックの id の値はこの例のように <service-id>/<acl-id> の形式にします。

terraform {
  required_providers {
    fastly  = {
      source  = "fastly/fastly"
      version = "~> 5.11.0"
    }
  }
}

import {
  id = "GAehIx2mU69NoLSfNILve4/WmvnU24Eu7uYjzQwLHvZ56"
  to = fastly_service_acl_entries.block_list
}

3. terraform コマンドを実行して ACL の定義ファイルをつくる

terraform init
terraform plan -generate-config-out=acl.tf

成功すると以下のようなファイル (acl.tf) が生成されているはずです。

# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "GAehIx2mU69NoLSfNILve4/WmvnU24Eu7uYjzQwLHvZ56"
resource "fastly_service_acl_entries" "block_list" {
  acl_id         = "WmvnU24Eu7uYjzQwLHvZ56"
  manage_entries = null
  service_id     = "GAehIx2mU69NoLSfNILve4"
  entry {
    comment = null
    ip      = "192.168.1.2"
    negated = false
    subnet  = null
  }
  entry {
    comment = null
    ip      = "192.168.1.1"
    negated = false
    subnet  = null
  }
}

4. 値が null になっているフィールドを変更する

4-1. manage_entry には true をセットする

デフォルトでは ACL のエントリは TF の管理対象外なので、この設定で管理対象にしています。

4-2. null がセットされている subnet, commnent には空の文字列 "" をセットする

# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "GAehIx2mU69NoLSfNILve4/WmvnU24Eu7uYjzQwLHvZ56"
resource "fastly_service_acl_entries" "block_list" {
  acl_id         = "WmvnU24Eu7uYjzQwLHvZ56"
  manage_entries = true
  service_id     = "GAehIx2mU69NoLSfNILve4"
  entry {
    comment = ""
    ip      = "192.168.1.2"
    negated = false
    subnet  = ""
  }
  entry {
    comment = ""
    ip      = "192.168.1.1"
    negated = false
    subnet  = ""
  }
}

5. terraform apply を実行してステートファイルをつくる

terraform apply を実行します。manage_entriestrue にしたので変更として検出されるはずです。 その他に想定していない変更がリストされていないことを確認して変更を適用します(プロンプトに yes で応答)。 このステップの完了後 terraform.tfstate が生成されているはずです。

terraform apply

( snip )

Apply complete! Resources: 1 imported, 0 added, 1 changed, 0 destroyed.

6. 変数を定義して ACL エントリのリストを別ファイルで管理する (Optional)

import した Terraform ファイルには ACL エントリがベタ書きされています。

以下のようにファイルを変更・作成すると別ファイルに定義した ACL エントリのリストを読み込ませるようにすることができます。

acl.tf

# __generated__ by Terraform
# Please review these resources and move them into your main configuration files.

# __generated__ by Terraform from "GAehIx2mU69NoLSfNILve4/WmvnU24Eu7uYjzQwLHvZ56"
resource "fastly_service_acl_entries" "block_list" {
  acl_id         = "WmvnU24Eu7uYjzQwLHvZ56"
  manage_entries = true
  service_id     = "GAehIx2mU69NoLSfNILve4"
  dynamic "entry" {
    for_each = var.acl_entries
    content {
      ip      = entry.value.ip
      comment = entry.value.comment
      negated = entry.value.negated
      subnet  = entry.value.subnet
    }
  }
}

variables.tf

variable "acl_entries" {
  description = "List of IP addresses to block"
  type = list(object({
    ip      = string
    comment = optional(string, "")
    negated = optional(bool, false)
    subnet  = optional(string, "")
  }))
}

terraform.tfvars

acl_entries = [
  {
    ip      = "192.168.1.2"
  },
  {
    ip      = "192.168.1.1"
  }
]

terraform plan を実行して変更が検出されなければ OK です。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment