Skip to content

Commit

Permalink
OPHYK-214 Create health check alarm for varda-rekisterointi
Browse files Browse the repository at this point in the history
  • Loading branch information
rce committed Dec 27, 2024
1 parent 3ca773c commit b2c7fc8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 33 deletions.
7 changes: 6 additions & 1 deletion infra/src/cdk-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,18 @@ class CdkApp extends cdk.App {
},
};

const config = getConfig();

const { hostedZone } = new DnsStack(this, "DnsStack", stackProps);
const { alarmTopic, alarmsToSlackLambda } = new AlarmStack(this, "AlarmStack", stackProps);
const { vpc, bastion } = new VpcStack(this, "VpcStack", stackProps);
const ecsStack = new ECSStack(this, "ECSStack", vpc, stackProps);
const vardaRekisterointiDatabaseStack = new VardRekisterointiDatabaseStack(this, "VardaRekisterointiDatabase", vpc, ecsStack.cluster, bastion, alarmTopic, stackProps);
const organisaatioDatabaseStack = new OrganisaatioDatabaseStack(this, "Database", vpc, ecsStack.cluster, bastion, alarmTopic, stackProps);
createHealthCheckStacks(this, alarmsToSlackLambda)
createHealthCheckStacks(this, alarmsToSlackLambda, [
{ name: "Organisaatio", url: new URL(`https://${config.virkailijaHost}/organisaatio-service/actuator/health`) },
{ name: "VardaRekisterointi", url: new URL(`https://${config.virkailijaHost}/varda-rekisterointi/actuator/health`) },
])
new VardaRekisterointiApplicationStack(this, "VardaRekisterointiApplication", vpc, hostedZone, {
database: vardaRekisterointiDatabaseStack.database,
ecsCluster: ecsStack.cluster,
Expand Down
75 changes: 43 additions & 32 deletions infra/src/health-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@ import * as cdk from "aws-cdk-lib";
import * as sns from "aws-cdk-lib/aws-sns";
import * as constructs from "constructs";
import * as route53 from "aws-cdk-lib/aws-route53";
import * as config from "./config";
import * as cloudwatch from "aws-cdk-lib/aws-cloudwatch";
import * as cloudwatch_actions from "aws-cdk-lib/aws-cloudwatch-actions";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as sns_subscriptions from "aws-cdk-lib/aws-sns-subscriptions";
import * as url from "node:url";

export const ROUTE53_HEALTH_CHECK_REGION = "us-east-1"

export function createHealthCheckStacks(app: cdk.App, alarmsToSlackLambda: lambda.IFunction) {
const healthCheckStack = new GlobalHealthCheckStack(app, "GlobalHealthCheckStack", {
export type HealthCheck = {
name: string
url: url.URL
}

export function createHealthCheckStacks(
app: cdk.App,
alarmsToSlackLambda: lambda.IFunction,
healthChecks: HealthCheck[],
) {
const healthCheckStack = new GlobalHealthCheckStack(app, "GlobalHealthCheckStack", healthChecks, {
env: {
account: process.env.CDK_DEPLOY_TARGET_ACCOUNT,
region: ROUTE53_HEALTH_CHECK_REGION,
Expand All @@ -29,42 +38,44 @@ export function createHealthCheckStacks(app: cdk.App, alarmsToSlackLambda: lambd

class GlobalHealthCheckStack extends cdk.Stack {
readonly globalAlarmTopic: sns.ITopic
constructor(scope: constructs.Construct, id: string, props: cdk.StackProps) {
constructor(scope: constructs.Construct, id: string, healthChecks: HealthCheck[], props: cdk.StackProps) {
super(scope, id, props);

this.globalAlarmTopic = new sns.Topic(this, "AlarmTopic", {
topicName: "GlobalAlarm",
});

const check = new route53.CfnHealthCheck(this, "VirkailijaDomainHealthCheck", {
healthCheckConfig: {
type: "HTTPS",
fullyQualifiedDomainName: config.getConfig().virkailijaHost,
port: 443,
resourcePath: "/organisaatio-service/actuator/health",
},
healthCheckTags: [{
key: "Name",
value: "OrganisaatioHealthCheck",
}],
});
for (const healthCheck of healthChecks) {
const check = new route53.CfnHealthCheck(this, `${healthCheck.name}HealthCheck`, {
healthCheckConfig: {
type: "HTTPS",
fullyQualifiedDomainName: healthCheck.url.hostname,
port: 443,
resourcePath: healthCheck.url.pathname,
},
healthCheckTags: [{
key: "Name",
value: `${healthCheck.name}HealthCheck`,
}],
});

const metric = new cloudwatch.Metric({
namespace: "AWS/Route53",
metricName: "HealthCheckStatus",
dimensionsMap: {
HealthCheckId: check.attrHealthCheckId,
},
})
const alarm = metric.createAlarm(this, "OrganisaatioHealthCheckAlarm", {
alarmName: "OrganisaatioHealthCheckAlarm",
threshold: 1,
evaluationPeriods: 1,
treatMissingData: cloudwatch.TreatMissingData.BREACHING,
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD,
})
alarm.addOkAction(new cloudwatch_actions.SnsAction(this.globalAlarmTopic));
alarm.addAlarmAction(new cloudwatch_actions.SnsAction(this.globalAlarmTopic));
const metric = new cloudwatch.Metric({
namespace: "AWS/Route53",
metricName: "HealthCheckStatus",
dimensionsMap: {
HealthCheckId: check.attrHealthCheckId,
},
})
const alarm = metric.createAlarm(this, `${healthCheck.name}HealthCheckAlarm`, {
alarmName: `${healthCheck.name}HealthCheckAlarm`,
threshold: 1,
evaluationPeriods: 1,
treatMissingData: cloudwatch.TreatMissingData.BREACHING,
comparisonOperator: cloudwatch.ComparisonOperator.LESS_THAN_THRESHOLD,
})
alarm.addOkAction(new cloudwatch_actions.SnsAction(this.globalAlarmTopic));
alarm.addAlarmAction(new cloudwatch_actions.SnsAction(this.globalAlarmTopic));
}
}
}

Expand Down

0 comments on commit b2c7fc8

Please sign in to comment.