After setting up an RDS instance and an EKS cluster using AWS CDK and Typescript, I need to find a way to trigger a Lambda function to set up database users for microservices on EKS. The Lambda function should run after the RDS instance is created but before the microservices are installed.
To achieve this, I have created a Custom Resource backed by a Lambda function in Typescript. This Lambda function retrieves the secrets generated by CDK for the RDS instance and the database users, connects to the database, and creates the necessary users.
Although I have not been able to explicitly declare the dependency, as my Lambda function does not technically require one (other than using secret.grantRead(lambda)
), simply running a retry loop until everything is up will not be effective due to the long creation times. Any code snippets or examples would be greatly appreciated, as most references so far have only discussed the general approach.
In relation to @jogold's response:
const database = new rds.DatabaseInstance(this, 'db', {
instanceIdentifier: conf.systemName,
engine: rds.DatabaseInstanceEngine.POSTGRES,
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MICRO),
masterUsername: MASTER_USER,
allocatedStorage: 20, maxAllocatedStorage: 100,
multiAz: conf.isProduction,
storageEncrypted: true,
backupRetention: cdk.Duration.days(7),
deletionProtection: false,
vpc, securityGroups: [ rdsSG ]
});
CDK automatically generates a secret with credentials to access the database. I attempted to attach this secret directly to my Lambda:
new cdk.CustomResource(this, 'preinstall', {
serviceToken: preinstallProvider.serviceToken,
properties: { secret: database.secret }
});
However, this resulted in the following error:
Error: Resolution error: Resolution error: Trying to resolve() a Construct at /Resources/${Token[td000.preinstall.Default.LogicalID.221]}/Properties/dbSecret/node/_actualNode.
Additionally, it appears that the ISecret interface does not contain the ID/name of the secret, leaving me unsure of how to proceed in the Lambda function.