AWS Terraform MCP Server を試してみた

AWS Terraform MCP Server を試してみた

Clock Icon2025.04.16

こんにちは!クラウド事業本部コンサルティング部のたかくに(@takakuni_)です。

AWS MCP Servers に新しく、AWS Terraform MCP Server が登場しました。

https://awslabs.github.io/mcp/servers/terraform-mcp-server/

https://github.com/awslabs/mcp?tab=readme-ov-file#aws-terraform-mcp-server

AWS Terraform MCP Server

AWS Terraform MCP Server は名前の通り、HashiCorp Terraform で AWS インフラをホストするときに役立つ MCP サーバーです。

主な機能は次のとおりで、ベストプラクティスに沿った設計がなされているか、静的解析ツールを利用して脆弱な設定が行われていないかなどをチェックできます。

  • Terraform のベストプラクティスをチェック
  • セキュリティファースト開発ワークフロー
  • Checkov 統合
  • AWS I-A (Integration and Automation) GenAI モジュール
  • プロバイダーのドキュメント検索
  • Terraform ワークフローの実行

各機能に簡単にフォーカスしてみます。

Terraform のベストプラクティス

ソースコードを確認すると、Best practices for using the Terraform AWS Provider を元に、ベストプラクティスに沿った設計であるかをチェックするようです。

項目数が多いですが、導入時に一度チーム内で読み合わせすると、良いかもしれません。

https://docs.aws.amazon.com/prescriptive-guidance/latest/terraform-aws-provider-best-practices/introduction.html

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/static/AWS_TERRAFORM_BEST_PRACTICES.md

セキュリティファースト開発ワークフロー

コードを読む限り、Terraform 開発ワークフローの一部を指しているようです。何か Tools が別途用意されているわけではないでした。ワークフローのステップの中で、セキュリティチェックを挟むよう指示が行われているようです。

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/static/TERRAFORM_WORKFLOW_GUIDE.md

Checkov 統合

先ほどのセキュリティファースト開発ワークフローの中で Checkov スキャンが指示されていました。

Tools で定義されているため、「Checkov でスキャンしてみて」で通じそうです。

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/impl/tools/run_checkov_scan.py

AWS I-A (Integration and Automation) GenAI モジュール

AWS I-A (Integration and Automation) GenAI モジュールは、現状、以下の I-A モジュールに対しての検索に利用する機能で Tools で定義されています。

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/impl/tools/search_specific_aws_ia_modules.py

今後、I-A モジュールのラインナップが増えるのでしょうか。要チェックですね。

プロバイダーのドキュメント検索

AWS プロバイダー/AWS CC プロバイダーのドキュメントを検索する機能です。主な動きは Tools で定義されています。各プロバイダーの GitHub URL を生成し、コンテンツを取得、マークダウンへ変換して解析をかけているようです。

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/impl/tools/search_aws_provider_docs.py

その他、Resource でもリソース一覧を取得するコードがありました。(AWS_PROVIDER_RESOURCES.md といったファイルは、プロバイダーの更新を追っかけていくのでしょうか。)

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/impl/resources/terraform_aws_provider_resources_listing.py

Terraform ワークフローの実行

最後に Terraform ワークフローの実行です。こちらは terraform コマンドを実行するための Tools が定義されています。

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/impl/tools/execute_terraform_command.py

Terraform MCP Server Instructions

本 MCP サーバーのインストラクションが用意されていました。チェックしておきましょう。

次の質問を想定して作成されているようです。

  • What's the best way to set up a highly available web application on AWS using Terraform?
    • Terraformを使ってAWS上に高可用性のWebアプリケーションを構築する最適な方法は何ですか?
  • Search for Bedrock modules in the Terraform Registry
    • Terraform RegistryでBedrockモジュールを検索してください
  • Find documentation for awscc_lambda_function resource (specifically AWSCC)
    • awscc_lambda_functionリソース(とくにAWSCC)のドキュメントを探してください
  • Find documentation for aws_lambda_function resource (specifically AWS)
    • aws_lambda_functionリソース(とくにAWS)のドキュメントを探してください
  • Execute terraform plan in my ./infrastructure directory
    • ./infrastructureディレクトリでterraform planを実行してください
  • How can I use the AWS Bedrock module to create a RAG application?
    • AWS Bedrockモジュールを使ってRAGアプリケーションを作成するにはどうすればよいですか?
  • Show me details about the AWS-IA Bedrock Terraform module
    • AWS-IA Bedrock Terraformモジュールの詳細を教えてください
  • Compare the four specific AWS-IA modules for generative AI applications
    • 生成AIアプリケーション向けの4つの特定のAWS-IAモジュールを比較してください
  • Let's develop a secure S3 bucket with proper encryption. I'll follow the development workflow.
    • 適切な暗号化を用いた安全なS3バケットを開発しましょう。開発ワークフローに従います。
  • I need to create Terraform code for a Lambda function. First, let me check the best practices.
    • Lambda関数用のTerraformコードを作成する必要があります。まずベストプラクティスを確認させてください。
  • Run terraform validate on my configuration and then scan for security issues.
    • 設定ファイルでterraform validateを実行し、その後セキュリティ問題をスキャンしてください。
  • Is this VPC configuration secure? Let's scan it with Checkov.
    • このVPC構成は安全ですか?Checkovでスキャンしましょう。
  • Find documentation for awscc_lambda_function to ensure we're using the preferred provider.
    • 推奨プロバイダーを使用していることを確認するため、awscc_lambda_functionのドキュメントを探してください。
  • We need a Bedrock implementation for RAG. Let's search for AWS-IA modules that can help.
    • RAG向けのBedrock実装が必要です。役立つAWS-IAモジュールを検索しましょう。

https://github.com/awslabs/mcp/blob/main/src/terraform-mcp-server/awslabs/terraform_mcp_server/static/MCP_INSTRUCTIONS.md#examples

やってみる

それでは実際に AWS Terraform MCP Server を利用してみましょう。

私は MCP Client に Roo Code を利用しているため、右上の MCP サーバーからグローバル MCP を編集をクリックします。

2025-04-16 at 21.52.45-terraform_aws_provider_resources_listing.py — mcp.png

Installation に従い、mcp.json を編集します。

mcp.json
{
  "mcpServers": {
    "awslabs.terraform-mcp-server": {
      "command": "uvx",
      "args": ["awslabs.terraform-mcp-server@latest"],
      "env": {
        "FASTMCP_LOG_LEVEL": "ERROR"
      },
      "disabled": false,
      "autoApprove": []
    }
  }
}

https://awslabs.github.io/mcp/servers/terraform-mcp-server/#installation

Roo Code の場合、以下のようになっていれば設定完了です。

2025-04-16 at 22.01.00-mcp_settings.json — mcp.png

昔書いたコードをチェックしてみたいと思います。

https://github.com/takakuni-classmethod/DevelopersIO-2024-bedrock/tree/main/terraform/rag

まずは前半戦です。ベストプラクティスに沿っているのか聞いてみました。MCP サーバーが働き、Best practices for using the Terraform AWS Provider を確認しに行っていることがわかります。

2025-04-16 at 22.19.12-knowledgebase.tf — rag.png

最終的な回答結果としては、Checkov の検出結果と組み合わせて、指摘事項が上がってきています。

2025-04-16 at 22.10.13-Visual Studio Code.png

続いて Checkov について聞いてみました。検出結果に基づき具体的にどこが間違っているのかを教えてくれました。どのように是正すべきなのかを、自然言語で解説してくれる部分が非常に便利ですね。

2025-04-16 at 22.11.17-Visual Studio Code.png

Checkov でチェックしていると言うことは、何かしらのレポートが出ているのでは?と気になったため聞いてみました。重要度別の検出結果も追加で聞いたら教えてくれました。

2025-04-16 at 22.12.10-Visual Studio Code.png

Checkov 以外も気になりますね。聞いてみるとズバズバ教えてくれました。

2025-04-16 at 22.25.13-knowledgebase.tf — rag.png

Terraform MCP サーバーと全然関係ないですが、優しく教えてくれる反面もあって Roo Code 良きだなと思いました。

2025-04-16 at 22.16.19-knowledgebase.tf — rag.png

S3 + DynamoDB の内容が気になりますね。 Prescriptive Guidance に引きづられているのか、Terraform v1.11 のアップデートには追いつけませんでした。

2025-04-16 at 22.18.19-knowledgebase.tf — rag.png

最後に AWS Provider のドキュメント検索を試してみます。執筆時点(2025年04月16日)では、 Unreleased な aws_redshift_integration について聞いてみましたが、GitHub 側にはリリースされているため、回答が返ってきていますね。

{"asset_name": "aws_redshift_integration", "asset_type": "resource", "description": "Terraform resource for managing a DynamoDB zero-ETL integration or S3 event integration with Amazon Redshift. You can refer to the [User Guide](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/RedshiftforDynamoDB-zero-etl.html) for a DynamoDB zero-ETL integration or the [User Guide](https://docs.aws.amazon.com/redshift/latest/dg/loading-data-copy-job.html) for a S3 event integration.", "url": "https://raw.githubusercontent.com/hashicorp/terraform-provider-aws/main/website/docs/r/redshift_integration.html.markdown", "example_usage": [{"title": "Basic Usage", "code": "resource \"aws_dynamodb_table\" \"example\" {\n  name           = \"dynamodb-table-example\"\n  read_capacity  = 1\n  write_capacity = 1\n  hash_key       = \"example\"\n\n  attribute {\n    name = \"example\"\n    type = \"S\"\n  }\n\n  point_in_time_recovery {\n    enabled = true\n  }\n}\n\nresource \"aws_redshiftserverless_namespace\" \"example\" {\n  namespace_name = \"redshift-example\"\n}\n\nresource \"aws_redshiftserverless_workgroup\" \"example\" {\n  namespace_name      = aws_redshiftserverless_namespace.example.namespace_name\n  workgroup_name      = \"example-workgroup\"\n  base_capacity       = 8\n  publicly_accessible = false\n\n  subnet_ids = [aws_subnet.example1.id, aws_subnet.example2.id, aws_subnet.example3.id]\n\n  config_parameter {\n    parameter_key   = \"enable_case_sensitive_identifier\"\n    parameter_value = \"true\"\n  }\n}\n\nresource \"aws_redshift_integration\" \"example\" {\n  integration_name = \"example\"\n  source_arn       = aws_dynamodb_table.example.arn\n  target_arn       = aws_redshiftserverless_namespace.example.arn\n}"}, {"title": "Use own KMS key", "code": "data \"aws_caller_identity\" \"current\" {}\n\nresource \"aws_kms_key\" \"example\" {\n  description             = \"example\"\n  deletion_window_in_days = 10\n}\n\nresource \"aws_kms_key_policy\" \"example\" {\n  key_id = aws_kms_key.example.id\n\n  policy = jsonencode({\n    Version = \"2008-10-17\"\n    Statement = [\n      {\n        Effect = \"Allow\"\n        Principal = {\n          AWS = \"arn:aws:iam::${data.aws_caller_identity.current.account_id}:root\"\n        }\n        Action   = \"kms:*\"\n        Resource = \"*\"\n      },\n      {\n        Effect = \"Allow\"\n        Principal = {\n          Service = \"redshift.amazonaws.com\"\n        }\n        Action = [\n          \"kms:Decrypt\",\n          \"kms:CreateGrant\"\n        ]\n        Resource = \"*\"\n        Condition = {\n          StringEquals = {\n            \"aws:SourceAccount\" = data.aws_caller_identity.current.account_id\n          }\n          ArnEquals = {\n            \"aws:SourceArn\" = \"arn:aws:redshift:*:${data.aws_caller_identity.current.account_id}:integration:*\"\n          }\n        }\n      }\n    ]\n  })\n}\n\nresource \"aws_redshift_integration\" \"example\" {\n  integration_name = \"example\"\n  source_arn       = aws_dynamodb_table.example.arn\n  target_arn       = aws_redshiftserverless_namespace.example.arn\n  kms_key_id       = aws_kms_key.example.arn\n\n  additional_encryption_context = {\n    \"example\" : \"test\",\n  }\n}"}], "arguments": [{"name": "integration_name", "description": "(Required) Name of the integration.", "argument_section": "main"}, {"name": "source_arn", "description": "(Required, Forces new resources) ARN of the database to use as the source for replication. You can specify a DynamoDB table or an S3 bucket.", "argument_section": "main"}, {"name": "target_arn", "description": "(Required, Forces new resources) ARN of the Redshift data warehouse to use as the target for replication.\n\nThe following arguments are optional:", "argument_section": "main"}, {"name": "additional_encryption_context", "description": "(Optional, Forces new resources) Set of non-secret key\u2013value pairs that contains additional contextual information about the data.\nFor more information, see the [User Guide](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#encrypt_context).\nYou can only include this parameter if you specify the `kms_key_id` parameter.", "argument_section": "main"}, {"name": "description", "description": "(Optional) Description of the integration.", "argument_section": "main"}, {"name": "kms_key_id", "description": "(Optional, Forces new resources) KMS key identifier for the key to use to encrypt the integration.\nIf you don't specify an encryption key, Redshift uses a default AWS owned key.\nYou can only include this parameter if `source_arn` references a DynamoDB table.", "argument_section": "main"}, {"name": "tags", "description": "(Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.", "argument_section": "main"}], "attributes": [{"name": "arn", "description": "ARN of the Integration."}, {"name": "tags_all", "description": "A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block)."}]}

2025-04-16 at 22.47.18-knowledgebase.tf — rag.png

まとめ

以上、「AWS Terraform MCP Server を試してみた」でした。

Terraform 使いであれば、コーディングのおともにぜひ導入して良さそうな印象を受けました。書き癖も矯正できそうで良さそうです。

このブログがどなたかの参考になれば幸いです。クラウド事業本部コンサルティング部のたかくに(@takakuni_)でした!

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.