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

Issue:
fix(keycloak): use defaultDatabaseName props remove bootstrap task
fix(keycloak): use defaultDatabaseName props remove bootstrap task


Discussion / hints:

Fix patch:
diff --git a/src/keycloak.ts b/src/keycloak.ts
index dbdefa5..948e9bd 100644
--- a/src/keycloak.ts
+++ b/src/keycloak.ts
@@ -41,11 +41,6 @@ const KEYCLOAK_DOCKER_IMAGE_URI_MAP: dockerImageMap = {
   'aws-cn': `048912060910.dkr.ecr.cn-northwest-1.amazonaws.com.cn/dockerhub/jboss/keycloak:${KEYCLOAK_VERSION}`,
 };
 
-const BOOTSTRAP_DOCKER_IMAGE_URI_MAP: dockerImageMap = {
-  'aws': 'public.ecr.aws/ubuntu/mysql:latest',
-  'aws-cn': '048912060910.dkr.ecr.cn-northwest-1.amazonaws.com.cn/dockerhub/mysql:latest',
-};
-
 /**
  * The ECS task autoscaling definition
  */
@@ -325,6 +320,7 @@ export class Database extends cdk.Construct {
   private _createRdsInstance(props: DatabaseProps): DatabaseCofig {
     const dbInstance = new rds.DatabaseInstance(this, 'DBInstance', {
       vpc: props.vpc,
+      databaseName: 'keycloak',
       vpcSubnets: props.databaseSubnets,
       engine: props.instanceEngine ?? rds.DatabaseInstanceEngine.mysql({
         version: rds.MysqlEngineVersion.VER_8_0_21,
@@ -349,6 +345,7 @@ export class Database extends cdk.Construct {
       engine: props.clusterEngine ?? rds.DatabaseClusterEngine.auroraMysql({
         version: rds.AuroraMysqlEngineVersion.VER_2_09_1,
       }),
+      defaultDatabaseName: 'keycloak',
       credentials: rds.Credentials.fromGeneratedSecret('admin'),
       instanceProps: {
         vpc: props.vpc,
@@ -373,6 +370,7 @@ export class Database extends cdk.Construct {
     const dbCluster = new rds.ServerlessCluster(this, 'AuroraServerlessCluster', {
       engine: rds.DatabaseClusterEngine.AURORA_MYSQL,
       vpc: props.vpc,
+      defaultDatabaseName: 'keycloak',
       vpcSubnets: props.databaseSubnets,
       credentials: rds.Credentials.fromGeneratedSecret('admin'),
       backupRetention: props.backupRetention ?? cdk.Duration.days(7),
@@ -472,27 +470,6 @@ export class ContainerService extends cdk.Construct {
       removalPolicy: cdk.RemovalPolicy.DESTROY,
     });
 
-    // bootstrap container that creates the database if not exist
-    const bootstrap = taskDefinition.addContainer('bootstrap', {
-      essential: false,
-      image: ecs.ContainerImage.fromRegistry(this.getBootstrapDockerImageUri()),
-      environment: {
-        DB_NAME: 'keycloak',
-        DB_USER: 'admin',
-        DB_ADDR: props.database.clusterEndpointHostname,
-      },
-      secrets: {
-        DB_PASSWORD: ecs.Secret.fromSecretsManager(props.database.secret, 'password'),
-      },
-      command: [
-        'sh', '-c',
-        'mysql -u$DB_USER -p$DB_PASSWORD -h$DB_ADDR -e "CREATE DATABASE IF NOT EXISTS $DB_NAME"',
-      ],
-      logging: ecs.LogDrivers.awsLogs({
-        streamPrefix: 'bootstrap',
-        logGroup,
-      }),
-    });
     const kc = taskDefinition.addContainer('keycloak', {
       image: ecs.ContainerImage.fromRegistry(this.getKeyCloakDockerImageUri()),
       environment: Object.assign({
@@ -526,10 +503,6 @@ export class ContainerService extends cdk.Construct {
       { containerPort: 55200, protocol: ecs.Protocol.UDP }, // jgroups-udp
       { containerPort: 54200, protocol: ecs.Protocol.UDP }, // jgroups-udp-fd
     );
-    kc.addContainerDependencies({
-      container: bootstrap,
-      condition: ecs.ContainerDependencyCondition.SUCCESS,
-    });
 
     // we need extra privileges to fetch keycloak docker images from China mirror site
     taskDefinition.executionRole?.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonEC2ContainerRegistryReadOnly'));
@@ -617,9 +590,6 @@ export class ContainerService extends cdk.Construct {
   private getKeyCloakDockerImageUri(): string {
     return this.getImageUriFromMap(KEYCLOAK_DOCKER_IMAGE_URI_MAP, 'KeycloakImageMap' );
   }
-  private getBootstrapDockerImageUri(): string {
-    return this.getImageUriFromMap(BOOTSTRAP_DOCKER_IMAGE_URI_MAP, 'BootstrapImageMap');
-  }
 }
 
 /**


Test patch:
diff --git a/test/__snapshots__/integ.snapshot.test.ts.snap b/test/__snapshots__/integ.snapshot.test.ts.snap
index 91f5570..ae984b7 100644
--- a/test/__snapshots__/integ.snapshot.test.ts.snap
+++ b/test/__snapshots__/integ.snapshot.test.ts.snap
@@ -48,6 +48,7 @@ Object {
         "DBSubnetGroupName": Object {
           "Ref": "KeyCloakDatabaseAuroraServerlessClusterSubnets6166944B",
         },
+        "DatabaseName": "keycloak",
         "Engine": "aurora-mysql",
         "EngineMode": "serverless",
         "MasterUserPassword": Object {
@@ -581,67 +582,6 @@ Object {
       "Properties": Object {
         "ContainerDefinitions": Array [
           Object {
-            "Command": Array [
-              "sh",
-              "-c",
-              "mysql -u$DB_USER -p$DB_PASSWORD -h$DB_ADDR -e \\"CREATE DATABASE IF NOT EXISTS $DB_NAME\\"",
-            ],
-            "Environment": Array [
-              Object {
-                "Name": "DB_NAME",
-                "Value": "keycloak",
-              },
-              Object {
-                "Name": "DB_USER",
-                "Value": "admin",
-              },
-              Object {
-                "Name": "DB_ADDR",
-                "Value": Object {
-                  "Fn::GetAtt": Array [
-                    "KeyCloakDatabaseAuroraServerlessClusterDB73D16F",
-                    "Endpoint.Address",
-                  ],
-                },
-              },
-            ],
-            "Essential": false,
-            "Image": "public.ecr.aws/ubuntu/mysql:latest",
-            "LogConfiguration": Object {
-              "LogDriver": "awslogs",
-              "Options": Object {
-                "awslogs-group": Object {
-                  "Ref": "KeyCloakKeyCloakContainerSerivceLogGroup010F2AAE",
-                },
-                "awslogs-region": "us-east-1",
-                "awslogs-stream-prefix": "bootstrap",
-              },
-            },
-            "Name": "bootstrap",
-            "Secrets": Array [
-              Object {
-                "Name": "DB_PASSWORD",
-                "ValueFrom": Object {
-                  "Fn::Join": Array [
-                    "",
-                    Array [
-                      Object {
-                        "Ref": "KeyCloakDatabaseAuroraServerlessClusterSecretAttachmentA32F9C7B",
-                      },
-                      ":password::",
-                    ],
-                  ],
-                },
-              },
-            ],
-          },
-          Object {
-            "DependsOn": Array [
-              Object {
-                "Condition": "SUCCESS",
-                "ContainerName": "bootstrap",
-              },
-            ],
             "Environment": Array [
               Object {
                 "Name": "DB_ADDR",
diff --git a/test/cluster.test.ts b/test/cluster.test.ts
index 1afa490..5b5c353 100644
--- a/test/cluster.test.ts
+++ b/test/cluster.test.ts
@@ -285,77 +285,6 @@ test('with env', () => {
 
     ContainerDefinitions: [
       {
-        Command: [
-          'sh',
-          '-c',
-          'mysql -u$DB_USER -p$DB_PASSWORD -h$DB_ADDR -e "CREATE DATABASE IF NOT EXISTS $DB_NAME"',
-        ],
-        Environment: [
-          {
-            Name: 'DB_NAME',
-            Value: 'keycloak',
-          },
-          {
-            Name: 'DB_USER',
-            Value: 'admin',
-          },
-          {
-            Name: 'DB_ADDR',
-            Value: {
-              'Fn::GetAtt': [
-                'KeyCloakDatabaseDBCluster06E9C0E1',
-                'Endpoint.Address',
-              ],
-            },
-          },
-        ],
-        Essential: false,
-        Image: {
-          'Fn::FindInMap': [
-            'KeyCloakKeyCloakContainerSerivceBootstrapImageMap1F3D33FC',
-            {
-              Ref: 'AWS::Partition',
-            },
-            'uri',
-          ],
-        },
-        LogConfiguration: {
-          LogDriver: 'awslogs',
-          Options: {
-            'awslogs-group': {
-              Ref: 'KeyCloakKeyCloakContainerSerivceLogGroup010F2AAE',
-            },
-            'awslogs-stream-prefix': 'bootstrap',
-            'awslogs-region': {
-              Ref: 'AWS::Region',
-            },
-          },
-        },
-        Name: 'bootstrap',
-        Secrets: [
-          {
-            Name: 'DB_PASSWORD',
-            ValueFrom: {
-              'Fn::Join': [
-                '',
-                [
-                  {
-                    Ref: 'KeyCloakDatabaseDBClusterSecretAttachment50401C92',
-                  },
-                  ':password::',
-                ],
-              ],
-            },
-          },
-        ],
-      },
-      {
-        DependsOn: [
-          {
-            Condition: 'SUCCESS',
-            ContainerName: 'bootstrap',
-          },
-        ],
         Environment: [
           {
             Name: 'DB_ADDR',