constructor()

in cdk/lib/facia-purger.ts [18:80]


	constructor(scope: App, id: string, props: GuStackProps) {
		super(scope, id, props);

		const fastlyServiceId = new CfnParameter(this, 'FastlyServiceId', {
			type: 'String',
			description: 'Id of service to purge',
		});
		const fastlyAPIKey = new CfnParameter(this, 'FastlyAPIKey', {
			type: 'String',
			description:
				'API key with purge writes for service with id FastlyServiceId',
			noEcho: true,
		});
		const emailRecipient = new CfnParameter(this, 'EmailRecipient', {
			type: 'String',
			description: 'The email address to send alarm notifications to',
		});

		const notificationTopic = new Topic(this, 'NotificationTopic');
		notificationTopic.addSubscription(
			new EmailSubscription(emailRecipient.valueAsString),
		);

		const lambda = new GuLambdaFunction(this, 'faciaPurgerLambda', {
			app: 'facia-purger',
			fileName: 'facia-purger.jar',
			runtime: Runtime.JAVA_11,
			handler: 'com.gu.purge.facia.Lambda',
			environment: {
				FastlyServiceId: fastlyServiceId.valueAsString,
				FastlyAPIKey: fastlyAPIKey.valueAsString,
				Stage: this.stage,
			},
			memorySize: 512,
			errorPercentageMonitoring: {
				toleratedErrorPercentage: 1,
				numberOfMinutesAboveThresholdBeforeAlarm: 5,
				snsTopicName: notificationTopic.topicName,
			},
		});

		this.overrideLogicalId(lambda, {
			logicalId: 'Lambda',
			reason: 'preserve existing triggers',
		});

		const invocationAlarm = new Alarm(this, 'invocationAlarm', {
			alarmDescription:
				'Notify if there are less than 5 invocations over last 10 minutes or there is insufficient data (i.e. no invocations)',
			comparisonOperator: ComparisonOperator.LESS_THAN_OR_EQUAL_TO_THRESHOLD,
			threshold: 5,
			evaluationPeriods: 1,
			actionsEnabled: true,
			treatMissingData: TreatMissingData.BREACHING,
			metric: lambda.metricInvocations({
				period: Duration.minutes(10),
				statistic: 'Sum',
				unit: Unit.COUNT,
			}),
		});

		invocationAlarm.addAlarmAction(new SnsAction(notificationTopic));
	}