ふり返る暇なんて無いね

日々のメモ書きをつらつらと。メインブログに書くほどでもないことを流してます

豆: Terraformのcurlプロバイダーよりhttpプロバイダーを使ったほうが良さそう

diary.masasuzu.net

前回 curl プロバイダーを紹介したのですが、 httpプロバイダーを使ったほうが望ましいので前回のサンプルコードを書き換えてみます。パラメータが多少変わるくらいですね。

ドキュメントを見ればわかるのですが、プロバイダー名はcurlなのですが、書き方としてAzure ADを前提にしたような書き方をされていて、品質に不安を覚えます。

それに対して、httpプロバイダーは汎用的にhttpリクエストを送るために書かれており、またhashicorp公式が出しているという安心感があります。

data "http" "github_meta" {
  method = "GET"
  url    = "https://api.github.com/meta"
}

locals {
  github_meta              = jsondecode(data.http.github_meta.response_body)
  github_webhook_cidr      = local.github_meta.hooks
  github_webhook_cidr_ipv4 = [for x in local.github_meta.hooks : x if length(split(".", x)) == 4]
  github_webhook_cidr_ipv6 = [for x in local.github_meta.hooks : x if length(split(":", x)) > 1]

}

resource "aws_ec2_managed_prefix_list" "main" {
  name           = "GitHub Hooks"
  address_family = "IPv4"
  max_entries    = length(local.github_webhook_cidr_ipv4)

  dynamic "entry" {
    for_each = local.github_webhook_cidr_ipv4
    content {
      cidr = entry.value
    }
  }
}

resource "aws_security_group" "main" {
  name   = "Allow GitHub Webhooks"
  vpc_id = var.vpc_id
}

resource "aws_vpc_security_group_ingress_rule" "main" {
  security_group_id = aws_security_group.main.id
  ip_protocol       = "tcp"
  from_port         = 443
  to_port           = 443
  prefix_list_id    = aws_ec2_managed_prefix_list.main.id
}

output "github_webhook_cidr_ipv4" {
  value = local.github_webhook_cidr_ipv4
}

output "github_webhook_cidr_ipv6" {
  value = local.github_webhook_cidr_ipv6
}