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

Issue:
Add Target Group attribute setting
创建栈的时候，需要修改ELB的Target Group健康检查设置：
1. Healthy Threshold 需要小于等于Mincontainers
2. Unhealthy Threshold 需要小于等于Mincontainers


Discussion / hints:

Fix patch:
diff --git a/src/keycloak.ts b/src/keycloak.ts
index 0aabf90..c125aff 100644
--- a/src/keycloak.ts
+++ b/src/keycloak.ts
@@ -92,13 +92,13 @@ export interface AutoScaleTask {
    *
    * @default - min + 5
    */
-  readonly max ?: number;
+  readonly max?: number;
   /**
    * The target cpu utilization for the service autoscaling
    *
    * @default 75
    */
-  readonly targetCpuUtilization ?: number;
+  readonly targetCpuUtilization?: number;
 }
 
 export interface KeyCloakProps {
@@ -501,6 +501,7 @@ export class ContainerService extends cdk.Construct {
 
     const vpc = props.vpc;
     const cluster = new ecs.Cluster(this, 'Cluster', { vpc });
+    cluster.node.addDependency(props.database);
     const taskRole = new iam.Role(this, 'TaskRole', {
       assumedBy: new iam.CompositePrincipal(
         new iam.ServicePrincipal('ecs.amazonaws.com'),
@@ -508,8 +509,8 @@ export class ContainerService extends cdk.Construct {
       ),
     });
     const taskDefinition = new ecs.FargateTaskDefinition(this, 'TaskDef', {
-      cpu: 1024,
-      memoryLimitMiB: 2048,
+      cpu: 4096,
+      memoryLimitMiB: 8192,
       executionRole: taskRole,
     });
 
@@ -527,6 +528,7 @@ export class ContainerService extends cdk.Construct {
         DB_USER: 'admin',
         DB_VENDOR: 'mysql',
         // KEYCLOAK_LOGLEVEL: 'DEBUG',
+        PROXY_ADDRESS_FORWARDING: 'true',
         JDBC_PARAMS: 'useSSL=false',
         JGROUPS_DISCOVERY_PROTOCOL: 'JDBC_PING',
         // We don't need to specify `initialize_sql` string into `JGROUPS_DISCOVERY_PROPERTIES` property,
@@ -572,7 +574,7 @@ export class ContainerService extends cdk.Construct {
       const minCapacity = props.autoScaleTask.min ?? props.nodeCount ?? 2;
       const scaling = this.service.autoScaleTaskCount({
         minCapacity,
-        maxCapacity: props.autoScaleTask.max ?? minCapacity+5,
+        maxCapacity: props.autoScaleTask.max ?? minCapacity + 5,
       });
       scaling.scaleOnCpuUtilization('CpuScaling', {
         targetUtilizationPercent: props.autoScaleTask.targetCpuUtilization ?? 75,
@@ -593,6 +595,9 @@ export class ContainerService extends cdk.Construct {
 
     listener.addTargets('ECSTarget', {
       targets: [this.service],
+      healthCheck: {
+        healthyThresholdCount: 3,
+      },
       // set slow_start.duration_seconds to 60
       // see https://docs.aws.amazon.com/cli/latest/reference/elbv2/modify-target-group-attributes.html
       slowStart: cdk.Duration.seconds(60),
@@ -630,14 +635,14 @@ export class ContainerService extends cdk.Construct {
       return imageMap.findInMap(cdk.Aws.PARTITION, 'uri');
     } else {
       if (stack.region.startsWith('cn-')) {
-        return map['aws-cn']+=version;
+        return map['aws-cn'] += version;
       } else {
-        return map.aws+=version;
+        return map.aws += version;
       }
     }
   }
   private getKeyCloakDockerImageUri(version: string): string {
-    return this.getImageUriFromMap(KEYCLOAK_DOCKER_IMAGE_URI_MAP, version, 'KeycloakImageMap' );
+    return this.getImageUriFromMap(KEYCLOAK_DOCKER_IMAGE_URI_MAP, version, 'KeycloakImageMap');
   }
 }
 


Test patch:
diff --git a/test/__snapshots__/integ.snapshot.test.ts.snap b/test/__snapshots__/integ.snapshot.test.ts.snap
index e423161..9d979d2 100644
--- a/test/__snapshots__/integ.snapshot.test.ts.snap
+++ b/test/__snapshots__/integ.snapshot.test.ts.snap
@@ -271,6 +271,7 @@ Object {
     },
     "KeyCloakKeyCloakContainerSerivceALBHttpsListenerECSTargetGroupCE3EF52C": Object {
       "Properties": Object {
+        "HealthyThresholdCount": 3,
         "Port": 8443,
         "Protocol": "HTTPS",
         "TargetGroupAttributes": Array [
@@ -338,6 +339,15 @@ Object {
       "Type": "AWS::EC2::SecurityGroupEgress",
     },
     "KeyCloakKeyCloakContainerSerivceClusterA18E44FF": Object {
+      "DependsOn": Array [
+        "KeyCloakDatabaseAuroraServerlessClusterDB73D16F",
+        "KeyCloakDatabaseAuroraServerlessClusterSecretAttachmentA32F9C7B",
+        "keycloakdemoKeyCloakDatabaseAuroraServerlessClusterSecretBBC2EF3A3fdaad7efa858a3daf9490cf0a702aeb",
+        "KeyCloakDatabaseAuroraServerlessClusterSecurityGroupfromkeycloakdemoKeyCloakDatabaseAuroraServerlessClusterSecurityGroup1CF1B8093306AB9B4528",
+        "KeyCloakDatabaseAuroraServerlessClusterSecurityGroupfromkeycloakdemoKeyCloakKeyCloakContainerSerivceServiceSecurityGroup4DE99C4FIndirectPortF24A4E85",
+        "KeyCloakDatabaseAuroraServerlessClusterSecurityGroupF0CB4641",
+        "KeyCloakDatabaseAuroraServerlessClusterSubnets6166944B",
+      ],
       "Type": "AWS::ECS::Cluster",
     },
     "KeyCloakKeyCloakContainerSerivceLogGroup010F2AAE": Object {
@@ -611,6 +621,10 @@ Object {
                 "Name": "DB_VENDOR",
                 "Value": "mysql",
               },
+              Object {
+                "Name": "PROXY_ADDRESS_FORWARDING",
+                "Value": "true",
+              },
               Object {
                 "Name": "JDBC_PARAMS",
                 "Value": "useSSL=false",
@@ -701,7 +715,7 @@ Object {
             ],
           },
         ],
-        "Cpu": "1024",
+        "Cpu": "4096",
         "ExecutionRoleArn": Object {
           "Fn::GetAtt": Array [
             "KeyCloakKeyCloakContainerSerivceTaskRole0658CED2",
@@ -709,7 +723,7 @@ Object {
           ],
         },
         "Family": "keycloakdemoKeyCloakKeyCloakContainerSerivceTaskDef486F1059",
-        "Memory": "2048",
+        "Memory": "8192",
         "NetworkMode": "awsvpc",
         "RequiresCompatibilities": Array [
           "FARGATE",
diff --git a/test/cluster.test.ts b/test/cluster.test.ts
index 8f41c4d..7b548d7 100644
--- a/test/cluster.test.ts
+++ b/test/cluster.test.ts
@@ -316,6 +316,10 @@ test('with env', () => {
             Name: 'DB_VENDOR',
             Value: 'mysql',
           },
+          {
+            Name: 'PROXY_ADDRESS_FORWARDING',
+            Value: 'true',
+          },
           {
             Name: 'JDBC_PARAMS',
             Value: 'useSSL=false',
@@ -420,7 +424,7 @@ test('with env', () => {
         ],
       },
     ],
-    Cpu: '1024',
+    Cpu: '4096',
     ExecutionRoleArn: {
       'Fn::GetAtt': [
         'KeyCloakKeyCloakContainerSerivceTaskRole0658CED2',
@@ -428,7 +432,7 @@ test('with env', () => {
       ],
     },
     Family: 'testingstackKeyCloakKeyCloakContainerSerivceTaskDef799BAD5B',
-    Memory: '2048',
+    Memory: '8192',
     NetworkMode: 'awsvpc',
     RequiresCompatibilities: [
       'FARGATE',