Skip to content

Commit 0ba7adb

Browse files
committed
fix(bedrock-agentcore-alpha): use static construct ID for asset-based runtime artifacts
1 parent 5cb99a5 commit 0ba7adb

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,3 +1,4 @@
1+
import * as path from 'path';
12
import * as ecr from 'aws-cdk-lib/aws-ecr';
23
import * as cdk from 'aws-cdk-lib';
34
import { AgentRuntimeArtifact } from '../../../lib/runtime/runtime-artifact';
@@ -42,7 +43,7 @@ describe('AgentRuntimeArtifact tests', () => {
4243
test('Should use default options when not specified for asset', () => {
4344
// Call without specifying options to use default {}
4445
// Use the testArtifact directory that exists in tests
45-
const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact');
46+
const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact'));
4647

4748
const runtime = new Runtime(stack, 'test-runtime', {
4849
runtimeName: 'test_runtime',
@@ -58,7 +59,7 @@ describe('AgentRuntimeArtifact tests', () => {
5859
});
5960

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

6364
// Try to render without binding
6465
expect(() => {
@@ -92,7 +93,7 @@ describe('AgentRuntimeArtifact tests', () => {
9293
});
9394

9495
test('Should only bind once for asset image', () => {
95-
const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact', {
96+
const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact'), {
9697
buildArgs: {
9798
TEST: 'value',
9899
},
@@ -114,4 +115,61 @@ describe('AgentRuntimeArtifact tests', () => {
114115
// Should return the same URI
115116
expect(rendered1.containerUri).toBe(rendered2.containerUri);
116117
});
118+
119+
test('Should use static construct ID for asset image regardless of directory', () => {
120+
// Create two separate stacks to test that the construct ID is always 'AgentRuntimeArtifact'
121+
const stack1 = new cdk.Stack(app, 'test-stack-1', {
122+
env: { account: '123456789012', region: 'us-east-1' },
123+
});
124+
const stack2 = new cdk.Stack(app, 'test-stack-2', {
125+
env: { account: '123456789012', region: 'us-east-1' },
126+
});
127+
128+
const testArtifactPath = path.join(__dirname, 'testArtifact');
129+
const artifact1 = AgentRuntimeArtifact.fromAsset(testArtifactPath);
130+
const artifact2 = AgentRuntimeArtifact.fromAsset(testArtifactPath);
131+
132+
const runtime1 = new Runtime(stack1, 'test-runtime', {
133+
runtimeName: 'test_runtime_1',
134+
agentRuntimeArtifact: artifact1,
135+
});
136+
const runtime2 = new Runtime(stack2, 'test-runtime', {
137+
runtimeName: 'test_runtime_2',
138+
agentRuntimeArtifact: artifact2,
139+
});
140+
141+
artifact1.bind(stack1, runtime1);
142+
artifact2.bind(stack2, runtime2);
143+
144+
// Both should succeed - if the ID was dynamic based on directory hash,
145+
// different directories would produce different IDs, but now it's static
146+
const rendered1: any = artifact1._render();
147+
const rendered2: any = artifact2._render();
148+
149+
expect(rendered1.containerUri).toBeDefined();
150+
expect(rendered2.containerUri).toBeDefined();
151+
});
152+
153+
test('Should fail when binding different asset artifacts to same scope', () => {
154+
// This test verifies the static ID behavior - binding different artifacts
155+
// to the same scope should fail because the construct ID is static
156+
const testArtifactPath = path.join(__dirname, 'testArtifact');
157+
const artifact = AgentRuntimeArtifact.fromAsset(testArtifactPath);
158+
159+
const runtime = new Runtime(stack, 'test-runtime', {
160+
runtimeName: 'test_runtime',
161+
agentRuntimeArtifact: artifact,
162+
});
163+
164+
// First bind should succeed
165+
artifact.bind(stack, runtime);
166+
167+
// Create a new artifact (same directory, but different artifact instance)
168+
const artifact2 = AgentRuntimeArtifact.fromAsset(testArtifactPath);
169+
170+
// Second bind to the same scope should fail because the construct ID 'AgentRuntimeArtifact' already exists
171+
expect(() => {
172+
artifact2.bind(stack, runtime);
173+
}).toThrow(/There is already a Construct with name 'AgentRuntimeArtifact'/);
174+
});
117175
});

0 commit comments

Comments
 (0)