|
| 1 | +module "s3bucket_access_logs" { |
| 2 | + source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/s3bucket?ref=v1.0.8" |
| 3 | + |
| 4 | + name = "access-logs" |
| 5 | + |
| 6 | + aws_account_id = var.aws_account_id |
| 7 | + region = var.region |
| 8 | + project = var.project |
| 9 | + environment = var.environment |
| 10 | + component = var.component |
| 11 | + |
| 12 | + acl = "private" |
| 13 | + force_destroy = false |
| 14 | + versioning = true |
| 15 | + |
| 16 | + lifecycle_rules = [ |
| 17 | + { |
| 18 | + enabled = true |
| 19 | + |
| 20 | + noncurrent_version_transition = [ |
| 21 | + { |
| 22 | + noncurrent_days = "30" |
| 23 | + storage_class = "STANDARD_IA" |
| 24 | + } |
| 25 | + ] |
| 26 | + |
| 27 | + noncurrent_version_expiration = { |
| 28 | + noncurrent_days = "90" |
| 29 | + } |
| 30 | + |
| 31 | + abort_incomplete_multipart_upload = { |
| 32 | + days = "1" |
| 33 | + } |
| 34 | + } |
| 35 | + ] |
| 36 | + |
| 37 | + policy_documents = [ |
| 38 | + data.aws_iam_policy_document.s3bucket_access_logs.json |
| 39 | + ] |
| 40 | + |
| 41 | + public_access = { |
| 42 | + block_public_acls = true |
| 43 | + block_public_policy = true |
| 44 | + ignore_public_acls = true |
| 45 | + restrict_public_buckets = true |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + default_tags = { |
| 50 | + Name = "S3 bucket access logs" |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +data "aws_iam_policy_document" "s3bucket_access_logs" { |
| 55 | + statement { |
| 56 | + sid = "DontAllowNonSecureConnection" |
| 57 | + effect = "Deny" |
| 58 | + |
| 59 | + actions = [ |
| 60 | + "s3:*", |
| 61 | + ] |
| 62 | + |
| 63 | + resources = [ |
| 64 | + module.s3bucket_access_logs.arn, |
| 65 | + "${module.s3bucket_access_logs.arn}/*", |
| 66 | + ] |
| 67 | + |
| 68 | + principals { |
| 69 | + type = "AWS" |
| 70 | + |
| 71 | + identifiers = [ |
| 72 | + "*", |
| 73 | + ] |
| 74 | + } |
| 75 | + |
| 76 | + condition { |
| 77 | + test = "Bool" |
| 78 | + variable = "aws:SecureTransport" |
| 79 | + |
| 80 | + values = [ |
| 81 | + "false", |
| 82 | + ] |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + statement { |
| 87 | + sid = "AllowManagedAccountsToList" |
| 88 | + effect = "Allow" |
| 89 | + |
| 90 | + actions = [ |
| 91 | + "s3:ListBucket", |
| 92 | + ] |
| 93 | + |
| 94 | + resources = [ |
| 95 | + module.s3bucket_lambda_artefacts.arn, |
| 96 | + ] |
| 97 | + |
| 98 | + principals { |
| 99 | + type = "AWS" |
| 100 | + identifiers = [ |
| 101 | + "arn:aws:iam::${var.aws_account_id}:root" |
| 102 | + ] |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + statement { |
| 107 | + sid = "AllowManagedAccountsToGet" |
| 108 | + effect = "Allow" |
| 109 | + |
| 110 | + actions = [ |
| 111 | + "s3:GetObject", |
| 112 | + ] |
| 113 | + |
| 114 | + resources = [ |
| 115 | + "${module.s3bucket_lambda_artefacts.arn}/*", |
| 116 | + ] |
| 117 | + |
| 118 | + principals { |
| 119 | + type = "AWS" |
| 120 | + identifiers = [ |
| 121 | + "arn:aws:iam::${var.aws_account_id}:root" |
| 122 | + ] |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + statement { |
| 127 | + sid = "AllowS3AccessLogging" |
| 128 | + effect = "Allow" |
| 129 | + |
| 130 | + actions = [ |
| 131 | + "s3:PutObject", |
| 132 | + ] |
| 133 | + |
| 134 | + resources = [ |
| 135 | + "${module.s3bucket_access_logs.arn}/*", |
| 136 | + ] |
| 137 | + |
| 138 | + principals { |
| 139 | + type = "Service" |
| 140 | + |
| 141 | + identifiers = [ |
| 142 | + "logging.s3.amazonaws.com", |
| 143 | + ] |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments