From d74d326ad79b73f0ab2a66b625ae84908ab090df Mon Sep 17 00:00:00 2001 From: Alexander Widera Date: Thu, 13 Jul 2023 17:44:14 +0200 Subject: [PATCH] feat: Customizable task cpu and memory settings --- API.md | 10 +++++++++ README.md | 15 +++++++++++++ src/keycloak.ts | 41 ++++++++++++++++++++++++++++++++---- test/cluster-quarkus.test.ts | 28 ++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) diff --git a/API.md b/API.md index 70c3756..7ee9127 100644 --- a/API.md +++ b/API.md @@ -57,6 +57,8 @@ new ContainerService(scope: Construct, id: string, props: ContainerServiceProps) * **privateSubnets** ([aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)) VPC subnets for keycloak service. __*Optional*__ * **publicSubnets** ([aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)) VPC public subnets for ALB. __*Optional*__ * **stickinessCookieDuration** ([Duration](#aws-cdk-lib-duration)) The sticky session duration for the keycloak workload with ALB. __*Default*__: one day + * **taskCpu** (number) The number of cpu units used by the keycloak task. __*Default*__: 4096 + * **taskMemory** (number) The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192 @@ -159,6 +161,8 @@ new KeyCloak(scope: Construct, id: string, props: KeyCloakProps) * **publicSubnets** ([aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)) VPC public subnets for ALB. __*Default*__: VPC public subnets * **singleDbInstance** (boolean) Whether to use single RDS instance rather than RDS cluster. __*Default*__: false * **stickinessCookieDuration** ([Duration](#aws-cdk-lib-duration)) The sticky session duration for the keycloak workload with ALB. __*Default*__: one day + * **taskCpu** (number) The number of cpu units used by the keycloak task. __*Default*__: 4096 + * **taskMemory** (number) The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192 * **vpc** ([aws_ec2.IVpc](#aws-cdk-lib-aws-ec2-ivpc)) VPC for the workload. __*Optional*__ @@ -226,6 +230,8 @@ addKeyCloakContainerService(props: ContainerServiceProps): ContainerService * **privateSubnets** ([aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)) VPC subnets for keycloak service. __*Optional*__ * **publicSubnets** ([aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)) VPC public subnets for ALB. __*Optional*__ * **stickinessCookieDuration** ([Duration](#aws-cdk-lib-duration)) The sticky session duration for the keycloak workload with ALB. __*Default*__: one day + * **taskCpu** (number) The number of cpu units used by the keycloak task. __*Default*__: 4096 + * **taskMemory** (number) The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192 __Returns__: * [ContainerService](#cdk-keycloak-containerservice) @@ -314,6 +320,8 @@ Name | Type | Description **privateSubnets**? | [aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection) | VPC subnets for keycloak service.
__*Optional*__ **publicSubnets**? | [aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection) | VPC public subnets for ALB.
__*Optional*__ **stickinessCookieDuration**? | [Duration](#aws-cdk-lib-duration) | The sticky session duration for the keycloak workload with ALB.
__*Default*__: one day +**taskCpu**? | number | The number of cpu units used by the keycloak task.
__*Default*__: 4096 +**taskMemory**? | number | The amount (in MiB) of memory used by the keycloak task.
__*Default*__: 8192 @@ -389,6 +397,8 @@ Name | Type | Description **publicSubnets**? | [aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection) | VPC public subnets for ALB.
__*Default*__: VPC public subnets **singleDbInstance**? | boolean | Whether to use single RDS instance rather than RDS cluster.
__*Default*__: false **stickinessCookieDuration**? | [Duration](#aws-cdk-lib-duration) | The sticky session duration for the keycloak workload with ALB.
__*Default*__: one day +**taskCpu**? | number | The number of cpu units used by the keycloak task.
__*Default*__: 4096 +**taskMemory**? | number | The amount (in MiB) of memory used by the keycloak task.
__*Default*__: 8192 **vpc**? | [aws_ec2.IVpc](#aws-cdk-lib-aws-ec2-ivpc) | VPC for the workload.
__*Optional*__ diff --git a/README.md b/README.md index 1a8cf53..74119ca 100644 --- a/README.md +++ b/README.md @@ -100,6 +100,21 @@ new KeyCloak(stack, 'KeyCloak', { ``` + +# Customize fargate task settings + +Define `taskCpu` or `taskMemory` for overriding the defaults for the ecs service task. +Could be useful for development environments. For example: + +```ts +new KeyCloak(stack, 'KeyCloak', { + nodeCount: 1, + taskCpu: 512, + taskMemory: 2048, +}); + +``` + # Deploy in existing Vpc Subnets You can deploy the workload in the existing Vpc and subnets. The `publicSubnets` are for the ALB, `privateSubnets` for the keycloak container tasks and `databaseSubnets` for the database. diff --git a/src/keycloak.ts b/src/keycloak.ts index 2672cfe..a77e999 100644 --- a/src/keycloak.ts +++ b/src/keycloak.ts @@ -272,13 +272,29 @@ export interface KeyCloakProps { */ readonly databaseRemovalPolicy?: cdk.RemovalPolicy; - /** * Overrides the default image * * @default quay.io/keycloak/keycloak:${KEYCLOAK_VERSION} */ readonly containerImage?: ecs.ContainerImage; + + /** + * The number of cpu units used by the keycloak task. + * + * @default 4096 + * @see FargateTaskDefinitionProps + */ + readonly taskCpu?: number; + + /** + * The amount (in MiB) of memory used by the keycloak task. + * + * @default 8192 + * @see FargateTaskDefinitionProps + */ + readonly taskMemory?: number; + } export class KeyCloak extends Construct { @@ -329,6 +345,8 @@ export class KeyCloak extends Construct { internetFacing: props.internetFacing ?? true, hostname: props.hostname, containerImage: props.containerImage, + taskCpu: props.taskCpu, + taskMemory: props.taskMemory, }); this.applicationLoadBalancer = keycloakContainerService.applicationLoadBalancer; @@ -671,13 +689,28 @@ export interface ContainerServiceProps { */ readonly hostname?: string; - /** * Overrides the default image * * @default quay.io/keycloak/keycloak:${KEYCLOAK_VERSION} */ readonly containerImage?: ecs.ContainerImage; + + /** + * The number of cpu units used by the keycloak task. + * + * @default 4096 + * @see FargateTaskDefinitionProps + */ + readonly taskCpu?: number; + + /** + * The amount (in MiB) of memory used by the keycloak task. + * + * @default 8192 + * @see FargateTaskDefinitionProps + */ + readonly taskMemory?: number; } export class ContainerService extends Construct { @@ -764,8 +797,8 @@ export class ContainerService extends Construct { ), }); const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', { - cpu: 4096, - memoryLimitMiB: 8192, + cpu: props.taskCpu ?? 4096, + memoryLimitMiB: props.taskMemory ?? 8192, executionRole, }); diff --git a/test/cluster-quarkus.test.ts b/test/cluster-quarkus.test.ts index 9b09a0e..fefd0e4 100644 --- a/test/cluster-quarkus.test.ts +++ b/test/cluster-quarkus.test.ts @@ -529,3 +529,31 @@ test('with env', () => { }, }); }); + + +test('with customized task settings', () => { + // GIVEN + const app = new App(); + const stack = new Stack(app, 'testing-stack'); + + // WHEN + new kc.KeyCloak(stack, 'KeyCloak', { + keycloakVersion: KeycloakVersion.V21_0_1, + certificateArn: 'MOCK_ARN', + hostname: 'keycloak.test', + taskCpu: 512, + taskMemory: 1024, + }); + + // THEN + const t = assertions.Template.fromStack(stack); + t.hasResourceProperties('AWS::ECS::TaskDefinition', { + ContainerDefinitions: [ + { + Name: 'keycloak', + }, + ], + Cpu: '512', + Memory: '1024', + }); +});