sns_alerts.tf 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. resource "aws_cloudwatch_log_group" "CAAccountCAAccountCloudTrailAnalysis" {
  2. name = "CAAccountCloudTrailAnalysis"
  3. }
  4. resource "aws_iam_role" "ca_account_cloudtrail_role" {
  5. name = "ca_account_cloudtrail_role"
  6. assume_role_policy = <<EOF
  7. {
  8. "Version": "2012-10-17",
  9. "Statement": [
  10. {
  11. "Sid": "",
  12. "Effect": "Allow",
  13. "Principal": {
  14. "Service": "cloudtrail.amazonaws.com"
  15. },
  16. "Action": "sts:AssumeRole"
  17. }
  18. ]
  19. }
  20. EOF
  21. }
  22. resource "aws_iam_role_policy" "allow_stream_policy" {
  23. name = "allow_stream_change"
  24. role = aws_iam_role.ca_account_cloudtrail_role.id
  25. policy = <<EOF
  26. {
  27. "Version": "2012-10-17",
  28. "Statement": [
  29. {
  30. "Effect": "Allow",
  31. "Action": [
  32. "logs:CreateLogStream",
  33. "logs:PutLogEvents"
  34. ],
  35. "Resource": [
  36. "${aws_cloudwatch_log_group.CAAccountCAAccountCloudTrailAnalysis.arn}"
  37. ]
  38. }
  39. ]
  40. }
  41. EOF
  42. }
  43. resource "aws_sns_topic" "ca_account_notification" {
  44. name = "CAAccountNotification"
  45. }
  46. resource "aws_sns_topic_subscription" "ca_account_notification" {
  47. for_each = local.recipients
  48. topic_arn = aws_sns_topic.ca_account_notification.arn
  49. protocol = "email"
  50. endpoint = each.value
  51. }
  52. #resource "aws_cloudwatch_log_metric_filter" "rootEvent" {
  53. # name = "Root_Account_Login"
  54. # pattern = <<EOF
  55. #{ ($.eventSource = "signin.amazonaws.com" ) && ( $.userIdentity.type = "Root" ) }
  56. #EOF
  57. # log_group_name = "${aws_cloudwatch_log_group.CAAccountCloudTrailAnalysis.name}"
  58. #
  59. # metric_transformation {
  60. # name = "${var.thiseventname}"
  61. # namespace = "${var.thisnamespace}"
  62. # value = "1"
  63. # }
  64. #}
  65. data "aws_caller_identity" "current" {}
  66. data "aws_iam_account_alias" "current" {}
  67. #resource "aws_cloudwatch_metric_alarm" "rootAlarm" {
  68. # alarm_name = "Root_Account_Login"
  69. # comparison_operator = "GreaterThanOrEqualToThreshold"
  70. # evaluation_periods = "1"
  71. # metric_name = "${var.thiseventname}"
  72. # namespace = "${var.thisnamespace}"
  73. # period = "300"
  74. # statistic = "Sum"
  75. # threshold = "1"
  76. # alarm_description = "in the AWS account with id = ${data.aws_caller_identity.current.account_id} and alias = ${data.aws_iam_account_alias.current.account_alias} the root user logged in"
  77. # alarm_actions = ["${aws_sns_topic.ca_account_notification.arn}"]
  78. #}