-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Expand file tree
/
Copy pathruntime-artifact.ts
More file actions
115 lines (100 loc) · 3.81 KB
/
runtime-artifact.ts
File metadata and controls
115 lines (100 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
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';
/**
* Abstract base class for agent runtime artifacts.
* Provides methods to reference container images from ECR repositories or local assets.
*/
export abstract class AgentRuntimeArtifact {
/**
* Reference an image in an ECR repository
*/
public static fromEcrRepository(repository: ecr.IRepository, tag: string = 'latest'): AgentRuntimeArtifact {
return new EcrImage(repository, tag);
}
/**
* Reference an agent runtime artifact that's constructed directly from sources on disk
* @param directory The directory where the Dockerfile is stored
* @param options The options to further configure the selected image
*/
public static fromAsset(directory: string, options: assets.DockerImageAssetOptions = {}): AgentRuntimeArtifact {
return new AssetImage(directory, options);
}
/**
* Called when the image is used by a Runtime to handle side effects like permissions
*/
public abstract bind(scope: Construct, runtime: Runtime): void;
/**
* Render the artifact configuration for CloudFormation
* @internal
*/
public abstract _render(): CfnRuntime.AgentRuntimeArtifactProperty;
}
class EcrImage extends AgentRuntimeArtifact {
private bound = false;
constructor(private readonly repository: ecr.IRepository, private readonly tag: string) {
super();
}
public bind(_scope: Construct, runtime: Runtime): void {
// Handle permissions (only once)
if (!this.bound && runtime.role) {
this.repository.grantPull(runtime.role);
this.bound = true;
}
}
public _render(): CfnRuntime.AgentRuntimeArtifactProperty {
// Return container configuration directly as expected by the runtime
// The runtime wraps this in containerConfiguration
return {
containerUri: this.repository.repositoryUriForTag(this.tag),
} as any;
}
}
class AssetImage extends AgentRuntimeArtifact {
private asset?: assets.DockerImageAsset;
private bound = false;
constructor(private readonly directory: string, private readonly options: assets.DockerImageAssetOptions = {}) {
super();
}
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}`, {
directory: this.directory,
...this.options,
});
}
// Grant permissions (only once)
if (!this.bound) {
this.asset.repository.grantPull(runtime.role);
this.bound = true;
}
}
public _render(): CfnRuntime.AgentRuntimeArtifactProperty {
if (!this.asset) {
throw new ValidationError('Asset not initialized. Call bind() before _render()');
}
// Return container configuration directly as expected by the runtime
// The runtime wraps this in containerConfiguration
return {
containerUri: this.asset.imageUri,
} as any;
}
}