Does anyone have any example of creating a cloudwa...
# aws
m
Does anyone have any example of creating a cloudwatch anomoly detection alarm with pulumi? Been using awsx exclusively for all metric/alarm needs but can't seem to figure this out.
just using aws.MetricAlarm
b
like a custom metric and alarm?
m
Yeah exactly, I've gone with that now. Just referenced the terraform docs for some examples.
b
Copy code
jenkins_backup_alarm(actions?: string[]) {
        var alarm_actions: any[] = [];

        if (actions) {
            for (let action of actions) {
                alarm_actions.push(this.topics[action]);
            }
        }

        var alarm_name = `${this.prefix}-jenkins-backup`;
        var alarm = new aws.cloudwatch.MetricAlarm(alarm_name, {
            namespace: `${this.company.toUpperCase()}/CUSTOM`,
            name: alarm_name,
            comparisonOperator: "LessThanThreshold",
            threshold: 1,
            statistic: "Maximum",
            period: 86400,
            evaluationPeriods: 1,
            treatMissingData: "breaching",
            metricName: "awsbackup-complete",
            dimensions: {
                environment: this.env.toLowerCase(),
                region: this.region,
            },
            alarmActions: alarm_actions,
            okActions: alarm_actions,
        });
    }
m
nice, ty
I ended up with the following as I specifically needed an anomaly detection (ie.
ANOMALY_DETECTION_BAND(m1)
Copy code
// alarm on 5xx error rise
    new aws.cloudwatch.MetricAlarm(
      resourceName(['api', '5xx'], service),
      {
        evaluationPeriods: 1,
        metricQueries: [
          {
            id: 'e1',
            returnData: true,
            expression: 'ANOMALY_DETECTION_BAND(m1)',
            label: 'Expected 5xx',
          },
          {
            id: 'm1',
            returnData: true,
            metric: pulumi.output({
              namespace: 'AWS/ApiGateway',
              metricName: '5XXError',
              unit: 'Count',
              stat: 'Sum',
              period: 300,
              dimensions: { ApiName: api.name },
            }),
          },
        ],
        thresholdMetricId: 'e1',
        alarmActions: [opsTopic],
        okActions: [opsTopic],
        treatMissingData: 'ignore',
        comparisonOperator: 'GreaterThanUpperThreshold',
      },
      { parent: this }
    )
b
👍