davidvasandani
11/28/2018 - 5:20 PM

notifications.tf

resource "aws_db_event_subscription" "default" {
  name      = "rds-event-sub-${var.environment}"
  sns_topic = "${aws_sns_topic.default.arn}"

  source_type = "db-instance"
  source_ids  = ["${aws_db_instance.main_rds_instance.id}"]

  event_categories = [
    "availability",
    "deletion",
    "failover",
    "failure",
    "low storage",
    "maintenance",
    "notification",
    "read replica",
    "recovery",
    "restoration",
  ]
}

resource "aws_sns_topic" "default" {
  name = "${var.environment}-rds-events"
}

resource "aws_lambda_permission" "allow_lambda_sns_to_slack" {
  statement_id  = "AllowSNSToSlackExecutionFromSNS"
  action        = "lambda:invokeFunction"
  function_name = "${module.sns_to_slack.lambda_function_arn}"
  principal     = "sns.amazonaws.com"
  source_arn    = "${aws_sns_topic.default.arn}"
}

resource "aws_sns_topic_subscription" "lambda_sns_to_slack" {
  topic_arn = "${aws_sns_topic.default.arn}"
  protocol  = "lambda"
  endpoint  = "${module.sns_to_slack.lambda_function_arn}"
}

module "sns_to_slack" {
  source = "github.com/builtinnya/aws-sns-slack-terraform/module"

  slack_webhook_url = "hooks.slack.com/services/123/456/789"

  slack_channel_map = {
    "topic-name" = "#slack-channel"
  }

  # The following variables are optional.
  lambda_iam_role_name   = "${var.environment}-sns-to-slack"
  lambda_iam_policy_name = "${var.environment}-sns-to-slack-policy"
  lambda_function_name   = "${var.environment}-sns-to-slack"
  default_username       = "AWS Lambda"
  default_channel        = "#ops-notifications"
  default_emoji          = ":aws:"
}