Skip to content

Commit 92ba54a

Browse files
committed
CCM-9873: toggle CDN. Add logging bucket for CDN access
1 parent 32acbeb commit 92ba54a

File tree

3 files changed

+188
-2
lines changed

3 files changed

+188
-2
lines changed

infrastructure/terraform/components/app/cloudfront_distribution_cdn.tf renamed to infrastructure/terraform/components/app/cloudfront_distribution_download.tf

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
resource "aws_cloudfront_distribution" "main" {
22
provider = aws.us-east-1
33

4-
enabled = true
5-
is_ipv6_enabled = true
4+
enabled = var.enable_file_download
5+
is_ipv6_enabled = var.enable_file_download
66
comment = "NHS Notify Template files CDN (${local.csi})"
77
default_root_object = "index.html"
88
price_class = "PriceClass_100" # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-distributionconfig.html#cfn-cloudfront-distribution-distributionconfig-priceclass
@@ -24,6 +24,11 @@ resource "aws_cloudfront_distribution" "main" {
2424
ssl_support_method = "sni-only"
2525
}
2626

27+
logging_config {
28+
bucket = module.s3bucket_cf_logs.bucket_regional_domain_name
29+
include_cookies = false
30+
}
31+
2732
origin {
2833
domain_name = module.backend_api.download_bucket_regional_domain_name
2934
origin_access_control_id = aws_cloudfront_origin_access_control.main.id
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
module "s3bucket_cf_logs" {
2+
source = "git::https://github.com/NHSDigital/nhs-notify-shared-modules.git//infrastructure/modules/s3bucket?ref=v1.0.9"
3+
providers = {
4+
aws = aws.us-east-1
5+
}
6+
7+
name = "cf-logs"
8+
9+
aws_account_id = var.aws_account_id
10+
region = "us-east-1"
11+
project = var.project
12+
environment = var.environment
13+
component = var.component
14+
15+
acl = "private"
16+
force_destroy = false
17+
versioning = true
18+
19+
object_ownership = "ObjectWriter"
20+
21+
lifecycle_rules = [
22+
{
23+
prefix = ""
24+
enabled = true
25+
26+
transition = [
27+
{
28+
days = "90"
29+
storage_class = "STANDARD_IA"
30+
},
31+
{
32+
days = "180"
33+
storage_class = "GLACIER"
34+
}
35+
]
36+
37+
expiration = {
38+
days = "365"
39+
}
40+
41+
42+
noncurrent_version_transition = [
43+
{
44+
noncurrent_days = "30"
45+
storage_class = "STANDARD_IA"
46+
},
47+
{
48+
noncurrent_days = "180"
49+
storage_class = "GLACIER"
50+
}
51+
52+
]
53+
54+
noncurrent_version_expiration = {
55+
noncurrent_days = "365"
56+
}
57+
58+
abort_incomplete_multipart_upload = {
59+
days = "1"
60+
}
61+
}
62+
]
63+
64+
policy_documents = [
65+
data.aws_iam_policy_document.s3bucket_cf_logs.json
66+
]
67+
68+
bucket_logging_target = {
69+
bucket = local.s3_buckets["access_logs"]["id"]
70+
}
71+
72+
public_access = {
73+
block_public_acls = true
74+
block_public_policy = true
75+
ignore_public_acls = true
76+
restrict_public_buckets = true
77+
}
78+
79+
default_tags = {
80+
Name = "Cloudfront Logs"
81+
}
82+
}
83+
84+
data "aws_iam_policy_document" "s3bucket_cf_logs" {
85+
statement {
86+
sid = "DontAllowNonSecureConnection"
87+
effect = "Deny"
88+
89+
actions = [
90+
"s3:*",
91+
]
92+
93+
resources = [
94+
module.s3bucket_cf_logs.arn,
95+
"${module.s3bucket_cf_logs.arn}/*",
96+
]
97+
98+
principals {
99+
type = "AWS"
100+
101+
identifiers = [
102+
"*",
103+
]
104+
}
105+
106+
condition {
107+
test = "Bool"
108+
variable = "aws:SecureTransport"
109+
110+
values = [
111+
"false",
112+
]
113+
}
114+
}
115+
116+
statement {
117+
effect = "Allow"
118+
actions = ["s3:PutObject"]
119+
resources = [
120+
"${module.s3bucket_cf_logs.arn}/*",
121+
]
122+
123+
principals {
124+
type = "Service"
125+
identifiers = ["logging.s3.amazonaws.com"]
126+
}
127+
condition {
128+
test = "StringEquals"
129+
variable = "aws:SourceAccount"
130+
values = [
131+
var.aws_account_id
132+
]
133+
}
134+
}
135+
136+
statement {
137+
sid = "AllowManagedAccountsToList"
138+
effect = "Allow"
139+
140+
actions = [
141+
"s3:ListBucket",
142+
]
143+
144+
resources = [
145+
module.s3bucket_cf_logs.arn,
146+
]
147+
148+
principals {
149+
type = "AWS"
150+
identifiers = [
151+
"arn:aws:iam::${var.aws_account_id}:root"
152+
]
153+
}
154+
}
155+
156+
statement {
157+
sid = "AllowManagedAccountsToGet"
158+
effect = "Allow"
159+
160+
actions = [
161+
"s3:GetObject",
162+
]
163+
164+
resources = [
165+
"${module.s3bucket_cf_logs.arn}/*",
166+
]
167+
168+
principals {
169+
type = "AWS"
170+
identifiers = [
171+
"arn:aws:iam::${var.aws_account_id}:root"
172+
]
173+
}
174+
}
175+
}

infrastructure/terraform/components/app/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,12 @@ variable "enable_proofing" {
194194
default = false
195195
}
196196

197+
variable "enable_file_download" {
198+
type = bool
199+
description = "Feature flag for downloading files"
200+
default = true
201+
}
202+
197203
variable "observability_account_id" {
198204
type = string
199205
description = "The Observability Account ID that needs access"

0 commit comments

Comments
 (0)