_This issue was originally opened by @tvd2017 as hashicorp/terraform#16108. It was migrated here as a result of the provider split. The original body of the issue is below._
I created the aws_cloudwatch_metric_alarm as follows, but need to know how to add the Notification to the alarm using an SNS topic I've created namely "My-SNS-Topic":
resource "aws_cloudwatch_metric_alarm" "ScaleUp-Mem-Alarm" {
alarm_name = "ScaleUp-MEM-Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
period = "300"
evaluation_periods = "3"
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
statistic = "Average"
threshold = "90"
alarm_description = "Created by Terraform"
alarm_actions = [
"${aws_appautoscaling_policy.ScaleUp_Policy.arn}"
]
dimensions {
cluster_name = "${aws_ecs_cluster.MyCluster.name}"
service_name = "${aws_ecs_service.MyService.name}"
}
}
In the AWS Console, when I edit the alarm I can manually add the Notification with:
Whenever this alarm: State is Alarm
Send notification to: My-SNS-Topic
But I need to add this Notification step into the Terraform script. Can anyone help? Thanks so much!
Hi @tvd2017,
Thanks for opening this issue. You are almost good with your configuration: you specified alarm_actions, which is about what to do when you are transitioning to an ALARM state.
You specified an appautoscaling_policy, but you can also specify a topic ARN, as in :
resource "aws_cloudwatch_metric_alarm" "ScaleUp-Mem-Alarm" {
alarm_name = "ScaleUp-MEM-Alarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
period = "300"
evaluation_periods = "3"
metric_name = "MemoryUtilization"
namespace = "AWS/ECS"
statistic = "Average"
threshold = "90"
alarm_description = "Created by Terraform"
alarm_actions = [
"${aws_appautoscaling_policy.ScaleUp_Policy.arn}",
"${aws_sns_topic.mytopic.arn}"
]
dimensions {
cluster_name = "${aws_ecs_cluster.MyCluster.name}"
service_name = "${aws_ecs_service.MyService.name}"
}
}
See the ${aws_sns_topic.mytopic.arn} line :)
Tell me if it fully answers your question!
Thanks :)
Hi Ninir, thanks so much. It's working good now. I knew that I was missing something like this. Greatly appreciate your help! Awesome 👍
Thank you for the quick feedback, and nice work on your part 👍
Happy Terraforming! :)
Hi Gauthier and Expert,
Would you be able to help us with this?
https://github.com/terraform-providers/terraform-provider-aws/issues/1836
Thanks.
-Tom
Can you help how to create a secondary action "whenever the alarm state is ok".
Adding ${aws_sns_topic.mytopic.arn} to my terraform script has added the action "whenever the alarm state is alarm". I need to add "whenever the alarm state is ok" via terraform as well.
Thanks for your help @Ninir.
I had the same issue as OP. I think this could be a little bit better documented.
Can you help how to create a secondary action "whenever the alarm state is ok".
Adding ${aws_sns_topic.mytopic.arn} to my terraform script has added the action "whenever the alarm state is alarm". I need to add "whenever the alarm state is ok" via terraform as well.
@rajarammohan88 W.r.t above, I think this has been resolved in the latest version with ok_actions.
I would agree a bit more verbosity in understanding "actions" would be useful, to detail that notifications to SNS topics fits within the scope of an action. The AWS documentation for Cloudwatch actions seems to imply that notifications are not actions.
I'm going to lock this issue because it has been closed for _30 days_ ⏳. This helps our maintainers find and focus on the active issues.
If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!
Most helpful comment
Can you help how to create a secondary action "whenever the alarm state is ok".
Adding ${aws_sns_topic.mytopic.arn} to my terraform script has added the action "whenever the alarm state is alarm". I need to add "whenever the alarm state is ok" via terraform as well.