|
1 | 1 | /* eslint-disable no-new */
|
2 | 2 | import type { aws_certificatemanager as acm, aws_ec2 as ec2, aws_efs as efs, aws_route53 as route53 } from 'aws-cdk-lib'
|
3 |
| -import { Duration, CfnOutput as Output, aws_lambda as lambda, aws_logs as logs, aws_secretsmanager as secretsmanager } from 'aws-cdk-lib' |
| 3 | +import { Duration, CfnOutput as Output, aws_ecs as ecs, aws_ecs_patterns as ecs_patterns, aws_lambda as lambda, aws_logs as logs, aws_secretsmanager as secretsmanager } from 'aws-cdk-lib' |
4 | 4 | import type { Construct } from 'constructs'
|
5 | 5 | import { path as p } from '@stacksjs/path'
|
6 | 6 | import { env } from '@stacksjs/env'
|
@@ -29,43 +29,78 @@ export class ComputeStack {
|
29 | 29 | // directory: p.cloudPath('src/server'),
|
30 | 30 | // })
|
31 | 31 |
|
32 |
| - this.apiServer = new lambda.Function(scope, 'WebServer', { |
33 |
| - functionName: `${props.slug}-${props.appEnv}-web-server`, |
34 |
| - description: 'The web server for the Stacks application', |
35 |
| - code: lambda.Code.fromAssetImage(p.frameworkPath('server')), |
36 |
| - handler: lambda.Handler.FROM_IMAGE, |
37 |
| - runtime: lambda.Runtime.FROM_IMAGE, |
| 32 | + const cluster = new ecs.Cluster(scope, 'StacksCluster', { |
38 | 33 | vpc,
|
39 |
| - memorySize: 512, // replace with your actual memory size |
40 |
| - timeout: Duration.minutes(5), // replace with your actual timeout |
41 |
| - logRetention: logs.RetentionDays.ONE_WEEK, |
42 |
| - architecture: lambda.Architecture.ARM_64, |
43 |
| - // filesystem: lambda.FileSystem.fromEfsAccessPoint(props.accessPoint, '/mnt/efs'), |
44 | 34 | })
|
45 | 35 |
|
46 |
| - const keysToRemove = ['_HANDLER', '_X_AMZN_TRACE_ID', 'AWS_REGION', 'AWS_EXECUTION_ENV', 'AWS_LAMBDA_FUNCTION_NAME', 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'AWS_LAMBDA_FUNCTION_VERSION', 'AWS_LAMBDA_INITIALIZATION_TYPE', 'AWS_LAMBDA_LOG_GROUP_NAME', 'AWS_LAMBDA_LOG_STREAM_NAME', 'AWS_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN', 'AWS_LAMBDA_RUNTIME_API', 'LAMBDA_TASK_ROOT', 'LAMBDA_RUNTIME_DIR', '_'] |
47 |
| - keysToRemove.forEach(key => delete env[key as EnvKey]) |
| 36 | + const taskDefinition = new ecs.FargateTaskDefinition(scope, 'TaskDef', { |
| 37 | + memoryLimitMiB: 512, // Match your Lambda memory size |
| 38 | + cpu: 256, // Choose an appropriate value |
| 39 | + }) |
48 | 40 |
|
49 |
| - const secrets = new secretsmanager.Secret(scope, 'StacksSecrets', { |
50 |
| - secretName: `${props.slug}-${props.appEnv}-secrets`, |
51 |
| - description: 'Secrets for the Stacks application', |
52 |
| - generateSecretString: { |
53 |
| - secretStringTemplate: JSON.stringify(env), |
54 |
| - generateStringKey: Object.keys(env).join(',').length.toString(), |
55 |
| - }, |
| 41 | + const container = taskDefinition.addContainer('WebServerContainer', { |
| 42 | + image: ecs.ContainerImage.fromAsset(p.frameworkPath('server')), |
| 43 | + // You can add environment variables, logging, etc., here |
56 | 44 | })
|
57 | 45 |
|
58 |
| - secrets.grantRead(this.apiServer) |
59 |
| - this.apiServer.addEnvironment('SECRETS_ARN', secrets.secretArn) |
| 46 | + const fargateService = new ecs_patterns.ApplicationLoadBalancedFargateService(scope, 'FargateService', { |
| 47 | + cluster, |
| 48 | + taskDefinition, |
| 49 | + desiredCount: 1, // Start with 1 task instance |
| 50 | + // Other configurations like public load balancer, domain name, etc. |
| 51 | + }) |
60 | 52 |
|
61 |
| - this.apiServerUrl = new lambda.FunctionUrl(scope, 'StacksServerUrl', { |
62 |
| - function: this.apiServer, |
63 |
| - authType: lambda.FunctionUrlAuthType.NONE, // becomes a public API |
64 |
| - cors: { |
65 |
| - allowedOrigins: ['*'], |
| 53 | + const volumeName = `${props.slug}-${props.appEnv}-efs` |
| 54 | + taskDefinition.addVolume({ |
| 55 | + name: volumeName, |
| 56 | + efsVolumeConfiguration: { |
| 57 | + fileSystemId: props.fileSystem.fileSystemId, |
66 | 58 | },
|
67 | 59 | })
|
68 | 60 |
|
| 61 | + container.addMountPoints({ |
| 62 | + sourceVolume: volumeName, |
| 63 | + containerPath: '/mnt/efs', |
| 64 | + readOnly: false, |
| 65 | + }) |
| 66 | + |
| 67 | + // this.apiServer = new lambda.Function(scope, 'WebServer', { |
| 68 | + // functionName: `${props.slug}-${props.appEnv}-web-server`, |
| 69 | + // description: 'The web server for the Stacks application', |
| 70 | + // code: lambda.Code.fromAssetImage(p.frameworkPath('server')), |
| 71 | + // handler: lambda.Handler.FROM_IMAGE, |
| 72 | + // runtime: lambda.Runtime.FROM_IMAGE, |
| 73 | + // vpc, |
| 74 | + // memorySize: 512, // replace with your actual memory size |
| 75 | + // timeout: Duration.minutes(5), // replace with your actual timeout |
| 76 | + // logRetention: logs.RetentionDays.ONE_WEEK, |
| 77 | + // architecture: lambda.Architecture.ARM_64, |
| 78 | + // // filesystem: lambda.FileSystem.fromEfsAccessPoint(props.accessPoint, '/mnt/efs'), |
| 79 | + // }) |
| 80 | + |
| 81 | + const keysToRemove = ['_HANDLER', '_X_AMZN_TRACE_ID', 'AWS_REGION', 'AWS_EXECUTION_ENV', 'AWS_LAMBDA_FUNCTION_NAME', 'AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'AWS_LAMBDA_FUNCTION_VERSION', 'AWS_LAMBDA_INITIALIZATION_TYPE', 'AWS_LAMBDA_LOG_GROUP_NAME', 'AWS_LAMBDA_LOG_STREAM_NAME', 'AWS_ACCESS_KEY', 'AWS_ACCESS_KEY_ID', 'AWS_SECRET_ACCESS_KEY', 'AWS_SESSION_TOKEN', 'AWS_LAMBDA_RUNTIME_API', 'LAMBDA_TASK_ROOT', 'LAMBDA_RUNTIME_DIR', '_'] |
| 82 | + keysToRemove.forEach(key => delete env[key as EnvKey]) |
| 83 | + |
| 84 | + // const secrets = new secretsmanager.Secret(scope, 'StacksSecrets', { |
| 85 | + // secretName: `${props.slug}-${props.appEnv}-secrets`, |
| 86 | + // description: 'Secrets for the Stacks application', |
| 87 | + // generateSecretString: { |
| 88 | + // secretStringTemplate: JSON.stringify(env), |
| 89 | + // generateStringKey: Object.keys(env).join(',').length.toString(), |
| 90 | + // }, |
| 91 | + // }) |
| 92 | + |
| 93 | + // secrets.grantRead(this.apiServer) |
| 94 | + // this.apiServer.addEnvironment('SECRETS_ARN', secrets.secretArn) |
| 95 | + |
| 96 | + // this.apiServerUrl = new lambda.FunctionUrl(scope, 'StacksServerUrl', { |
| 97 | + // function: this.apiServer, |
| 98 | + // authType: lambda.FunctionUrlAuthType.NONE, // becomes a public API |
| 99 | + // cors: { |
| 100 | + // allowedOrigins: ['*'], |
| 101 | + // }, |
| 102 | + // }) |
| 103 | + |
69 | 104 | const apiPrefix = 'api'
|
70 | 105 | new Output(scope, 'ApiUrl', {
|
71 | 106 | value: `https://${props.domain}/${apiPrefix}`,
|
|
0 commit comments