|
| 1 | +# IAM role for EventBridge to write to Firehose |
| 2 | +resource "aws_iam_role" "eventbridge_firehose_role" { |
| 3 | + name = "${var.environment}-eventbridge-to-firehose-role" |
| 4 | + |
| 5 | + assume_role_policy = jsonencode({ |
| 6 | + Version = "2012-10-17" |
| 7 | + Statement = [{ |
| 8 | + Effect = "Allow" |
| 9 | + Principal = { |
| 10 | + Service = "events.amazonaws.com" |
| 11 | + } |
| 12 | + Action = "sts:AssumeRole" |
| 13 | + }] |
| 14 | + }) |
| 15 | + |
| 16 | + tags = { |
| 17 | + Environment = var.environment |
| 18 | + Purpose = "splunk-forwarding" |
| 19 | + ManagedBy = "terraform" |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +# IAM policy for EventBridge to access Firehose |
| 24 | +resource "aws_iam_role_policy" "eventbridge_to_firehose_policy" { |
| 25 | + name = "${var.environment}-eventbridge-to-firehose-policy" |
| 26 | + role = aws_iam_role.eventbridge_firehose_role.id |
| 27 | + |
| 28 | + policy = jsonencode({ |
| 29 | + Version = "2012-10-17" |
| 30 | + Statement = [{ |
| 31 | + Effect = "Allow" |
| 32 | + Action = [ |
| 33 | + "firehose:PutRecord", |
| 34 | + "firehose:PutRecordBatch" |
| 35 | + ] |
| 36 | + Resource = module.splunk_forwarder.firehose_delivery_stream_arn |
| 37 | + }] |
| 38 | + }) |
| 39 | +} |
| 40 | + |
| 41 | +# EventBridge rule to capture CloudWatch alarm state changes |
| 42 | +resource "aws_cloudwatch_event_rule" "alarm_state_change" { |
| 43 | + name = "cloudwatch-alarm-state-change-to-splunk" |
| 44 | + description = "Forward CloudWatch alarm state changes to Splunk via Firehose" |
| 45 | + |
| 46 | + event_pattern = jsonencode({ |
| 47 | + source = ["aws.cloudwatch"] |
| 48 | + detail-type = ["CloudWatch Alarm State Change"] |
| 49 | + }) |
| 50 | + |
| 51 | + tags = { |
| 52 | + Environment = var.environment |
| 53 | + Purpose = "splunk-forwarding" |
| 54 | + ManagedBy = "terraform" |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +# EventBridge target to send events to Firehose |
| 59 | +resource "aws_cloudwatch_event_target" "firehose_target" { |
| 60 | + rule = aws_cloudwatch_event_rule.alarm_state_change.name |
| 61 | + arn = module.splunk_forwarder.firehose_delivery_stream_arn |
| 62 | + role_arn = aws_iam_role.eventbridge_firehose_role.arn |
| 63 | + |
| 64 | + # Transform the CloudWatch alarm event into a format suitable for Splunk |
| 65 | + input_transformer { |
| 66 | + input_paths = { |
| 67 | + account = "$.account" |
| 68 | + region = "$.region" |
| 69 | + time = "$.time" |
| 70 | + alarm_name = "$.detail.alarmName" |
| 71 | + new_state = "$.detail.state.value" |
| 72 | + old_state = "$.detail.previousState.value" |
| 73 | + reason = "$.detail.state.reason" |
| 74 | + } |
| 75 | + |
| 76 | + input_template = jsonencode({ |
| 77 | + time = "<time>" |
| 78 | + source = "elid-${var.environment}:cloudwatch:alarm" |
| 79 | + sourcetype = "aws:cloudwatch:alarm" |
| 80 | + event = { |
| 81 | + alarm_name = "<alarm_name>" |
| 82 | + new_state = "<new_state>" |
| 83 | + old_state = "<old_state>" |
| 84 | + reason = "<reason>" |
| 85 | + region = "<region>" |
| 86 | + } |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
0 commit comments