# guardrail-baseline / aws / t1 "Standard" # # Two service control policies. The first protects the security baseline: the # organisation, audit logging, threat detection, account-wide public access # blocks, the root user, and the security team's own roles. The second keeps # regional services inside the regions you use. Named roles are exempt, so # break-glass and security automation keep working. # GovCloud and China use their own partition in ARNs. data "aws_partition" "current" {} locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) exempt = length(var.exempt_role_arns) > 0 } data "aws_iam_policy_document" "baseline" { statement { sid = "DenyLeavingOrganization" effect = "Deny" actions = ["organizations:LeaveOrganization"] resources = ["*"] } statement { sid = "DenyAuditAndDetectionTampering" effect = "Deny" actions = [ "cloudtrail:StopLogging", "cloudtrail:DeleteTrail", "cloudtrail:UpdateTrail", "cloudtrail:PutEventSelectors", "config:StopConfigurationRecorder", "config:DeleteConfigurationRecorder", "config:DeleteDeliveryChannel", "guardduty:DeleteDetector", "guardduty:DisassociateFromAdministratorAccount", "securityhub:DisableSecurityHub", "access-analyzer:DeleteAnalyzer", "s3:PutAccountPublicAccessBlock", ] resources = ["*"] dynamic "condition" { for_each = local.exempt ? [1] : [] content { test = "ArnNotLike" variable = "aws:PrincipalArn" values = var.exempt_role_arns } } } statement { sid = "ProtectSecurityRoles" effect = "Deny" actions = [ "iam:AttachRolePolicy", "iam:DetachRolePolicy", "iam:PutRolePolicy", "iam:DeleteRolePolicy", "iam:UpdateAssumeRolePolicy", "iam:UpdateRole", "iam:DeleteRole", "iam:PutRolePermissionsBoundary", "iam:DeleteRolePermissionsBoundary", ] resources = ["arn:${data.aws_partition.current.partition}:iam::*:role${var.security_role_path}*"] dynamic "condition" { for_each = local.exempt ? [1] : [] content { test = "ArnNotLike" variable = "aws:PrincipalArn" values = var.exempt_role_arns } } } statement { sid = "DenyRootUser" effect = "Deny" actions = ["*"] resources = ["*"] condition { test = "StringLike" variable = "aws:PrincipalArn" values = ["arn:${data.aws_partition.current.partition}:iam::*:root"] } } } # Global services answer from us-east-1 whatever region you use, so they are # left out of the region rule. Review this list against the services you run. data "aws_iam_policy_document" "regions" { statement { sid = "DenyOutsideAllowedRegions" effect = "Deny" not_actions = [ "iam:*", "organizations:*", "account:*", "sts:*", "support:*", "health:*", "budgets:*", "ce:*", "cur:*", "billing:*", "trustedadvisor:*", "route53:*", "route53domains:*", "cloudfront:*", "globalaccelerator:*", "waf:*", "wafv2:*", "shield:*", "kms:*", "s3:GetBucketLocation", "s3:ListAllMyBuckets", ] resources = ["*"] condition { test = "StringNotEquals" variable = "aws:RequestedRegion" values = var.allowed_regions } dynamic "condition" { for_each = local.exempt ? [1] : [] content { test = "ArnNotLike" variable = "aws:PrincipalArn" values = var.exempt_role_arns } } } } resource "aws_organizations_policy" "baseline" { name = "guardrail-baseline" description = "Protects the organisation, audit logging, detection, public access blocks, the root user, and security roles." type = "SERVICE_CONTROL_POLICY" content = data.aws_iam_policy_document.baseline.json } resource "aws_organizations_policy" "regions" { name = "guardrail-regions" description = "Regional services only in: ${join(", ", var.allowed_regions)}." type = "SERVICE_CONTROL_POLICY" content = data.aws_iam_policy_document.regions.json } resource "aws_organizations_policy_attachment" "baseline" { for_each = toset(var.target_ids) policy_id = aws_organizations_policy.baseline.id target_id = each.value } resource "aws_organizations_policy_attachment" "regions" { for_each = toset(var.target_ids) policy_id = aws_organizations_policy.regions.id target_id = each.value }