-
Notifications
You must be signed in to change notification settings - Fork 0
/
litellm-cdk-stack.ts
418 lines (376 loc) · 15.2 KB
/
litellm-cdk-stack.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import * as cdk from 'aws-cdk-lib';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecs from 'aws-cdk-lib/aws-ecs';
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
import * as rds from 'aws-cdk-lib/aws-rds';
import * as secretsmanager from 'aws-cdk-lib/aws-secretsmanager';
import { Construct } from 'constructs';
import * as elasticloadbalancingv2 from 'aws-cdk-lib/aws-elasticloadbalancingv2';
import * as certificatemanager from 'aws-cdk-lib/aws-certificatemanager';
import * as route53 from 'aws-cdk-lib/aws-route53';
import * as targets from 'aws-cdk-lib/aws-route53-targets';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as ecr from "aws-cdk-lib/aws-ecr";
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as path from 'path';
import * as cr from 'aws-cdk-lib/custom-resources';
import * as elasticache from 'aws-cdk-lib/aws-elasticache';
interface LiteLLMStackProps extends cdk.StackProps {
domainName: string;
certificateArn: string;
liteLLMVersion: string;
architecture: string;
ecrLitellmRepository: string;
ecrMiddlewareRepository: string;
logBucketArn: string;
openaiApiKey: string;
azureOpenAiApiKey: string;
azureApiKey: string;
anthropicApiKey: string;
groqApiKey: string;
cohereApiKey: string;
coApiKey: string;
hfToken: string;
huggingfaceApiKey: string;
databricksApiKey: string;
geminiApiKey: string;
codestralApiKey: string;
mistralApiKey: string;
azureAiApiKey: string;
nvidiaNimApiKey: string;
xaiApiKey: string;
perplexityaiApiKey: string;
githubApiKey: string;
deepseekApiKey: string;
}
export class LitellmCdkStack extends cdk.Stack {
constructor(scope: Construct, id: string, props: LiteLLMStackProps) {
super(scope, id, props);
const configBucket = new s3.Bucket(this, 'LiteLLMConfigBucket', {
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
encryption: s3.BucketEncryption.S3_MANAGED,
});
new s3deploy.BucketDeployment(this, 'DeployConfig', {
sources: [s3deploy.Source.asset(path.join(__dirname, '../', "../", "config"))],
destinationBucket: configBucket,
include: ['config.yaml'], // Only include config.yaml
exclude: ['*'],
});
// Create VPC
const vpc = new ec2.Vpc(this, 'LiteLLMVpc', {
maxAzs: 2,
natGateways: 1,
});
// Create ECS Cluster
const cluster = new ecs.Cluster(this, 'LiteLLMCluster', {
vpc,
containerInsights: true,
});
// Create RDS Instance
const databaseSecret = new secretsmanager.Secret(this, 'DBSecret', {
generateSecretString: {
secretStringTemplate: JSON.stringify({
username: 'llmproxy',
}),
generateStringKey: 'password',
excludePunctuation: true,
},
});
const dbSecurityGroup = new ec2.SecurityGroup(this, 'DBSecurityGroup', {
vpc,
description: 'Security group for RDS instance',
allowAllOutbound: true,
});
const database = new rds.DatabaseInstance(this, 'Database', {
engine: rds.DatabaseInstanceEngine.postgres({
version: rds.PostgresEngineVersion.VER_15,
}),
instanceType: ec2.InstanceType.of(ec2.InstanceClass.T3, ec2.InstanceSize.MICRO),
vpc,
vpcSubnets: {
subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS,
},
securityGroups: [dbSecurityGroup],
credentials: rds.Credentials.fromSecret(databaseSecret),
databaseName: 'litellm',
});
const redisSecurityGroup = new ec2.SecurityGroup(this, 'RedisSecurityGroup', {
vpc,
description: 'Security group for Redis cluster',
allowAllOutbound: true,
});
// Create Redis Subnet Group
const redisSubnetGroup = new elasticache.CfnSubnetGroup(this, 'RedisSubnetGroup', {
description: 'Subnet group for Redis cluster',
subnetIds: vpc.privateSubnets.map(subnet => subnet.subnetId),
cacheSubnetGroupName: 'litellm-redis-subnet-group',
});
const redisParameterGroup = new elasticache.CfnParameterGroup(this, 'RedisParameterGroup', {
cacheParameterGroupFamily: 'redis7',
description: 'Redis parameter group',
});
// Create Redis Cluster
const redis = new elasticache.CfnReplicationGroup(this, 'RedisCluster', {
replicationGroupDescription: 'Redis cluster',
engine: 'redis',
cacheNodeType: 'cache.t3.micro',
numCacheClusters: 2,
automaticFailoverEnabled: true,
cacheParameterGroupName: redisParameterGroup.ref,
cacheSubnetGroupName: redisSubnetGroup.ref,
securityGroupIds: [redisSecurityGroup.securityGroupId],
engineVersion: '7.0',
port: 6379,
});
// Make sure the subnet group is created before the cluster
redis.addDependency(redisSubnetGroup);
redis.addDependency(redisParameterGroup);
// Create LiteLLM Secret
const litellmMasterAndSaltKeySecret = new secretsmanager.Secret(this, 'LiteLLMSecret', {
generateSecretString: {
secretStringTemplate: JSON.stringify({
LITELLM_MASTER_KEY: 'placeholder',
LITELLM_SALT_KEY: 'placeholder',
}),
generateStringKey: 'dummy',
},
});
const litellmOtherSecrets = new secretsmanager.Secret(this, 'LiteLLMApiKeySecret', {
generateSecretString: {
secretStringTemplate: JSON.stringify({
OPENAI_API_KEY: props.openaiApiKey,
AZURE_OPENAI_API_KEY: props.azureOpenAiApiKey,
AZURE_API_KEY: props.azureApiKey,
ANTHROPIC_API_KEY: props.anthropicApiKey,
GROQ_API_KEY: props.groqApiKey,
COHERE_API_KEY: props.cohereApiKey,
CO_API_KEY: props.coApiKey,
HF_TOKEN: props.hfToken,
HUGGINGFACE_API_KEY: props.huggingfaceApiKey,
DATABRICKS_API_KEY: props.databricksApiKey,
GEMINI_API_KEY: props.geminiApiKey,
CODESTRAL_API_KEY: props.codestralApiKey,
MISTRAL_API_KEY: props.mistralApiKey,
AZURE_AI_API_KEY: props.azureAiApiKey,
NVIDIA_NIM_API_KEY: props.nvidiaNimApiKey,
XAI_API_KEY: props.xaiApiKey,
PERPLEXITYAI_API_KEY: props.perplexityaiApiKey,
GITHUB_API_KEY: props.githubApiKey,
DEEPSEEK_API_KEY: props.deepseekApiKey,
}),
generateStringKey: 'dummy',
},
});
const generateSecretKeys = new cr.AwsCustomResource(this, 'GenerateSecretKeys', {
onCreate: {
service: 'SecretsManager',
action: 'putSecretValue',
parameters: {
SecretId: litellmMasterAndSaltKeySecret.secretArn,
SecretString: JSON.stringify({
LITELLM_MASTER_KEY: 'sk-' + Math.random().toString(36).substring(2),
LITELLM_SALT_KEY: 'sk-' + Math.random().toString(36).substring(2),
}),
},
physicalResourceId: cr.PhysicalResourceId.of('SecretInitializer'),
},
policy: cr.AwsCustomResourcePolicy.fromSdkCalls({
resources: [litellmMasterAndSaltKeySecret.secretArn],
}),
});
litellmMasterAndSaltKeySecret.grantWrite(generateSecretKeys);
// Create Task Definition
const taskDefinition = new ecs.FargateTaskDefinition(this, 'LiteLLMTaskDef', {
memoryLimitMiB: 1024,
cpu: 512,
runtimePlatform: {
cpuArchitecture: props.architecture == "x86" ? ecs.CpuArchitecture.X86_64 : ecs.CpuArchitecture.ARM64,
operatingSystemFamily: ecs.OperatingSystemFamily.LINUX
},
});
taskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject', 's3:ListBucket'],
resources: [configBucket.bucketArn, `${configBucket.bucketArn}/*`],
}));
taskDefinition.addToTaskRolePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
's3:*',
],
resources: [props.logBucketArn, `${props.logBucketArn}/*`],
}));
taskDefinition.taskRole.addToPrincipalPolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'bedrock:*', // Full access to Bedrock
],
resources: ['*']
}));
taskDefinition.taskRole.addToPrincipalPolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: [
'sagemaker:InvokeEndpoint',
],
resources: ['*']
}));
// Create a custom secret for the database URL
const dbUrlSecret = new secretsmanager.Secret(this, 'DBUrlSecret', {
secretStringValue: cdk.SecretValue.unsafePlainText(
`postgresql://llmproxy:${databaseSecret.secretValueFromJson('password').unsafeUnwrap()}@${database.instanceEndpoint.hostname}:5432/litellm`
),
});
const ecrLitellmRepository = ecr.Repository.fromRepositoryName(
this,
props.ecrLitellmRepository!,
props.ecrLitellmRepository!
);
// Add container to task definition
const container = taskDefinition.addContainer('LiteLLMContainer', {
image: ecs.ContainerImage.fromEcrRepository(ecrLitellmRepository, props.liteLLMVersion),
logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'LiteLLM' }),
secrets: {
DATABASE_URL: ecs.Secret.fromSecretsManager(dbUrlSecret),
LITELLM_MASTER_KEY: ecs.Secret.fromSecretsManager(litellmMasterAndSaltKeySecret, 'LITELLM_MASTER_KEY'),
UI_PASSWORD: ecs.Secret.fromSecretsManager(litellmMasterAndSaltKeySecret, 'LITELLM_MASTER_KEY'),
LITELLM_SALT_KEY: ecs.Secret.fromSecretsManager(litellmMasterAndSaltKeySecret, 'LITELLM_SALT_KEY'),
OPENAI_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'OPENAI_API_KEY'),
AZURE_OPENAI_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'AZURE_OPENAI_API_KEY'),
AZURE_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'AZURE_API_KEY'),
ANTHROPIC_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'ANTHROPIC_API_KEY'),
GROQ_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'GROQ_API_KEY'),
COHERE_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'COHERE_API_KEY'),
CO_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'CO_API_KEY'),
HF_TOKEN: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'HF_TOKEN'),
HUGGINGFACE_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'HUGGINGFACE_API_KEY'),
DATABRICKS_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'DATABRICKS_API_KEY'),
GEMINI_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'GEMINI_API_KEY'),
CODESTRAL_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'CODESTRAL_API_KEY'),
MISTRAL_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'MISTRAL_API_KEY'),
AZURE_AI_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'AZURE_AI_API_KEY'),
NVIDIA_NIM_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'NVIDIA_NIM_API_KEY'),
XAI_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'XAI_API_KEY'),
PERPLEXITYAI_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'PERPLEXITYAI_API_KEY'),
GITHUB_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'GITHUB_API_KEY'),
DEEPSEEK_API_KEY: ecs.Secret.fromSecretsManager(litellmOtherSecrets, 'DEEPSEEK_API_KEY'),
},
environment: {
LITELLM_CONFIG_BUCKET_NAME: configBucket.bucketName,
LITELLM_CONFIG_BUCKET_OBJECT_KEY: 'config.yaml',
UI_USERNAME: "admin",
REDIS_URL: `redis://${redis.attrPrimaryEndPointAddress}:${redis.attrPrimaryEndPointPort}`
}
});
const ecrMiddlewareRepository = ecr.Repository.fromRepositoryName(
this,
props.ecrMiddlewareRepository!,
props.ecrMiddlewareRepository!
);
const middlewareContainer = taskDefinition.addContainer('MiddlewareContainer', {
image: ecs.ContainerImage.fromEcrRepository(ecrMiddlewareRepository, "latest"),
logging: ecs.LogDrivers.awsLogs({ streamPrefix: 'Middleware' }),
});
const domainParts = props.domainName.split(".");
const domainName = domainParts.slice(1).join(".");
const hostName = domainParts[0];
// Retrieve the existing Route 53 hosted zone
const hostedZone = route53.HostedZone.fromLookup(this, 'Zone', {
domainName: `${domainName}.`
});
const certificate = certificatemanager.Certificate.fromCertificateArn(this, 'Certificate',
props.certificateArn
);
const fargateService = new ecs_patterns.ApplicationMultipleTargetGroupsFargateService(this, 'LiteLLMService', {
cluster,
taskDefinition,
serviceName: "LiteLLMService",
loadBalancers: [
{
name: 'ALB',
publicLoadBalancer: true,
domainName: `${domainName}.`,
domainZone: hostedZone,
listeners: [
{
name: 'Listener',
protocol: elasticloadbalancingv2.ApplicationProtocol.HTTPS,
certificate: certificate,
sslPolicy: elasticloadbalancingv2.SslPolicy.RECOMMENDED,
},
],
},
],
targetGroups: [
{
containerPort: 4000,
listener: 'Listener',
},
{
containerPort: 3000,
listener: 'Listener',
priority: 5,
pathPattern: '/bedrock*',
},
],
desiredCount: 1,
healthCheckGracePeriod: cdk.Duration.seconds(300),
});
redisSecurityGroup.addIngressRule(
fargateService.service.connections.securityGroups[0],
ec2.Port.tcp(6379),
'Allow ECS tasks to connect to Redis'
);
const targetGroupLlmGateway = fargateService.targetGroups[0];
targetGroupLlmGateway.configureHealthCheck({
path: '/health/liveliness',
port: '4000',
protocol: elasticloadbalancingv2.Protocol.HTTP,
healthyThresholdCount: 2,
unhealthyThresholdCount: 3,
timeout: cdk.Duration.seconds(10),
interval: cdk.Duration.seconds(30),
});
const targetGroupMiddleware = fargateService.targetGroups[1];
targetGroupMiddleware.configureHealthCheck({
path: '/bedrock/health/liveliness',
port: '3000',
protocol: elasticloadbalancingv2.Protocol.HTTP,
healthyThresholdCount: 2,
unhealthyThresholdCount: 3,
timeout: cdk.Duration.seconds(10),
interval: cdk.Duration.seconds(30),
});
new route53.ARecord(this, 'DNSRecord', {
zone: hostedZone,
target: route53.RecordTarget.fromAlias(
new targets.LoadBalancerTarget(fargateService.loadBalancer)
),
recordName: props.domainName, // This will be the full domain name
});
dbSecurityGroup.addIngressRule(
fargateService.service.connections.securityGroups[0],
ec2.Port.tcp(5432),
'Allow ECS tasks to connect to RDS'
);
const scaling = fargateService.service.autoScaleTaskCount({
maxCapacity: 4,
minCapacity: 1,
});
scaling.scaleOnCpuUtilization('CpuScaling', {
targetUtilizationPercent: 70,
});
new cdk.CfnOutput(this, 'ServiceURL', {
value: `https://${props.domainName}`,
});
new cdk.CfnOutput(this, 'LitellmEcsCluster', {
value: cluster.clusterName,
description: 'Name of the ECS Cluster'
});
new cdk.CfnOutput(this, 'LitellmEcsTask', {
value: fargateService.service.serviceName,
description: 'Name of the task service'
});
}
}