Repository: aws-samples/cdk-keycloak (language: python)

Issue:
Customizing FargateTaskDefinition - cpu and memory settings
It would be useful (at least for development environments) to customize the FargateTaskDefinition, especially CPU and memoryLimit. Currently this is fixed to `cpu: 4096` and `memoryLimitMiB: 8192`.

Is it already possible to customize this with CDK after definition with cdk-keycloak? Or must there be a code change in cdk-keycloak lib?

It seems that `keycloakContainerService` that contains the TaskDefiniton is not accessible through the object i've created with:
```
const keycloak : kc.KeyCloak = new kc.KeyCloak(this, 'KeyCloak', { .. });
```

At least I don't know how to reference it, to modify its properties.
fix: aws-cdk-lib 2.80.0 dependencies since pr #201 and all failing unit tests since pr #195
Fixes #
- Dependencies to aws-cdk-lib in version 2.80.0 fixed
- Unit tests related to updated aurora version fixed
- Unit tests related to aws-cdk-lib 2.80.0 fixed


Discussion / hints:

Fix patch:
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** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>)  VPC subnets for keycloak service. __*Optional*__
   * **publicSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>)  VPC public subnets for ALB. __*Optional*__
   * **stickinessCookieDuration** (<code>[Duration](#aws-cdk-lib-duration)</code>)  The sticky session duration for the keycloak workload with ALB. __*Default*__: one day
+  * **taskCpu** (<code>number</code>)  The number of cpu units used by the keycloak task. __*Default*__: 4096
+  * **taskMemory** (<code>number</code>)  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** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>)  VPC public subnets for ALB. __*Default*__: VPC public subnets
   * **singleDbInstance** (<code>boolean</code>)  Whether to use single RDS instance rather than RDS cluster. __*Default*__: false
   * **stickinessCookieDuration** (<code>[Duration](#aws-cdk-lib-duration)</code>)  The sticky session duration for the keycloak workload with ALB. __*Default*__: one day
+  * **taskCpu** (<code>number</code>)  The number of cpu units used by the keycloak task. __*Default*__: 4096
+  * **taskMemory** (<code>number</code>)  The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192
   * **vpc** (<code>[aws_ec2.IVpc](#aws-cdk-lib-aws-ec2-ivpc)</code>)  VPC for the workload. __*Optional*__
 
 
@@ -226,6 +230,8 @@ addKeyCloakContainerService(props: ContainerServiceProps): ContainerService
   * **privateSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>)  VPC subnets for keycloak service. __*Optional*__
   * **publicSubnets** (<code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code>)  VPC public subnets for ALB. __*Optional*__
   * **stickinessCookieDuration** (<code>[Duration](#aws-cdk-lib-duration)</code>)  The sticky session duration for the keycloak workload with ALB. __*Default*__: one day
+  * **taskCpu** (<code>number</code>)  The number of cpu units used by the keycloak task. __*Default*__: 4096
+  * **taskMemory** (<code>number</code>)  The amount (in MiB) of memory used by the keycloak task. __*Default*__: 8192
 
 __Returns__:
 * <code>[ContainerService](#cdk-keycloak-containerservice)</code>
@@ -314,6 +320,8 @@ Name | Type | Description
 **privateSubnets**? | <code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code> | VPC subnets for keycloak service.<br/>__*Optional*__
 **publicSubnets**? | <code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code> | VPC public subnets for ALB.<br/>__*Optional*__
 **stickinessCookieDuration**? | <code>[Duration](#aws-cdk-lib-duration)</code> | The sticky session duration for the keycloak workload with ALB.<br/>__*Default*__: one day
+**taskCpu**? | <code>number</code> | The number of cpu units used by the keycloak task.<br/>__*Default*__: 4096
+**taskMemory**? | <code>number</code> | The amount (in MiB) of memory used by the keycloak task.<br/>__*Default*__: 8192
 
 
 
@@ -389,6 +397,8 @@ Name | Type | Description
 **publicSubnets**? | <code>[aws_ec2.SubnetSelection](#aws-cdk-lib-aws-ec2-subnetselection)</code> | VPC public subnets for ALB.<br/>__*Default*__: VPC public subnets
 **singleDbInstance**? | <code>boolean</code> | Whether to use single RDS instance rather than RDS cluster.<br/>__*Default*__: false
 **stickinessCookieDuration**? | <code>[Duration](#aws-cdk-lib-duration)</code> | The sticky session duration for the keycloak workload with ALB.<br/>__*Default*__: one day
+**taskCpu**? | <code>number</code> | The number of cpu units used by the keycloak task.<br/>__*Default*__: 4096
+**taskMemory**? | <code>number</code> | The amount (in MiB) of memory used by the keycloak task.<br/>__*Default*__: 8192
 **vpc**? | <code>[aws_ec2.IVpc](#aws-cdk-lib-aws-ec2-ivpc)</code> | VPC for the workload.<br/>__*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,
     });
 


Test patch:
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',
+  });
+});