constructor()

in packages/cdk/lib/obligatron.ts [19:65]


	constructor(stack: GuStack, props: ObligatronProps) {
		const { vpc, dbAccess, db } = props;
		const app = 'obligatron';

		const lambda = new GuLambdaFunction(stack, 'obligatron', {
			app,
			vpc,
			architecture: Architecture.ARM_64,
			runtime: Runtime.NODEJS_20_X,
			securityGroups: [dbAccess],
			fileName: `${app}.zip`,
			handler: 'index.main',
			environment: {
				DATABASE_HOSTNAME: db.dbInstanceEndpointAddress,
				QUERY_LOGGING: 'false', // Set this to 'true' to enable SQL query logging
			},
			timeout: Duration.minutes(5),
			// Unfortunately Prisma doesn't support streaming data from Postgres at the moment https://github.com/prisma/prisma/issues/5055
			// This means that all rows need to be loaded into memory at the same time whenever a query is ran hence the high memory requirement.
			memorySize: 4096,
			errorPercentageMonitoring: {
				toleratedErrorPercentage: 0,
				snsTopicName: 'devx-alerts',
			},

			/*
			Override the default provided by GuCDK for improved compatability with https://github.com/guardian/cloudwatch-logs-management when producing log lines with markers.
			See also: https://github.com/guardian/cloudwatch-logs-management/issues/326.
			 */
			loggingFormat: LoggingFormat.TEXT,
		});

		Obligations.forEach((obligation, index) => {
			const startTime = (9 + index).toString();
			new Rule(stack, `obligatron-${obligation}`, {
				description: `Daily execution of Obligatron lambda for '${obligation}' obligation`,
				schedule: Schedule.cron({ minute: '0', hour: startTime }),
				targets: [
					new LambdaFunction(lambda, {
						event: RuleTargetInput.fromText(obligation),
					}),
				],
			});
		});

		db.grantConnect(lambda, 'obligatron');
	}