🍣

AgentCore Identityに保存した API キーを利用するには、Secret Managerへのアクセス権が必要

に公開

AgentCore Runtime で Strands Agent を実行する際、AgentCore Identity から API キーを取得して外部データへアクセスする場面があります。しかし @requires_api_key デコレータを使用した際にうまく API キーを取得できないケースに遭遇しました。今回はこのケースでの調査と解決方法をお伝えします。

発生した問題

AgentCore Identity を使って API キーを管理し、AgentCore RuntimeにデプロイしたStrands Agentアプリを利用するケースで遭遇しました。外部 APIへアクセスしようとした際に、次のようなエラーメッセージがCloudWatch Logsに残っていました。

ERROR | __main__ | Failed to initialize ESA API key: An error occurred (AccessDeniedException) when calling the GetResourceApiKey operation: The requested operation could not be completed. You are not authorized to perform: secretsmanager:GetSecretValue

このログを見つけるまで少し時間がかかってしまったのですが、AWSのマネコンから調べるよりも、AgentCoreのデプロイコマンド実行結果に表示される AWS CLIコマンドを使う方が見つけやすかったです。

% uv run agentcore launch
🚀 Launching Bedrock AgentCore (codebuild mode - RECOMMENDED)...
   • Build ARM64 containers in the cloud with CodeBuild
   • No local Docker required (DEFAULT behavior)
   • Production-ready deployment

💡 Deployment options:
   • agentcore launch                → CodeBuild (current)
   • agentcore launch --local        → Local development
   • agentcore launch --local-build  → Local build + cloud deploy

...

│ 💡 Tail logs with:                                                                                      │
│    aws logs tail /aws/bedrock-agentcore/runtimes/agent-xxxxx-DEFAULT --follow              │
│    aws logs tail /aws/bedrock-agentcore/runtimes/agent-xxxx-DEFAULT --since 1h

ログを見つけてしまえば簡単ですね。AgentCore Runtimeがsecretsmanager:GetSecretValueの操作権限を持っていないことがエラーにつながっていました。

AgentCore Runtimeに、Secrets Manager へのアクセス権を付与する

AWS や AgentCore の仕組みを理解していないと少し困るかもしれません。「AgentCore Runtimeがアクセスしたいリソースは、AgentCore IdentityではなくSecrets Manager」です。これはAgentCore IdentityでAPIキーなどを管理する場合、内部的にSecrets Managerを利用しており、保存されたAPIキーを取得するには、AgentCore Identityが作成したSecrets Managerへのアクセスが必要になるからです。

よって以下のようなポリシーをアタッチしてやりましょう。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetSecretValue"
            ],
            "Resource": "*"
        }
    ]
}

より厳密な管理を行う場合は、Resource*をAgentCore Identityが作成したSecretsのARNに差し替えましょう。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "secretsmanager:GetSecretValue"
            ],
            "Resource": [
                "arn:aws:secretsmanager:us-east-1:YOUR_ACCOUNT_ID:secret:YOUR_SECRET_NAME*"
            ]
        }
    ]
}

ツール・外部APIに接続しているかをチェックしよう

AI エージェントはAPI認証エラーが起きていても、自分の知識でなんとかタスクを完了させようとします。この辺りはOpenAIのブログでも説明されているハルシネーションの原因でもあります。

Think about it like a multiple-choice test. If you do not know the answer but take a wild guess, you might get lucky and be right. Leaving it blank guarantees a zero. In the same way, when models are graded only on accuracy, the percentage of questions they get exactly right, they are encouraged to guess rather than say “I don’t know.”

https://openai.com/index/why-language-models-hallucinate/

そのため、外部 API を連携させるエージェントを開発する際は、「本当にツールとAPIリクエストを実行しているか」を確認する仕組みと体制を整える必要がありそうです。例えば今回利用したStrands SDKにはロガーが用意されています。これを利用して開発中は詳細なログを取ることも一つでしょう。

https://strandsagents.com/latest/documentation/docs/user-guide/observability-evaluation/logs/

また AgentCore にも監視ツールが用意されていますのでこれもぜひ利用してみてください。

https://speakerdeck.com/licux/bedrock-agentcore-observabilityniyoruaiezientonoyun-yong

MCP とか AI を使ってる話を盛岡でやります

こんな話も含めた社内のAI利活用について、2025/9/13に東北IT物産展 in 岩手 2025というイベントで登壇予定です。

https://tohoku-it-bussanten.com/#a6

AIエージェントに入門するセッションなども用意されていますので、ぜひご参加ください。

デジタルキューブ

Discussion