Skip to content

Commit d0a4b5a

Browse files
committed
fix(bedrock-agentcore-alpha): use static construct ID for asset-based runtime artifacts
1 parent 3d6b204 commit d0a4b5a

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-6
lines changed

packages/@aws-cdk/aws-bedrock-agentcore-alpha/lib/runtime/runtime-artifact.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import * as ecr from 'aws-cdk-lib/aws-ecr';
1515
import * as assets from 'aws-cdk-lib/aws-ecr-assets';
1616
import { CfnRuntime } from 'aws-cdk-lib/aws-bedrockagentcore';
17-
import { md5hash } from 'aws-cdk-lib/core/lib/helpers-internal';
1817
import { Construct } from 'constructs';
1918
import { Runtime } from './runtime';
2019
import { ValidationError } from './validation-helpers';
@@ -123,8 +122,7 @@ class AssetImage extends AgentRuntimeArtifact {
123122
public bind(scope: Construct, runtime: Runtime): void {
124123
// Create the asset if not already created
125124
if (!this.asset) {
126-
const hash = md5hash(this.directory);
127-
this.asset = new assets.DockerImageAsset(scope, `AgentRuntimeArtifact${hash}`, {
125+
this.asset = new assets.DockerImageAsset(scope, 'AgentRuntimeArtifact', {
128126
directory: this.directory,
129127
...this.options,
130128
});

packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/runtime-artifact.test.ts

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import * as path from 'path';
12
import * as ecr from 'aws-cdk-lib/aws-ecr';
23
import * as cdk from 'aws-cdk-lib';
4+
import { Template } from 'aws-cdk-lib/assertions';
35
import { AgentRuntimeArtifact } from '../../../lib/runtime/runtime-artifact';
46
import { Runtime } from '../../../lib/runtime/runtime';
57

@@ -42,7 +44,7 @@ describe('AgentRuntimeArtifact tests', () => {
4244
test('Should use default options when not specified for asset', () => {
4345
// Call without specifying options to use default {}
4446
// Use the testArtifact directory that exists in tests
45-
const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact');
47+
const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact'));
4648

4749
const runtime = new Runtime(stack, 'test-runtime', {
4850
runtimeName: 'test_runtime',
@@ -58,7 +60,7 @@ describe('AgentRuntimeArtifact tests', () => {
5860
});
5961

6062
test('Should throw error if _render is called before bind for AssetImage', () => {
61-
const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact');
63+
const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact'));
6264

6365
// Try to render without binding
6466
expect(() => {
@@ -92,7 +94,7 @@ describe('AgentRuntimeArtifact tests', () => {
9294
});
9395

9496
test('Should only bind once for asset image', () => {
95-
const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact', {
97+
const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact'), {
9698
buildArgs: {
9799
TEST: 'value',
98100
},
@@ -114,4 +116,60 @@ describe('AgentRuntimeArtifact tests', () => {
114116
// Should return the same URI
115117
expect(rendered1.containerUri).toBe(rendered2.containerUri);
116118
});
119+
120+
test('Should use static construct ID for asset image regardless of directory', () => {
121+
// Create two separate stacks to test that the construct ID is always 'AgentRuntimeArtifact'
122+
const stack1 = new cdk.Stack(app, 'test-stack-1', {
123+
env: { account: '123456789012', region: 'us-east-1' },
124+
});
125+
const stack2 = new cdk.Stack(app, 'test-stack-2', {
126+
env: { account: '123456789012', region: 'us-east-1' },
127+
});
128+
129+
const testArtifactPath = path.join(__dirname, 'testArtifact');
130+
131+
const runtime1 = new Runtime(stack1, 'test-runtime', {
132+
runtimeName: 'test_runtime_1',
133+
agentRuntimeArtifact: AgentRuntimeArtifact.fromAsset(testArtifactPath),
134+
});
135+
const runtime2 = new Runtime(stack2, 'test-runtime', {
136+
runtimeName: 'test_runtime_2',
137+
agentRuntimeArtifact: AgentRuntimeArtifact.fromAsset(testArtifactPath),
138+
});
139+
140+
// Both stacks should synthesize successfully with the static construct ID
141+
const template1 = Template.fromStack(stack1);
142+
const template2 = Template.fromStack(stack2);
143+
144+
template1.hasResourceProperties('AWS::BedrockAgentCore::Runtime', {
145+
AgentRuntimeName: 'test_runtime_1',
146+
});
147+
template2.hasResourceProperties('AWS::BedrockAgentCore::Runtime', {
148+
AgentRuntimeName: 'test_runtime_2',
149+
});
150+
151+
// Verify both runtimes have the same static construct ID for the asset
152+
const assetNode1 = runtime1.node.findChild('AgentRuntimeArtifact');
153+
const assetNode2 = runtime2.node.findChild('AgentRuntimeArtifact');
154+
expect(assetNode1.node.id).toBe('AgentRuntimeArtifact');
155+
expect(assetNode1.node.id).toEqual(assetNode2.node.id);
156+
});
157+
158+
test('internal: should throw when binding different asset artifacts to same scope', () => {
159+
const testArtifactPath = path.join(__dirname, 'testArtifact');
160+
161+
const runtime = new Runtime(stack, 'test-runtime', {
162+
runtimeName: 'test_runtime',
163+
agentRuntimeArtifact: AgentRuntimeArtifact.fromAsset(testArtifactPath),
164+
});
165+
166+
// Synthesize to trigger lazy asset creation
167+
Template.fromStack(stack);
168+
169+
const artifact2 = AgentRuntimeArtifact.fromAsset(testArtifactPath);
170+
171+
expect(() => {
172+
artifact2.bind(runtime, runtime);
173+
}).toThrow(/There is already a Construct with name 'AgentRuntimeArtifact'/);
174+
});
117175
});

0 commit comments

Comments
 (0)