forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo-project-stack.ts
More file actions
155 lines (132 loc) · 4.5 KB
/
Copy pathdemo-project-stack.ts
File metadata and controls
155 lines (132 loc) · 4.5 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
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
import * as cdk from "aws-cdk-lib";
import { Construct } from "constructs";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as secretsmanager from "aws-cdk-lib/aws-secretsmanager";
import * as logs from "aws-cdk-lib/aws-logs"; // Add this import
import * as ecr from "aws-cdk-lib/aws-ecr";
import { requireEnv } from "./utils";
export interface ProjectStackProps extends cdk.StackProps {
/**
* Path to the directory of the demo to deploy, relative to the root of the repository.
*/
projectName: string;
projectDescription: string;
/**
* Path to the Dockerfile to use, relative to the root of the repository. By default, this will be `${demoDir}/Dockerfile`.
*/
environmentVariables?: {
[key: string]: string;
};
environmentVariablesFromSecrets?: string[];
buildSecrets?: string[];
buildArgs?: Record<string, string>;
port: string;
timeout?: number;
memorySize?: number;
includeInPRComment?: boolean;
outputEnvVariable?: string;
overrideBuildProps?: Partial<cdk.aws_ecr_assets.DockerImageAssetProps>;
imageTag: string;
outputs?: Record<string, string>;
entrypoint?: string[];
cmd?: string[];
}
export class PreviewProjectStack extends cdk.Stack {
fnUrl: string;
constructor(scope: Construct, id: string, props: ProjectStackProps) {
const uniqueEnvironmentId = requireEnv("UNIQUE_ENV_ID");
const processedId = `${id}${uniqueEnvironmentId}`;
super(scope, processedId, props);
const secrets = secretsmanager.Secret.fromSecretNameV2(
this,
"ApiKeys",
"previews/api-keys"
);
// Create explicit log groups
const logGroup = new logs.LogGroup(this, "FunctionLogGroup", {
logGroupName: `/aws/lambda/previews/${processedId}-Fn`,
removalPolicy: cdk.RemovalPolicy.DESTROY,
retention: logs.RetentionDays.ONE_WEEK, // Adjust retention as needed
});
let environmentVariables: Record<string, string> = {};
let buildSecrets: Record<string, string> = {};
if (props.environmentVariables) {
environmentVariables = { ...props.environmentVariables };
}
if (props.environmentVariablesFromSecrets) {
for (const secret of props.environmentVariablesFromSecrets) {
environmentVariables[secret] = secrets
.secretValueFromJson(secret)
.unsafeUnwrap();
}
}
if(props.buildSecrets) {
for (const secret of props.buildSecrets) {
buildSecrets[secret] = `id=${secret}`;
}
}
const ecrRepository = ecr.Repository.fromRepositoryName(this, "ECRRepo", "coagents");
const fn = new lambda.Function(this, `Function`, {
logGroup: logGroup,
runtime: lambda.Runtime.FROM_IMAGE,
architecture: lambda.Architecture.X86_64,
handler: lambda.Handler.FROM_IMAGE,
environment: {
...environmentVariables,
PORT: props.port.toString(),
AWS_LWA_INVOKE_MODE: "RESPONSE_STREAM",
},
code: lambda.Code.fromEcrImage(ecrRepository, {
tagOrDigest: props.imageTag,
entrypoint: props.entrypoint,
cmd: props.cmd,
}),
timeout: cdk.Duration.seconds(props.timeout ?? 300),
memorySize: props.memorySize ?? 2048,
});
// Add Function URL with streaming support
const fnUrl = fn.addFunctionUrl({
authType: lambda.FunctionUrlAuthType.NONE,
cors: {
allowedOrigins: ["*"],
allowedMethods: [lambda.HttpMethod.ALL],
allowedHeaders: ["*"],
allowCredentials: true,
},
invokeMode: lambda.InvokeMode.RESPONSE_STREAM,
});
this.fnUrl = fnUrl.url;
// Output the Function URL
new cdk.CfnOutput(this, "FunctionUrl", {
value: fnUrl.url,
});
new cdk.CfnOutput(this, "IncludeInComment", {
value: `${props.includeInPRComment ?? false}`,
});
new cdk.CfnOutput(this, "StackId", {
value: this.stackId,
});
new cdk.CfnOutput(this, "StackName", {
value: this.stackName,
});
new cdk.CfnOutput(this, "ProjectName", {
value: props.projectName,
});
new cdk.CfnOutput(this, "ProjectDescription", {
value: props.projectDescription,
});
new cdk.CfnOutput(this, "UniqueEnvironmentId", {
value: `${uniqueEnvironmentId}`,
});
if (props.outputs) {
for (const [key, value] of Object.entries(props.outputs)) {
new cdk.CfnOutput(this, key, {
value: value,
});
}
}
// Add tag for PR number to all resources
cdk.Tags.of(this).add("env-id", uniqueEnvironmentId);
cdk.Tags.of(this).add("preview-env", "true");
}
}