はじめに
従来、AI エージェントを環境にデプロイするには、コンテナ環境の構築、認証基盤の設定、ネットワーク設定など多くの作業が必要でした。AgentCore の L2 Construct を使用することで、これらの複雑な設定を数十行のコードで実現できます。
本記事では、最小限の設定から始めて段階的に機能を追加していく方法を解説します。
使用するリソース
- Amazon Bedrock AgentCore Runtime: エージェント実行環境
- Amazon Cognito User Pool: 認証基盤
- Amazon S3 Bucket: ストレージ(例)
AgentCore L2 Construct の魅力
AgentCore L2 Construct (@aws-cdk/aws-bedrock-agentcore-alpha) を使用すると、以下のような複雑な設定を自動化できます。
- IAM ロールとポリシーの自動作成
- ECR へのコンテナイメージの自動アップロード
- Cognito との統合設定
- ネットワーク設定とセキュリティグループ
- CloudWatch Logs の設定
これらを手動で設定すると数百行のコードが必要ですが、L2 Construct を使えば数十行で完結します。
2026/03 現在、このライブラリは alpha 版です。今後の変更により、記事に記載しているコードは動作しない可能性がありますのでご注意ください。
ステップ 1: 最小構成でデプロイ
まずは最小限の設定で AgentCore Runtime をデプロイしてみましょう。
ローカルアセットからのデプロイ
Dockerfile を含むローカルディレクトリを指定するだけで、CDK が自動的にコンテナイメージをビルドし、ECR にアップロードして Runtime を作成します。
import * as cdk from "aws-cdk-lib";
import * as path from "path";
import { Construct } from "constructs";
import * as agentcore from "@aws-cdk/aws-bedrock-agentcore-alpha";
export class AgentcoreStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// ローカルディレクトリから Runtime Artifact を作成
// agents/my_agent ディレクトリに Dockerfile が配置されている想定
const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromAsset(
path.join(__dirname, "../../agents/my_agent")
);
// Runtime を作成(これだけ!)
const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
runtimeName: "my_agent",
agentRuntimeArtifact: agentRuntimeArtifact,
description: "My first AgentCore Runtime",
});
}
}
これだけで、以下が自動的に実行されます。
- Dockerfile からコンテナイメージをビルド
- CDK 管理の ECR リポジトリにイメージをプッシュ
- 実行用の IAM ロールを作成
- CloudWatch Logs の設定
- Runtime の作成と設定
ステップ 2: Cognito 認証を追加
次に、Cognito User Pool を使用した認証を追加します。L2 Construct を使えば、Cognito との統合も authorizerConfiguration を指定することで可能です。
import * as cognito from "aws-cdk-lib/aws-cognito";
// Cognito User Pool を作成
const userPool = new cognito.UserPool(this, "UserPool", {
userPoolName: "my-agent-pool",
selfSignUpEnabled: false,
signInAliases: {
email: true,
username: false,
}
});
const userPoolClient = new cognito.UserPoolClient(
this,
"UserPoolClient",
{
userPool: userPool,
userPoolClientName: "my-agent-client",
authFlows: {
userPassword: true,
userSrp: true,
},
}
);
// Cognito 認証を使用する Runtime を作成
const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
runtimeName: "my_agent",
agentRuntimeArtifact: agentRuntimeArtifact,
description: "Agent runtime with Cognito auth",
authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingCognito(
userPool,
[userPoolClient]
),
});
ステップ 3: カスタムヘッダーと環境変数を設定
Runtime にカスタムヘッダーや環境変数を渡す設定を追加します。
const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
runtimeName: "my_agent",
agentRuntimeArtifact: agentRuntimeArtifact,
description: "Agent runtime with custom configuration",
authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingCognito(
userPool,
[userPoolClient]
),
// カスタムヘッダーの設定
requestHeaderConfiguration: {
allowlistedHeaders: [
"Authorization",
"X-Amzn-Bedrock-AgentCore-Runtime-Custom-IdToken"
]
},
// 環境変数の設定
environmentVariables: {
USER_POOL_ID: userPool.userPoolId,
USER_CLIENT_ID: userPoolClient.userPoolClientId,
},
});
これで、Runtime 内のアプリケーションコードから環境変数やカスタムヘッダーも受け取れるようになります。
おわりに
AgentCore L2 Construct を使用することで、認証・権限管理・環境変数設定などを含む AgentCore 環境を短時間で構築できました。
なお、本記事では基本的なデプロイ方法に焦点を当てましたが、AgentCore L2 Construct には VPC 設定やバージョニングとエンドポイントの指定なども行えます。
セキュリティや運用優秀性の観点からも必要になる機能かと思うので、また触れてみたいと思います。