diff --git a/packages/@aws-cdk/aws-bedrock-agentcore-alpha/lib/runtime/runtime-artifact.ts b/packages/@aws-cdk/aws-bedrock-agentcore-alpha/lib/runtime/runtime-artifact.ts index 5f3f5035831d5..699682fe21404 100644 --- a/packages/@aws-cdk/aws-bedrock-agentcore-alpha/lib/runtime/runtime-artifact.ts +++ b/packages/@aws-cdk/aws-bedrock-agentcore-alpha/lib/runtime/runtime-artifact.ts @@ -14,7 +14,6 @@ import * as ecr from 'aws-cdk-lib/aws-ecr'; import * as assets from 'aws-cdk-lib/aws-ecr-assets'; import { CfnRuntime } from 'aws-cdk-lib/aws-bedrockagentcore'; -import { md5hash } from 'aws-cdk-lib/core/lib/helpers-internal'; import { Construct } from 'constructs'; import { Runtime } from './runtime'; import { ValidationError } from './validation-helpers'; @@ -123,8 +122,7 @@ class AssetImage extends AgentRuntimeArtifact { public bind(scope: Construct, runtime: Runtime): void { // Create the asset if not already created if (!this.asset) { - const hash = md5hash(this.directory); - this.asset = new assets.DockerImageAsset(scope, `AgentRuntimeArtifact${hash}`, { + this.asset = new assets.DockerImageAsset(scope, 'AgentRuntimeArtifact', { directory: this.directory, ...this.options, }); diff --git a/packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/runtime-artifact.test.ts b/packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/runtime-artifact.test.ts index af2b287452852..35341fbe0019c 100644 --- a/packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/runtime-artifact.test.ts +++ b/packages/@aws-cdk/aws-bedrock-agentcore-alpha/test/agentcore/runtime/runtime-artifact.test.ts @@ -1,3 +1,4 @@ +import * as path from 'path'; import * as ecr from 'aws-cdk-lib/aws-ecr'; import * as cdk from 'aws-cdk-lib'; import { AgentRuntimeArtifact } from '../../../lib/runtime/runtime-artifact'; @@ -42,7 +43,7 @@ describe('AgentRuntimeArtifact tests', () => { test('Should use default options when not specified for asset', () => { // Call without specifying options to use default {} // Use the testArtifact directory that exists in tests - const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact'); + const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact')); const runtime = new Runtime(stack, 'test-runtime', { runtimeName: 'test_runtime', @@ -58,7 +59,7 @@ describe('AgentRuntimeArtifact tests', () => { }); test('Should throw error if _render is called before bind for AssetImage', () => { - const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact'); + const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact')); // Try to render without binding expect(() => { @@ -92,7 +93,7 @@ describe('AgentRuntimeArtifact tests', () => { }); test('Should only bind once for asset image', () => { - const artifact = AgentRuntimeArtifact.fromAsset('test/agentcore/runtime/testArtifact', { + const artifact = AgentRuntimeArtifact.fromAsset(path.join(__dirname, 'testArtifact'), { buildArgs: { TEST: 'value', }, @@ -114,4 +115,61 @@ describe('AgentRuntimeArtifact tests', () => { // Should return the same URI expect(rendered1.containerUri).toBe(rendered2.containerUri); }); + + test('Should use static construct ID for asset image regardless of directory', () => { + // Create two separate stacks to test that the construct ID is always 'AgentRuntimeArtifact' + const stack1 = new cdk.Stack(app, 'test-stack-1', { + env: { account: '123456789012', region: 'us-east-1' }, + }); + const stack2 = new cdk.Stack(app, 'test-stack-2', { + env: { account: '123456789012', region: 'us-east-1' }, + }); + + const testArtifactPath = path.join(__dirname, 'testArtifact'); + const artifact1 = AgentRuntimeArtifact.fromAsset(testArtifactPath); + const artifact2 = AgentRuntimeArtifact.fromAsset(testArtifactPath); + + const runtime1 = new Runtime(stack1, 'test-runtime', { + runtimeName: 'test_runtime_1', + agentRuntimeArtifact: artifact1, + }); + const runtime2 = new Runtime(stack2, 'test-runtime', { + runtimeName: 'test_runtime_2', + agentRuntimeArtifact: artifact2, + }); + + artifact1.bind(stack1, runtime1); + artifact2.bind(stack2, runtime2); + + // Both should succeed - if the ID was dynamic based on directory hash, + // different directories would produce different IDs, but now it's static + const rendered1: any = artifact1._render(); + const rendered2: any = artifact2._render(); + + expect(rendered1.containerUri).toBeDefined(); + expect(rendered2.containerUri).toBeDefined(); + }); + + test('Should fail when binding different asset artifacts to same scope', () => { + // This test verifies the static ID behavior - binding different artifacts + // to the same scope should fail because the construct ID is static + const testArtifactPath = path.join(__dirname, 'testArtifact'); + const artifact = AgentRuntimeArtifact.fromAsset(testArtifactPath); + + const runtime = new Runtime(stack, 'test-runtime', { + runtimeName: 'test_runtime', + agentRuntimeArtifact: artifact, + }); + + // First bind should succeed + artifact.bind(stack, runtime); + + // Create a new artifact (same directory, but different artifact instance) + const artifact2 = AgentRuntimeArtifact.fromAsset(testArtifactPath); + + // Second bind to the same scope should fail because the construct ID 'AgentRuntimeArtifact' already exists + expect(() => { + artifact2.bind(stack, runtime); + }).toThrow(/There is already a Construct with name 'AgentRuntimeArtifact'/); + }); });