# compliance-evaluation / aws / t1 "Standard" # # AWS Config recording to an encrypted, retained history, a conformance pack # of managed rules, an alert on every noncompliant result, and optional # automatic repair of public buckets. data "aws_caller_identity" "current" {} data "aws_partition" "current" {} locals { tags = merge({ owner = var.owner environment = var.environment data_classification = "internal" managed_by = "terraform" }, var.extra_tags) account = data.aws_caller_identity.current.account_id partition = data.aws_partition.current.partition # The pack: rule name = AWS managed rule identifier. Each is one common way # a resource drifts from where it should be. pack_rules = { restricted-ssh = "INCOMING_SSH_DISABLED" restricted-common-ports = "RESTRICTED_INCOMING_TRAFFIC" s3-bucket-public-write-prohibited = "S3_BUCKET_PUBLIC_WRITE_PROHIBITED" s3-bucket-ssl-requests-only = "S3_BUCKET_SSL_REQUESTS_ONLY" encrypted-volumes = "ENCRYPTED_VOLUMES" ec2-imdsv2-check = "EC2_IMDSV2_CHECK" vpc-flow-logs-enabled = "VPC_FLOW_LOGS_ENABLED" rds-instance-public-access-check = "RDS_INSTANCE_PUBLIC_ACCESS_CHECK" iam-root-access-key-check = "IAM_ROOT_ACCESS_KEY_CHECK" root-account-mfa-enabled = "ROOT_ACCOUNT_MFA_ENABLED" iam-user-mfa-enabled = "IAM_USER_MFA_ENABLED" multi-region-cloudtrail-enabled = "MULTI_REGION_CLOUD_TRAIL_ENABLED" cloud-trail-log-file-validation = "CLOUD_TRAIL_LOG_FILE_VALIDATION_ENABLED" } } data "aws_iam_policy_document" "key" { statement { sid = "AccountManagesKeyThroughIAM" actions = ["kms:*"] resources = ["*"] principals { type = "AWS" identifiers = ["arn:${local.partition}:iam::${local.account}:root"] } } statement { sid = "ConfigEncryptsHistory" actions = ["kms:GenerateDataKey", "kms:Decrypt"] resources = ["*"] principals { type = "Service" identifiers = ["config.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } } } resource "aws_kms_key" "config" { description = "AWS Config history for ${var.name}" enable_key_rotation = true deletion_window_in_days = 30 policy = data.aws_iam_policy_document.key.json } resource "aws_s3_bucket" "config" { bucket_prefix = "${var.name}-config-history-" } resource "aws_s3_bucket_public_access_block" "config" { bucket = aws_s3_bucket.config.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true } resource "aws_s3_bucket_versioning" "config" { bucket = aws_s3_bucket.config.id versioning_configuration { status = "Enabled" } } resource "aws_s3_bucket_server_side_encryption_configuration" "config" { bucket = aws_s3_bucket.config.id rule { apply_server_side_encryption_by_default { sse_algorithm = "aws:kms" kms_master_key_id = aws_kms_key.config.arn } bucket_key_enabled = true } } resource "aws_s3_bucket_lifecycle_configuration" "config" { bucket = aws_s3_bucket.config.id rule { id = "retention" status = "Enabled" filter {} expiration { days = var.retention_days } noncurrent_version_expiration { noncurrent_days = 30 } abort_incomplete_multipart_upload { days_after_initiation = 1 } } } data "aws_iam_policy_document" "config_bucket" { statement { sid = "ConfigChecksTheBucket" actions = ["s3:GetBucketAcl", "s3:ListBucket"] resources = [aws_s3_bucket.config.arn] principals { type = "Service" identifiers = ["config.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } } statement { sid = "ConfigWritesHistory" actions = ["s3:PutObject"] resources = ["${aws_s3_bucket.config.arn}/AWSLogs/${local.account}/Config/*"] principals { type = "Service" identifiers = ["config.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } condition { test = "StringEquals" variable = "s3:x-amz-acl" values = ["bucket-owner-full-control"] } } statement { sid = "EncryptedTransportOnly" effect = "Deny" actions = ["s3:*"] resources = [aws_s3_bucket.config.arn, "${aws_s3_bucket.config.arn}/*"] principals { type = "*" identifiers = ["*"] } condition { test = "Bool" variable = "aws:SecureTransport" values = ["false"] } } } resource "aws_s3_bucket_policy" "config" { bucket = aws_s3_bucket.config.id policy = data.aws_iam_policy_document.config_bucket.json } resource "aws_iam_service_linked_role" "config" { aws_service_name = "config.amazonaws.com" } resource "aws_config_configuration_recorder" "this" { name = "default" role_arn = aws_iam_service_linked_role.config.arn recording_group { all_supported = true include_global_resource_types = var.record_global_resources } recording_mode { recording_frequency = "CONTINUOUS" } } resource "aws_config_delivery_channel" "this" { name = "default" s3_bucket_name = aws_s3_bucket.config.bucket s3_kms_key_arn = aws_kms_key.config.arn snapshot_delivery_properties { delivery_frequency = "TwentyFour_Hours" } depends_on = [aws_config_configuration_recorder.this, aws_s3_bucket_policy.config] } resource "aws_config_configuration_recorder_status" "this" { name = aws_config_configuration_recorder.this.name is_enabled = true depends_on = [aws_config_delivery_channel.this] } resource "aws_config_conformance_pack" "baseline" { name = "${var.name}-baseline" template_body = yamlencode({ Resources = { for name, id in local.pack_rules : replace(title(replace(name, "-", " ")), " ", "") => { Type = "AWS::Config::ConfigRule" Properties = { ConfigRuleName = name Source = { Owner = "AWS", SourceIdentifier = id } } } } }) depends_on = [aws_config_configuration_recorder_status.this] } # Public read sits outside the pack, because a rule inside a pack cannot take # a remediation defined out here. resource "aws_config_config_rule" "s3_public_read" { name = "s3-bucket-public-read-prohibited" source { owner = "AWS" source_identifier = "S3_BUCKET_PUBLIC_READ_PROHIBITED" } depends_on = [aws_config_configuration_recorder_status.this] } data "aws_iam_policy_document" "ssm_trust" { statement { actions = ["sts:AssumeRole"] principals { type = "Service" identifiers = ["ssm.amazonaws.com"] } condition { test = "StringEquals" variable = "aws:SourceAccount" values = [local.account] } } } resource "aws_iam_role" "remediate" { count = var.remediate_public_buckets ? 1 : 0 name = "${var.name}-remediate-public-buckets" assume_role_policy = data.aws_iam_policy_document.ssm_trust.json } resource "aws_iam_role_policy" "remediate" { count = var.remediate_public_buckets ? 1 : 0 name = "block-bucket-public-access" role = aws_iam_role.remediate[0].id policy = jsonencode({ Version = "2012-10-17" Statement = [{ Effect = "Allow" Action = ["s3:PutBucketPublicAccessBlock", "s3:GetBucketPublicAccessBlock"] Resource = "arn:${local.partition}:s3:::*" }] }) } resource "aws_config_remediation_configuration" "s3_public_read" { count = var.remediate_public_buckets ? 1 : 0 config_rule_name = aws_config_config_rule.s3_public_read.name target_type = "SSM_DOCUMENT" target_id = "AWS-DisableS3BucketPublicReadWrite" automatic = true maximum_automatic_attempts = 3 retry_attempt_seconds = 60 parameter { name = "AutomationAssumeRole" static_value = aws_iam_role.remediate[0].arn } parameter { name = "S3BucketName" resource_value = "RESOURCE_ID" } } # Every rule result that turns noncompliant, from the pack or outside it. resource "aws_cloudwatch_event_rule" "noncompliant" { name = "${var.name}-noncompliant" description = "An AWS Config rule found a noncompliant resource." event_pattern = jsonencode({ source = ["aws.config"] "detail-type" = ["Config Rules Compliance Change"] detail = { messageType = ["ComplianceChangeNotification"] newEvaluationResult = { complianceType = ["NON_COMPLIANT"] } } }) } resource "aws_cloudwatch_event_target" "noncompliant" { rule = aws_cloudwatch_event_rule.noncompliant.name target_id = "alert" arn = var.alert_topic_arn }