-
Notifications
You must be signed in to change notification settings - Fork 0
/
me_container.ts
135 lines (119 loc) · 3.95 KB
/
me_container.ts
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright 2023-2024 Amazon.com, Inc. or its affiliates.
*/
import { RemovalPolicy, SymlinkFollowMode } from "aws-cdk-lib";
import { DockerImageAsset } from "aws-cdk-lib/aws-ecr-assets";
import { ContainerImage } from "aws-cdk-lib/aws-ecs";
import { Construct } from "constructs";
import { OSMLAccount } from "../osml_account";
import { OSMLECRDeployment } from "../osml_ecr_deployment";
import { OSMLVpc } from "../osml_vpc";
export class MEContainerConfig {
constructor(
public ME_DEFAULT_CONTAINER = "awsosml/osml-models:latest",
public ME_CONTAINER_BUILD_PATH = "lib/osml-models",
public ME_CONTAINER_BUILD_TARGET = "osml_model",
public ME_CONTAINER_REPOSITORY = "model-container"
) {}
}
/**
* Represents the properties required to configure an OSML (OversightML)
* model endpoint container..
*
* @interface MEContainerProps
*/
export interface MEContainerProps {
/**
* The OSML (OversightML) account associated with the model endpoints.
*/
account: OSMLAccount;
/**
* The OSML vpc to deploy into
*/
osmlVpc: OSMLVpc;
/**
* Optional flag to instruct building model container from source.
*/
buildFromSource?: boolean;
/**
* (Optional) Custom configuration for the MEContainer Construct
*/
config?: MEContainerConfig;
}
/**
* Represents a construct responsible for deploying an ECR container image
* for the model to be used with the model runner.
*/
export class MEContainer extends Construct {
/**
* The container image used for Construct.
*/
public containerImage: ContainerImage;
/**
* The URI of the container image.
*/
public containerUri: string;
/**
* The removal policy for the Construct resources.
*/
public removalPolicy: RemovalPolicy;
/**
* Configuration for the Construct.
*/
public config?: MEContainerConfig;
/**
* Creates an instance of OMSLEndpointContainer.
* @param {Construct} scope - The scope/stack in which to define this construct.
* @param {string} id - The id of this construct within the current scope.
* @param {MEContainerProps} props - The properties of this construct.
* @returns MEContainer - The OMSLEndpointContainer instance.
*/
constructor(scope: Construct, id: string, props: MEContainerProps) {
super(scope, id);
// Set the removal policy based on the account type
this.removalPolicy = props.account.prodLike
? RemovalPolicy.RETAIN
: RemovalPolicy.DESTROY;
// Check if a custom configuration was provided for the MR model container
if (props.config != undefined) {
// Import the existing MR configuration
this.config = props.config;
} else {
// Create a new default configuration
this.config = new MEContainerConfig();
}
// Create the container image and URI based on the account type
if (props.buildFromSource == true) {
// Dev account: Create a Docker image asset
const dockerImageAsset: DockerImageAsset = new DockerImageAsset(
this,
id,
{
directory: this.config.ME_CONTAINER_BUILD_PATH,
file: "Dockerfile",
followSymlinks: SymlinkFollowMode.ALWAYS,
target: this.config.ME_CONTAINER_BUILD_TARGET
}
);
this.containerImage =
ContainerImage.fromDockerImageAsset(dockerImageAsset);
this.containerUri = dockerImageAsset.imageUri;
} else {
// Non-dev account: Deploy to ECR using OSMLECRDeployment
const ecrDeployment: OSMLECRDeployment = new OSMLECRDeployment(
this,
"MEContainerECRDeployment",
{
account: props.account,
sourceUri: this.config.ME_DEFAULT_CONTAINER,
repositoryName: this.config.ME_CONTAINER_REPOSITORY,
removalPolicy: this.removalPolicy,
vpc: props.osmlVpc.vpc,
vpcSubnets: props.osmlVpc.selectedSubnets
}
);
this.containerImage = ecrDeployment.containerImage;
this.containerUri = ecrDeployment.ecrContainerUri;
}
}
}