-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathweb.ts
98 lines (85 loc) · 3.46 KB
/
web.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
import { CpuArchitecture, FargateTaskDefinition, ICluster } from 'aws-cdk-lib/aws-ecs';
import { Construct } from 'constructs';
import { Duration, aws_ecs as ecs } from 'aws-cdk-lib';
import { IAlb } from '../alb';
import { IRepository } from 'aws-cdk-lib/aws-ecr';
import { EnvironmentProps } from '../../environment-props';
import { getAdditionalEnvironmentVariables, getAdditionalSecretVariables } from './environment-variables';
export interface WebServiceProps {
cluster: ICluster;
alb: IAlb;
imageTag: string;
/**
* If true, enable debug outputs
* @default false
*/
debug?: boolean;
customRepository?: IRepository;
additionalEnvironmentVariables: EnvironmentProps['additionalEnvironmentVariables'];
}
export class WebService extends Construct {
constructor(scope: Construct, id: string, props: WebServiceProps) {
super(scope, id);
const { cluster, alb, debug = false, customRepository } = props;
const port = 3000;
const taskDefinition = new FargateTaskDefinition(this, 'Task', {
cpu: 256,
memoryLimitMiB: 512,
runtimePlatform: { cpuArchitecture: CpuArchitecture.X86_64 },
});
taskDefinition.addContainer('Main', {
image: customRepository
? ecs.ContainerImage.fromEcrRepository(customRepository, `dify-web_${props.imageTag}`)
: ecs.ContainerImage.fromRegistry(`langgenius/dify-web:${props.imageTag}`),
environment: {
// The log level for the application. Supported values are `DEBUG`, `INFO`, `WARNING`, `ERROR`, `CRITICAL`
LOG_LEVEL: debug ? 'DEBUG' : 'ERROR',
// enable DEBUG mode to output more logs
DEBUG: debug ? 'true' : 'false',
// The base URL of console application api server, refers to the Console base URL of WEB service if console domain is different from api or web app domain.
// example: http://cloud.dify.ai
CONSOLE_API_URL: alb.url,
// The URL prefix for Web APP frontend, refers to the Web App base URL of WEB service if web app domain is different from console or api domain.
// example: http://udify.app
APP_API_URL: alb.url,
// Setting host to 0.0.0.0 seems necessary for health check to pass.
// https://nextjs.org/docs/pages/api-reference/next-config-js/output
HOSTNAME: '0.0.0.0',
PORT: port.toString(),
...getAdditionalEnvironmentVariables(this, 'web', props.additionalEnvironmentVariables),
},
secrets: {
...getAdditionalSecretVariables(this, 'web', props.additionalEnvironmentVariables),
},
logging: ecs.LogDriver.awsLogs({
streamPrefix: 'log',
}),
portMappings: [{ containerPort: port }],
healthCheck: {
// use wget instead of curl due to alpine: https://stackoverflow.com/a/47722899/18550269
command: ['CMD-SHELL', `wget --no-verbose --tries=1 --spider http://localhost:${port}/ || exit 1`],
interval: Duration.seconds(15),
startPeriod: Duration.seconds(30),
timeout: Duration.seconds(5),
retries: 3,
},
});
const service = new ecs.FargateService(this, 'FargateService', {
cluster,
taskDefinition,
capacityProviderStrategies: [
{
capacityProvider: 'FARGATE',
weight: 0,
},
{
capacityProvider: 'FARGATE_SPOT',
weight: 1,
},
],
enableExecuteCommand: true,
minHealthyPercent: 100,
});
alb.addEcsService('Web', service, port, '/', ['/*']);
}
}