0
0

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未対応のAWSリソースの管理方法はマネコンの通信に答えがあった

0
Posted at

この記事は人間が書いて、 AI(Claude)に文章体裁を整えています。
内容は執筆時点の情報です。

はじめに

職場でTerraformを使ってるチームに所属しているのですが、
Amazon Bedrock AgentCore Gateway リソースのログ設定(Invocation Logging) が
Terraformでは設定できなかったようで、マネコンで設定する手法をとっていました。

調べてみると、Terraform Registry で aws_bedrockagentcore_gateway リソースを調べると、
Gateway の作成・認証・MCP 設定などはあるものの、
Invocation Logging に関する設定項目が見当たりません。

そもそもAPIがないようで当時は未対応なのかそういうこともあるのかと思っていましたが、
ふと、「じゃあマネコン操作はどうやってるんだ?」と思い、
その調査と検証の記録をまとめます。

結論

Terraform だけで AgentCore Gateway の Invocation Logging を設定できます。

ただし aws_bedrockagentcore_gateway リソースに設定項目はなく、
CloudWatch Logs の Vended Logs Delivery API を使う必要があります。

調査:AgentCore 専用 API は存在するのか

まず AgentCore 側に Invocation Logging 用の API があるかを確認しました。

aws bedrock-agentcore-control help | grep -i "log\|invoc"

結果は 0件
bedrock-agentcore-control CLI にはログ・Invocation 関連のコマンドが存在しません。

boto3 でも同様に確認しました。

import boto3
client = boto3.client('bedrock-agentcore-control', region_name='us-east-1')
methods = [m for m in dir(client) if 'log' in m.lower() or 'invoc' in m.lower()]
# → []

こちらも 0件
AgentCore 側には Invocation Logging を設定する専用 API が存在しないことがわかりました。

では、マネコンはどうやって設定しているのか。通信を解析して調べました。

調査:マネコンの通信を解析した

Terraformリソースに設定項目がない場合、
マネコンが何を呼んでいるかを調べるのが早いです。
基本的にマネコン操作は裏でAPIを叩いているので操作方法がわかるからですね。

ブラウザの DevTools(Network タブ)で Invocation Logging を保存したときの
通信をキャプチャすると、AgentCore 専用の API は一切呼ばれておらず、
以下の CloudWatch Logs API だけが呼ばれていました。

Logs_20140328.PutDeliverySource
Logs_20140328.PutDeliveryDestination
Logs_20140328.CreateDelivery

PutDeliverySource のリクエストボディはこうなっていました。

{
  "name": "WdDeliverySource-xxxxxxxx",
  "logType": "APPLICATION_LOGS",
  "resourceArn": "arn:aws:bedrock-agentcore:us-east-1:ACCOUNT:gateway/GATEWAY_ID"
}

resourceArn に AgentCore Gateway の ARN を渡しているだけです。

CloudWatch Logs Vended Logs Delivery とは

AWS の一部サービスは、
CloudWatch Logs の Vended Logs Delivery という仕組みを使ってログを出力します。

この仕組みは以下の 3 リソースで構成されます。

リソース 役割
Delivery Source ログを送り出す AWS リソース(今回は AgentCore Gateway)
Delivery Destination ログの送り先(CloudWatch Logs、S3、Firehose など)
Delivery Source と Destination を紐付ける

AgentCore Gateway はこの仕組みに対応しており、
PutDeliverySourceresourceArn に Gateway ARN を渡せます。

Terraform で書く

上記の 3 つの操作に対応する Terraform リソースがすでに存在します。

aws_cloudwatch_log_delivery_source
aws_cloudwatch_log_delivery_destination
aws_cloudwatch_log_delivery

実際に動いた Terraform コードは以下のとおりです。

# CloudWatch Logs ロググループ(送り先)
resource "aws_cloudwatch_log_group" "agentcore_gateway" {
  name              = "/aws/bedrock-agentcore/gateway/invocation-logs"
  retention_in_days = 7
}

# Delivery Source: AgentCore Gateway ARN を指定
resource "aws_cloudwatch_log_delivery_source" "agentcore_gateway" {
  name         = "agentcore-gateway-invocation-source"
  log_type     = "APPLICATION_LOGS"
  resource_arn = "arn:aws:bedrock-agentcore:us-east-1:ACCOUNT_ID:gateway/GATEWAY_ID"
}

# Delivery Destination: ロググループを指定
resource "aws_cloudwatch_log_delivery_destination" "agentcore_gateway" {
  name = "agentcore-gateway-invocation-destination"

  delivery_destination_configuration {
    destination_resource_arn = aws_cloudwatch_log_group.agentcore_gateway.arn
  }
}

# Delivery: Source と Destination を紐付け
resource "aws_cloudwatch_log_delivery" "agentcore_gateway" {
  delivery_source_name     = aws_cloudwatch_log_delivery_source.agentcore_gateway.name
  delivery_destination_arn = aws_cloudwatch_log_delivery_destination.agentcore_gateway.arn
}

resource_arn には aws_bedrockagentcore_gateway リソースの arn 属性を参照すれば、
依存関係も自動で解決されます。

resource_arn = aws_bedrockagentcore_gateway.my_gateway.arn

検証結果

terraform apply を実行したところ、4 リソースすべての作成に成功しました。

aws_cloudwatch_log_group.agentcore_gateway: Creation complete
aws_cloudwatch_log_delivery_destination.agentcore_gateway: Creation complete
aws_cloudwatch_log_delivery_source.agentcore_gateway: Creation complete
aws_cloudwatch_log_delivery.agentcore_gateway: Creation complete

Apply complete! Resources: 4 added, 0 changed, 0 destroyed.

マネコンの Invocation Logging 画面でも設定が反映されていることを確認しました。

image.png

まとめ

  • aws_bedrockagentcore_gateway リソースに Invocation Logging の設定項目はありません
  • ただし Terraform 未対応ではなく、CloudWatch Logs Delivery API 経由で設定する仕組みになっています
  • aws_cloudwatch_log_delivery_source / aws_cloudwatch_log_delivery_destination / aws_cloudwatch_log_delivery の 3 リソースを使えば Terraform だけで完結します
  • 1 Gateway に対して Delivery Source は 1 つだけという制約があるので注意が必要です

「Terraform でできない」と思ったときは、マネコンの通信を確認してみると意外と別経路で設定できることがあります。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?