2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraform MCP ServerでTROCCOのTerraform Providerを試してみる(つまりCommunity Providerでも使えるよ!)

Posted at

はじめに

18時間前にTerraform公式のMCPが公開されました!これは!!!ということで、TROCCOで使えないか試してみます!!!!!(面白かったので謎にハイテンション)

image.png

image.png

Terraform MCP Serverでできること

執筆時点の2025/5/20現在、Terraform MCP Serverでできることは以下の通りです。

  • Terraformプロバイダーとモジュールの発見を自動化
  • Terraformレジストリからデータの抽出と分析
  • プロバイダーのリソースとデータソースに関する詳細情報の取得
  • Terraformモジュールの探索と理解

レジストリのドキュメントから対応する情報を確認するのはめんどくさいなかなか大変ですが、ドキュメント周りの作業をサポートしてくれるものになっています。

あくまでドキュメント探索にできることが限定されているので、変に危ない動きをしなさそうなのも安心ですね。

Terraform MCP Serverを使ってみる

使えるといっても、どうせオフィシャルプロバイダーだけなのでは?と使う前の私は思っていました。しかしその疑いはいい意味で裏切られることになります。

なお、以下の利用事例はVSCodeのGitHub Copilot Agentを利用しています。

まずは聞いてみる

まずはProviderについて聞いてみます。overviewを読み取って回答してくれます!天才かよ。

GrZYmzYbAAIYycq.jpg

(最初にtroccoについて教えてと言って、回答してもらえなかったのはここだけの話。ちゃんとtrocco-io/troccoと指定しましょう。)

コードを書いてもらう

続いてコードを書いてもらいます。ちょっとややこしい条件も付けてみる。すると、関連するリソースそれぞれについて調べてくれます。この時点で大体あっている予感がしていました。

GrZYtwybAAUMLDR.jpg

tfファイル化してproviderの設定も書いてもらう

最初はIDだけだったリソースもリソースとして追加して、tfファイルとしてまとめてもらいます。

GrZY0GtaUAAlVrf.jpg

生成されたコードは以下の通り。4行だけ修正して、terraform.tfvarsを追加したところ、実行してリソースが作成できました。すごい!

main.tf
# TROCCOリソース一式(MySQL→BigQuery転送、通知・ラベル・スケジュール付き)

resource "trocco_connection" "mysql" {
  connection_type = "mysql"
  name            = "MySQL Example"
  description     = "MySQL接続サンプル"
  host            = "db.example.com"
  port            = 3306
  user_name       = "root"
  password        = "password"
  # database        = "your_database"  これは手動で削除
}

resource "trocco_connection" "bigquery" {
  connection_type = "bigquery"
  name            = "BigQuery Example"
  description     = "BigQuery接続サンプル"
  project_id               = "your_project_id"
  service_account_json_key = <<JSON
  {
    "type": "service_account",
    "project_id": "your_project_id",
    "private_key_id": "your_private_key_id",
    "private_key": "-----BEGIN PRIVATE KEY-----\n..."
  }
  JSON
}

resource "trocco_notification_destination" "slack" {
  type = "slack_channel"
  slack_channel_config = {
    channel     = "#general"
    webhook_url = "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
  }
}

resource "trocco_notification_destination" "email" {
  type = "email"
  email_config = {
    email = "notify@example.com"
  }
}

resource "trocco_label" "sample" {
  name        = "sample-label"
  description = "サンプルラベル"
  color       = "#FF0000"
}

resource "trocco_job_definition" "mysql_to_bigquery" {
  name                     = "mysql_to_bigquery_sample"
  description              = "MySQLからBigQueryへの転送ジョブのサンプル"
  is_runnable_concurrently = false
  retry_limit              = 1

  input_option_type = "mysql"
  input_option = {
    mysql_input_option = {
      database            = "your_database"
      mysql_connection_id = trocco_connection.mysql.id
      input_option_columns = [
        { name = "id", type = "long" },
        { name = "name", type = "string" },
        { name = "email", type = "string" },
        { name = "created_at", type = "timestamp" },
      ]
      query = "SELECT * FROM your_table;"
    }
  }

  output_option_type = "bigquery"
  output_option = {
    bigquery_output_option = {
      bigquery_connection_id = trocco_connection.bigquery.id
      dataset                = "your_dataset"
      table                  = "your_table"
      mode                   = "append"
      location               = "asia-northeast1"
      auto_create_dataset    = true
      # bigquery_output_option_clustering_fields = []  これは手動で追記した
      # bigquery_output_option_merge_keys        = []  これは手動で追記した
      # bigquery_output_option_column_options    = []  これは手動で追記した
    }
  }

  filter_columns = [
    { name = "id", src = "id", type = "long" },
    { name = "name", src = "name", type = "string" },
    { name = "email", src = "email", type = "string" },
    { name = "created_at", src = "created_at", type = "timestamp" },
  ]

  labels = [
    { name = trocco_label.sample.name }
  ]

  notifications = [
    {
      destination_type  = "slack"
      slack_channel_id  = trocco_notification_destination.slack.id
      message           = "ジョブが完了しました"
      notification_type = "job"
      notify_when       = "finished"
    },
    {
      destination_type  = "email"
      email_id          = trocco_notification_destination.email.id
      message           = "ジョブが失敗しました"
      notification_type = "job"
      notify_when       = "failed"
    }
  ]

  schedules = [
    {
      frequency = "daily"
      hour      = 1
      minute    = 0
      time_zone = "Asia/Tokyo"
    }
  ]
}

provider.tf
terraform {
  required_providers {
    trocco = {
      source  = "trocco-io/trocco"
      version = ">= 0.15.0"
    }
  }
}

provider "trocco" {
  api_key = var.trocco_api_key  # 例: 環境変数やtfvarsで設定
  region  = "japan"            # 必要に応じて変更
}

variable "trocco_api_key" {
  description = "TROCCO API Key"
  type        = string
  sensitive   = true
}

おわりに

機能自体は外部からドキュメントを取ってくることに限定されているようですし、割と使いやすいMCPに思います。TROCCOのTerraformを利用する際にも、ぜひ利用を考えてみるとよさそうです!

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?