|
| 1 | + |
| 2 | +# AWS Dynamic Lookups |
| 3 | +data "aws_availability_zones" "available" {} |
| 4 | +data "aws_region" "current" {} |
| 5 | +data "aws_caller_identity" "current" {} |
| 6 | + |
| 7 | +# Create all resources to Protect |
| 8 | +resource "aws_shield_protection" "nat_eip" { |
| 9 | + name = "imms-${var.environment}-fhir-api-eip-shield" |
| 10 | + resource_arn = "arn:aws:ec2:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:eip-allocation/${aws_eip.nat.id}" |
| 11 | +} |
| 12 | + |
| 13 | +resource "aws_shield_protection" "parent_dns" { |
| 14 | + provider = aws.use1 |
| 15 | + name = "imms-${var.environment}-fhir-api-parent-dns-shield" |
| 16 | + resource_arn = aws_route53_zone.parent_hosted_zone.arn |
| 17 | +} |
| 18 | + |
| 19 | +resource "aws_shield_protection" "child_dns" { |
| 20 | + provider = aws.use1 |
| 21 | + name = "imms-${var.environment}-fhir-api-parent-dns-shield" |
| 22 | + resource_arn = aws_route53_zone.child_hosted_zone.arn |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | + |
| 27 | +locals { |
| 28 | + regional_shield_arn = { |
| 29 | + nat_gateway_eip = aws_shield_protection.nat_eip.resource_arn |
| 30 | + } |
| 31 | + global_shield_arn = { |
| 32 | + route53_parent_zone = aws_shield_protection.parent_dns.resource_arn |
| 33 | + route53_child_zone = aws_shield_protection.child_dns.resource_arn |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +# Create Metric Alarms for each of those resources |
| 39 | +resource "aws_cloudwatch_metric_alarm" "ddos_protection_regional" { |
| 40 | + for_each = local.regional_shield_arn |
| 41 | + |
| 42 | + alarm_name = "imms-${var.environment}-shield_ddos_${each.key}" |
| 43 | + alarm_description = "Alarm when Shield detects DDoS on ${each.key}" |
| 44 | + |
| 45 | + namespace = "AWS/DDoSProtection" |
| 46 | + metric_name = "DDoSDetected" |
| 47 | + statistic = "Maximum" |
| 48 | + period = 60 |
| 49 | + evaluation_periods = 20 |
| 50 | + datapoints_to_alarm = 1 |
| 51 | + threshold = 0 |
| 52 | + comparison_operator = "GreaterThanThreshold" |
| 53 | + treat_missing_data = "notBreaching" |
| 54 | + |
| 55 | + dimensions = { |
| 56 | + ResourceArn = each.value |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +# Create Metric Alarms for Global Resources in us-east-1 Region |
| 61 | +resource "aws_cloudwatch_metric_alarm" "ddos_protection_global" { |
| 62 | + for_each = local.global_shield_arn |
| 63 | + |
| 64 | + provider = aws.use1 |
| 65 | + alarm_name = "imms-${var.environment}-shield_ddos_${each.key}" |
| 66 | + alarm_description = "Alarm when Shield detects DDoS on ${each.key}" |
| 67 | + |
| 68 | + namespace = "AWS/DDoSProtection" |
| 69 | + metric_name = "DDoSDetected" |
| 70 | + statistic = "Maximum" |
| 71 | + period = 60 |
| 72 | + evaluation_periods = 20 |
| 73 | + datapoints_to_alarm = 1 |
| 74 | + threshold = 0 |
| 75 | + comparison_operator = "GreaterThanThreshold" |
| 76 | + treat_missing_data = "notBreaching" |
| 77 | + |
| 78 | + dimensions = { |
| 79 | + ResourceArn = each.value |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | + |
| 84 | +# Event Bus Rule for eu-west-2 Region |
| 85 | + |
| 86 | +resource "aws_cloudwatch_event_rule" "shield_ddos_rule_regional" { |
| 87 | + name = "imms-${var.environment}-shield_ddos_rule_${data.aws_region.current.region}" |
| 88 | + description = "Forward Shield DDoS CloudWatch alarms to CSOC event bus" |
| 89 | + |
| 90 | + event_pattern = jsonencode({ |
| 91 | + "source" = ["aws.cloudwatch"], |
| 92 | + "detail-type" = ["CloudWatch Alarm State Change"], |
| 93 | + "resources" = [ |
| 94 | + for alarm in aws_cloudwatch_metric_alarm.ddos_protection_regional : alarm.arn |
| 95 | + ] |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | +resource "aws_cloudwatch_event_target" "shield_ddos_target_regional" { |
| 102 | + rule = aws_cloudwatch_event_rule.shield_ddos_rule_regional.name |
| 103 | + target_id = "csoc-eventbus" |
| 104 | + arn = "arn:aws:events:eu-west-2:${var.csoc_account_id}:event-bus/shield-eventbus" |
| 105 | + role_arn = aws_iam_role.eventbridge_forwarder_role.arn |
| 106 | +} |
| 107 | + |
| 108 | +# Event Bus Rule for us-east-1 Region |
| 109 | + |
| 110 | +resource "aws_cloudwatch_event_rule" "shield_ddos_rule_global" { |
| 111 | + provider = aws.use1 |
| 112 | + name = "imms-${var.environment}-shield_ddos_rule-us-east-1" |
| 113 | + description = "Forward Shield DDoS CloudWatch alarms (global) to CSOC event bus" |
| 114 | + |
| 115 | + event_pattern = jsonencode({ |
| 116 | + "source" = ["aws.cloudwatch"], |
| 117 | + "detail-type" = ["CloudWatch Alarm State Change"], |
| 118 | + "resources" = [ |
| 119 | + for alarm in aws_cloudwatch_metric_alarm.ddos_protection_global : alarm.arn |
| 120 | + ] |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +resource "aws_cloudwatch_event_target" "shield_ddos_target_global" { |
| 125 | + provider = aws.use1 |
| 126 | + rule = aws_cloudwatch_event_rule.shield_ddos_rule_global.name |
| 127 | + target_id = "csoc-eventbus" |
| 128 | + arn = "arn:aws:events:us-east-1:${var.csoc_account_id}:event-bus/shield-eventbus" |
| 129 | + role_arn = aws_iam_role.eventbridge_forwarder_role.arn |
| 130 | +} |
0 commit comments