Skip to content

Commit

Permalink
Lisätty DBT:lle valvontaa
Browse files Browse the repository at this point in the history
  • Loading branch information
augustk committed Oct 17, 2024
1 parent 89fd0ac commit 3b2cd21
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
1 change: 1 addition & 0 deletions cdk/bin/cdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const ecsStack = new EcsStack(app, `${config.environment}-EcsStack`, {
auroraSecurityGroup: databaseStack.auroraSecurityGroup,
githubActionsDeploymentRole: externalRolesStack.githubActionsDeploymentRole,
ecsImageTag: ecsImageTag,
slackAlarmIntegrationSnsTopic: monitorStack.slackAlarmIntegrationSnsTopic,
vpc: networkStack.vpc,
...props,
});
Expand Down
45 changes: 45 additions & 0 deletions cdk/lib/ecs-stack.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import * as cdk from 'aws-cdk-lib';
import * as appscaling from 'aws-cdk-lib/aws-applicationautoscaling';
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';
import * as cloudwatchActions from 'aws-cdk-lib/aws-cloudwatch-actions';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as ecr from 'aws-cdk-lib/aws-ecr';
import * as ecs from 'aws-cdk-lib/aws-ecs';
Expand All @@ -8,6 +10,7 @@ import { CfnRule } from 'aws-cdk-lib/aws-events';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Effect } from 'aws-cdk-lib/aws-iam';
import * as logs from 'aws-cdk-lib/aws-logs';
import * as sns from 'aws-cdk-lib/aws-sns';
import * as ssm from 'aws-cdk-lib/aws-ssm';
import * as cdkNag from 'cdk-nag';
import { Construct } from 'constructs';
Expand All @@ -18,6 +21,7 @@ export interface EcsStackProps extends GenericStackProps {
auroraSecurityGroup: ec2.ISecurityGroup;
ecsImageTag: string;
githubActionsDeploymentRole: iam.IRole;
slackAlarmIntegrationSnsTopic: sns.ITopic;
vpc: ec2.IVpc;
}

Expand All @@ -27,6 +31,12 @@ export class EcsStack extends cdk.Stack {

const config: Config = props.config;

const addActionsToAlarm = (alarm: cloudwatch.Alarm) => {
alarm.addAlarmAction(
new cloudwatchActions.SnsAction(props.slackAlarmIntegrationSnsTopic)
);
};

const ecsSecurityGroupName = `${config.environment}-ovara-ecs-sg`;
const ecsSecurityGroup = new ec2.SecurityGroup(this, ecsSecurityGroupName, {
securityGroupName: ecsSecurityGroupName,
Expand Down Expand Up @@ -214,6 +224,41 @@ export class EcsStack extends cdk.Stack {
},
];

// Metriikat ja hälytykset

const ovaraCustomMetricsNamespace = `${config.environment}-OvaraCustomMetrics`;
const dbtRunnerFailedErrorMetricName = 'DbtRunnerFailedError';

const dbtRunnerFailedErrorMetric = new cloudwatch.Metric({
namespace: ovaraCustomMetricsNamespace,
metricName: dbtRunnerFailedErrorMetricName,
period: cdk.Duration.minutes(5),
unit: cloudwatch.Unit.NONE,
statistic: cloudwatch.Stats.SUM,
});

new logs.MetricFilter(
this,
`${config.environment}-dbtRunnerFailedErrorMetricFilter`,
{
filterPattern: logs.FilterPattern.literal('"Done. PASS" -"ERROR=0"'),
logGroup: dbtTaskLogGroup,
metricName: dbtRunnerFailedErrorMetricName,
metricNamespace: ovaraCustomMetricsNamespace,
}
);

const dbtRunnerFailedErrorAlarm = new cloudwatch.Alarm(this, 'AlarmId', {
metric: dbtRunnerFailedErrorMetric,
evaluationPeriods: 3,
datapointsToAlarm: 1,
alarmName: `${config.environment}-ovara-DbtRunnerFailedErrorError`,
alarmDescription: 'DBT-ajossa tapahtui virhe',
comparisonOperator: cloudwatch.ComparisonOperator.GREATER_THAN_THRESHOLD,
threshold: 0,
});
addActionsToAlarm(dbtRunnerFailedErrorAlarm);

cdkNag.NagSuppressions.addStackSuppressions(this, [
{ id: 'AwsSolutions-IAM5', reason: "Can't fix this." },
{ id: 'AwsSolutions-ECS2', reason: 'Static environment variables' },
Expand Down
11 changes: 4 additions & 7 deletions dbt-container/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@ echo "Running Ovara DBT script..."

start=$(date +%s)

echo "Listing contents of /root folder"
ls -Al /root

echo "Listing contents of /root/dbt folder"
ls -Al /root/dbt

cd dbt
. venv/bin/activate

if [[ -z "$1" ]]; then
echo "Running DBT without any extra paramaters"
dbt build --target=prod
echo "Finished running DBT"
else
echo "Running DBT with extra paramaters: $1"
dbt build --target=prod "$1"
echo "Finished running DBT"
fi

echo Ajon kesto `expr $(date +%s) - ${start}` s
echo "Ajon kesto `expr $(date +%s) - ${start}` s"

exit 0

0 comments on commit 3b2cd21

Please sign in to comment.