はじめに
Mastra v1(stable) リリースおめでとうございます.
本記事では, Mastra を AgentCore Runtime にデプロイし,
AgentCore Observability で AI エージェントを観測できるようにするまでの手順を,
AWS CDK によるインフラ構築も含めて, 簡潔に紹介します.
なお, トランザクション検索が有効であることを前提とします.
Mastra 側の実装
packages/ai/src/package.json
{
"name": "example",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "mastra dev",
"build": "mastra build"
},
"dependencies": {
"@ai-sdk/amazon-bedrock": "^4.0.22",
"@aws-sdk/credential-providers": "^3.971.0",
"@aws/aws-distro-opentelemetry-node-autoinstrumentation": "^0.8.1",
"@mastra/core": "^1.0.4",
"@mastra/otel-bridge": "^1.0.0",
"@opentelemetry/instrumentation": "^0.57.1",
"zod": "^3.25.76"
},
"devDependencies": {
"@types/node": "^24.10.9",
"mastra": "^1.0.1",
"typescript": "^5.9.3"
}
}
packages/ai/src/mastra/agents/example-agent.ts
import { Agent } from '@mastra/core/agent';
import { createAmazonBedrock } from '@ai-sdk/amazon-bedrock';
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
const bedrock = createAmazonBedrock({
region: 'us-east-1',
redentialProvider: fromNodeProviderChain(),
});
export const exampleAgent = new Agent({
id: 'example-agent',
name: 'example-agent',
instructions: '...',
model: bedrock('us.anthropic.claude-sonnet-4-5-20250929-v1:0'),
})
packages/ai/src/mastra/index.ts
import { Mastra } from '@mastra/core/mastra';
import { exampleAgent } from './agents/example-agent';
import { registerApiRoute } from '@mastra/core/server';
import { Observability } from '@mastra/observability';
import { OtelBridge } from '@mastra/otel-bridge';
export const mastra = new Mastra({
agents: {
exampleAgent
},
server: {
host: '0.0.0.0',
port: 8080,
apiRoutes: [
// Health check endpoint (REQUIRED by AgentCore)
registerApiRoute("/ping", {
method: "GET",
handler: async (c) => {
return c.json({
status: "Healthy",
time_of_last_update: Math.floor(Date.now() / 1000),
});
},
}),
// Agent invocation endpoint (REQUIRED by AgentCore)
registerApiRoute("/invocations", {
method: "POST",
handler: async (c) => {
const mastra = c.get("mastra");
const prompt = await c.req.text();
const agent = mastra.getAgent("exampleAgent");
const response = await agent.generate(prompt);
return c.text(response.text);
},
})
]
},
observability: new Observability({
configs: {
default: {
serviceName: "example",
bridge: new OtelBridge()
}
}
})
});
packages/ai/Dockerfile
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY src ./src
RUN npm run build
RUN addgroup -g 1001 -S nodejs && \
adduser -S mastra -u 1001 && \
chown -R mastra:nodejs /app
USER mastra
ENV PORT=8080
ENV NODE_ENV=production
EXPOSE 8080
CMD ["node", "--require", "@aws/aws-distro-opentelemetry-node-autoinstrumentation/register", ".mastra/output/index.mjs"]
AWS CDK 側の実装
packages/infrastructure/package.json
{
"name": "infrastructure",
"version": "0.1.0",
"private": true,
"bin": {
"infrastructure": "bin/infrastructure.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"@types/jest": "^30",
"@types/node": "^24.10.1",
"aws-cdk": "2.1101.0",
"jest": "^30",
"ts-jest": "^29",
"ts-node": "^10.9.2",
"typescript": "~5.9.3"
},
"dependencies": {
"@aws-cdk/aws-bedrock-agentcore-alpha": "^2.235.1-alpha.0",
"aws-cdk-lib": "^2.234.1",
"constructs": "^10.0.0"
}
}
packages/infrastructure/lib/infrastructure-stack.ts
import * as cdk from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import { AgentRuntimeArtifact, Runtime } from '@aws-cdk/aws-bedrock-agentcore-alpha';
import { aws_iam } from 'aws-cdk-lib';
export class InfrastructureStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const agentRuntimeArtifact = AgentRuntimeArtifact.fromAsset(
'../ai/'
);
const runtime = new Runtime(this, 'AgentRuntime', {
runtimeName: 'example-agent',
agentRuntimeArtifact,
});
runtime.addToRolePolicy(
new aws_iam.PolicyStatement({
effect: aws_iam.Effect.ALLOW,
actions: [
"bedrock:InvokeModel",
"bedrock:InvokeModelWithResponseStream",
],
resources: [
"arn:aws:bedrock:*::foundation-model/*",
`arn:aws:bedrock:${this.region}:${this.account}:inference-profile/*`,
],
}),
);
}
}
デプロイ
$ cd packages/infrastructure
$ cdk deploy ...
まとめ
取り急ぎ、Mastra を AgentCore Runtime にデプロイする最小構成をまとめました.
あわせて AgentCore Observability で, エージェントの実行状況を観測できるところまで確認しています.