Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions infra/csoc_role_creation.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resource "aws_iam_role" "eventbridge_forwarder_role" {
name = "${local.short_prefix}-eventbridge-forwarder-role"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no short_prefix in the infra project. We could use imms-${var.environment} instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you Matt, done

assume_role_policy = jsonencode({
Version : "2012-10-17",
Statement = [{
Sid = "TrustEventBridgeService",
Effect = "Allow",
Principal = { Service = "events.amazonaws.com" },
Action = "sts:AssumeRole",
Condition = {
StringEquals = {
"aws:SourceAccount" = var.immunisation_account_id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be imms_account_id

}
}
}]
})
}

resource "aws_iam_role_policy" "eventbridge_forwarder_policy" {
name = "${local.short_prefix}-eventbridge-forwarder-policy"
role = aws_iam_role.eventbridge_forwarder_role.id

policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Sid = "ActionsForResource",
Effect = "Allow",
Action = ["events:PutEvents"],
Resource = [
"arn:aws:events:eu-west-2:${var.csoc_account_id}:event-bus/shield-eventbus"
]
}]
})
}
1 change: 1 addition & 0 deletions infra/environments/dev/variables.tfvars
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imms_account_id = "345594581768"
dspp_account_id = "603871901111"
mns_account_id = "631615744739"
csoc_account_id = "693466633220"
admin_role = "root" # We shouldn't be using the root account. There should be an Admin role
dev_ops_role = "role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_DEV-IMMS-Devops_745af4f208886ecc"
dspp_admin_role = "root"
Expand Down
1 change: 1 addition & 0 deletions infra/environments/preprod/variables.tfvars
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imms_account_id = "084828561157"
dspp_account_id = "603871901111"
mns_account_id = "631615744739"
csoc_account_id = "693466633220"
# admin_role = "role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_PREPROD-IMMS-Admin_acce656dcacf6f4c"
admin_role = "root"
dev_ops_role = "role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_PREPROD-IMMS-Devops_1d28e4f37b940bcd"
Expand Down
1 change: 1 addition & 0 deletions infra/environments/prod/variables.tfvars
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
imms_account_id = "664418956997"
dspp_account_id = "232116723729"
mns_account_id = "758334270304"
csoc_account_id = "693466633220"
# admin_role = "role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_PROD-IMMS-Admin_edd6691e4b74064e"
admin_role = "root"
dev_ops_role = "role/aws-reserved/sso.amazonaws.com/eu-west-2/AWSReservedSSO_PROD-IMMS-Devops_8f32c62195d56b76"
Expand Down
150 changes: 150 additions & 0 deletions infra/shield_protection.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@

# AWS Dynamic Lookups
data "aws_availability_zones" "available" {}
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}

provider "aws" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take it this didn't work with the default provider?

Copy link
Contributor Author

@Akol125 Akol125 Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it mentioned in the conference page that some global resources would not work with eu-west-2 such as Route53, cloudfront etc, Hence the reason why I am using us-east-1. Number two in this confluence link -> shield advance docs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like it's needed for global resources, but maybe we could put the alternative alias provider in main: https://developer.hashicorp.com/terraform/language/block/provider

alias = "use1"
region = "us-east-1"
}

# Create all resources to Protect
resource "aws_shield_protection" "nat_eip" {
name = "shield_nat_eip"
resource_arn = "arn:aws:ec2:${data.aws_region.current.region}:${data.aws_caller_identity.current.account_id}:eip-allocation/${aws_eip.example.id}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aws_eip.example? Should this be aws_eip.nat?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


tags = {
Environment = "imms-${var.environment}-fhir-api-eip-shield"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it should go in the name attribute - same for the other aws_shield_protection resources

}
}

resource "aws_shield_protection" "parent_dns" {
provider = aws.use1
name = "shield_ddos_parent_zone"
resource_arn = aws_route53_zone.parent_hosted_zone.arn

tags = {
Environment = "imms-${var.environment}-fhir-api-parent-dns-shield"
}
}

resource "aws_shield_protection" "child_dns" {
provider = aws.use1
name = "route53_shield_ddos_childzone"
resource_arn = aws_route53_zone.child_hosted_zone.arn

tags = {
Environment = "imms-${var.environment}-fhir-api-child-dns-shield"
}
}



locals {
regional_shield_arn = {
nat_gateway_eip = aws_shield_protection.nat_eip.resource_arn
}
}

locals {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can combine these outer locals blocks

global_shield_arn = {
route53_parent_zone = aws_shield_protection.parent_dns.resource_arn
route53_child_zone = aws_shield_protection.child_dns.resource_arn
}
}


