Slide 36
Slide 36 text
AIエージェント時代にAWS CDKを使う理由
// infra/cdk/test/agent-runtime-stack.test.ts
describe('AgentRuntimeStack', () => {
test.each(variants)('snapshot: $name', (variant) => {
const app = new cdk.App();
const stack = new AgentRuntimeStack(app, 'AgentRuntimeStack', {
stage: variant.stage,
env: { region: 'ap-northeast-1' },
gatewayUrl: 'https://gateway.example.com/mcp',
/* ...
省略 */
});
expect(snapshotTemplate(stack)).toMatchSnapshot();
});
});
// variants = staging / production
を test.each
で展開
//
デプロイせず CloudFormation
差分を検知する
// infra/cdk/lib/stacks/AgentRuntimeStack.ts
this.runtime = new agentcore.Runtime(this, 'Runtime', {
agentRuntimeArtifact:
agentcore.AgentRuntimeArtifact.fromImageUri(imageUri),
protocolConfiguration: agentcore.ProtocolType.HTTP,
networkConfiguration:
agentcore.RuntimeNetworkConfiguration.usingPublicNetwork(),
executionRole: this.executionRole,
environmentVariables: { GATEWAY_URL: gatewayUrl, /* ... */ },
});
//
リソース間の関係を1
行で表現できる
// IAM Policy
は CDK
が自動生成
agentRuntimeRepository.grantPull(this.executionRole);
// TS199
行 /
出力CFn1381
行、約7
倍の差
Testable かつ Readable
AIエージェントが書くのなら直接CloudFormationを書かせればいいじゃないですか何故AWS CDKを使う必要があるのさ (watany)
コーディングエージェントがコードを書く昨今、
大事なのはエージェントへのフィードバックサイクルと、
人間の読みやすさ
AWS CDK を使えば、
TypeScript の恩恵による型チェックや Linter によるフィードバックに加えて、
オフラインテストも書きやすい
(スナップショットテスト、
アサーションテストなど)
AWS CDK は最終的に CloudFormation テンプレート(yaml / json)に変換されてデプロイされるが、
その yaml / json を直接書こうとする
と行数が多すぎてレビューが困難
AWS CDK を使えば抽象化されていて行数を抑えられるのと、
リソースの関係性を表す記法が充実しているのでレビューがしやすい