0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】CDKで「Alarm.__init__() got an unexpected keyword argument 'alarm_actions' 」エラーになった場合

Posted at

概要

CDKでcloudwatch alarmを作成したら、以下のエラーになりました。

TypeError: Alarm.init() got an unexpected keyword argument 'alarm_actions'

原因と解決方法

エラーが出た時のコードが以下。

from aws_cdk import (
    aws_cloudwatch as cloudwatch
)

### 省略 ###
        topic = sns.Topic(
            self, "Sample-SnsTopic",
            topic_name="Sample-Log",
            display_name="[Sample]-[Log]"
        )
        
        cloudwatch.Alarm(
            self, "Sample-Alarm",
            metric=cloudwatch.Metric(
                namespace="Sample-Log",
                metric_name="Sample",
                period=Duration.minutes(1),
                statistic="Sum"
            ),
            threshold=1,
            evaluation_periods=1,
            alarm_actions=[topic.topic_arn],
            treat_missing_data=cloudwatch.TreatMissingData.NOT_BREACHING
        )

エラーではalarm_actionsがない、と言われています。
CDK v2以降ではalarm_actionsではなくadd_alarm_action()メソッドでアクションを追加するらしい。

以下のように、cloudwatch.Alarmalarm_actions引数を削除し、add_alarm_action(cw_actions.SnsAction(topic))でSNS通知を追加する形に修正します。

from aws_cdk import (
    aws_cloudwatch as cloudwatch,
    aws_cloudwatch_actions as cw_actions
)

### 省略 ###

        topic = sns.Topic(
            self, "Sample-SnsTopic",
            topic_name="Sample-Log",
            display_name="[Sample]-[Log]"
        )

        cloudwatch_alarm = cloudwatch.Alarm(
            self, "Sample-Alarm",
            metric=cloudwatch.Metric(
                namespace="Sample-Log",
                metric_name="Sample",
                period=Duration.minutes(1),
                statistic="Sum"
            ),
            threshold=1,
            evaluation_periods=1,
            treat_missing_data=cloudwatch.TreatMissingData.NOT_BREACHING
        )
        cloudwatch_alarm.add_alarm_action(cw_actions.SnsAction(topic))

これによりエラーは解消されました!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?