# Create Metric Alarms for each of those resources
resource "aws_cloudwatch_metric_alarm" "ddos_protection_regional" {
for_each = local.regional_shield_arn

alarm_name = "shield_ddos_${each.key}"
alarm_description = "Alarm when Shield detects DDoS on ${each.key}"

namespace = "AWS/DDoSProtection"
metric_name = "DDoSDetected"
statistic = "Maximum"
period = 60
evaluation_periods = 20
datapoints_to_alarm = 1
threshold = 0
comparison_operator = "GreaterThanThreshold"
treat_missing_data = "notBreaching"

dimensions = {
ResourceArn = each.value
}
}

# Create Metric Alarms for Global Resources in us-east-1 Region
resource "aws_cloudwatch_metric_alarm" "ddos_protection_global" {
for_each = locals.global_shield_arn

provider = aws.use1
alarm_name = "shield_ddos_${each.key}"
alarm_description = "Alarm when Shield detects DDoS on ${each.key}"

namespace = "AWS/DDoSProtection"
metric_name = "DDoSDetected"
statistic = "Maximum"
period = 60
evaluation_periods = 20
datapoints_to_alarm = 1
threshold = 0
comparison_operator = "GreaterThanThreshold"
treat_missing_data = "notBreaching"

dimensions = {
ResourceArn = each.value
}
}


# Event Bus Rule for eu-west-2 Region

resource "aws_cloudwatch_event_rule" "shield_ddos_rule_regional" {
name = "imms_${var.environment}_shield_ddos_rule_${data.aws_region.current.name}"
description = "Forward Shield DDoS CloudWatch alarms to CSOC event bus"

event_pattern = jsonencode({
"source" = ["aws.cloudwatch"],
"detail-type" = ["CloudWatch Alarm State Change"],
"resources" = [
for alarm in aws_cloudwatch_metric_alarm.ddos_protection_regional : alarm.arn
]
})
}



resource "aws_cloudwatch_event_target" "shield_ddos_target_regional" {
rule = aws_cloudwatch_event_rule.shield_ddos_rule_regional.name
target_id = "csoc-eventbus"
arn = "arn:aws:events:eu-west-2:${var.csoc_account_id}:event-bus/shield-eventbus"
role_arn = aws_iam_role.shield_ddos_forwarder.arn
}

# Event Bus Rule for us-east-1 Region

resource "aws_cloudwatch_event_rule" "shield_ddos_rule_global" {
provider = aws.use1
name = "imms_${var.environment}_shield_ddos_rule_us-east-1"
description = "Forward Shield DDoS CloudWatch alarms (global) to CSOC event bus"

event_pattern = jsonencode({
"source" = ["aws.cloudwatch"],
"detail-type" = ["CloudWatch Alarm State Change"],
"resources" = [
for alarm in aws_cloudwatch_metric_alarm.ddos_protection_global : alarm.arn
]
})
}

resource "aws_cloudwatch_event_target" "shield_ddos_target_global" {
provider = aws.use1
rule = aws_cloudwatch_event_rule.shield_ddos_rule_global.name
target_id = "csoc-eventbus"
arn = "arn:aws:events:us-east-1:${var.csoc_account_id}:event-bus/shield-eventbus"
role_arn = aws_iam_role.shield_ddos_forwarder.arn
}
7 changes: 7 additions & 0 deletions infra/shield_subscription.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// One-time Shield Advanced subscription for the account.
// This resource is account-level.

resource "aws_shield_subscription" "shield_subscription" {
auto_renew = "ENABLED"

}
6 changes: 6 additions & 0 deletions infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ variable "dspp_account_id" {
description = "DSPP Core AWS account ID"
type = string
}
variable "csoc_account_id" {
description = "CSOC Core AWS account ID"
type = string

}

variable "auto_ops_role" {
default = "role/auto-ops"
type = string
Expand Down
34 changes: 34 additions & 0 deletions terraform/csoc_role_creation.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
resource "aws_iam_role" "eventbridge_forwarder_role" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used? This is the only change in the terraform folder

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No it isn't, it was a stale file, I forgot to remove, as I initially thought the code would go into terraform service level. Removing now

name = "${local.short_prefix}-eventbridge-forwarder-role"
assume_role_policy = jsonencode({
Version : "2012-10-17",
Statement = [{
Sid = "TrustEventBridgeService",
Effect = "Allow",
Principal = { Service = "events.amazonaws.com" },
Action = "sts:AssumeRole",
Condition = {
StringEquals = {
"aws:SourceAccount" = var.immunisation_account_id
}
}
}]
})
}

resource "aws_iam_role_policy" "eventbridge_forwarder_policy" {
name = "${local.short_prefix}-eventbridge-forwarder-policy"
role = aws_iam_role.eventbridge_forwarder_role.id

policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Sid = "ActionsForResource",
Effect = "Allow",
Action = ["events:PutEvents"],
Resource = [
"arn:aws:events:eu-west-2:${var.csoc_account_id}:event-bus/shield-eventbus"
]
}]
})
}