1+ locals {
2+ # required by USA-based CI pipeline runners to run smoke tests
3+ allow_us_comms = ! local. is_production
4+ }
5+
6+ resource "aws_cloudfront_origin_access_control" "s3" {
7+ name = " ${ terraform . workspace } _cloudfront_s3_oac_policy"
8+ description = " CloudFront S3 OAC"
9+ origin_access_control_origin_type = " s3"
10+ signing_behavior = " never"
11+ signing_protocol = " sigv4"
12+ }
13+
114module "cloudfront_firewall_waf_v2" {
215 source = " ./modules/firewall_waf_v2"
316 cloudfront_acl = true
@@ -8,16 +21,136 @@ module "cloudfront_firewall_waf_v2" {
821 providers = { aws = aws.us_east_1 }
922}
1023
11- module "cloudfront-distribution-lg" {
12- source = " ./modules/cloudfront"
13- bucket_domain_name = module. ndr-lloyd-george-store . bucket_regional_domain_name
14- bucket_id = module. ndr-lloyd-george-store . bucket_id
15- qualifed_arn = module. edge-presign-lambda . qualified_arn
16- depends_on = [module . edge-presign-lambda . qualified_arn , module . ndr-lloyd-george-store . bucket_id , module . ndr-lloyd-george-store . bucket_domain_name , module . ndr-document-pending-review-store . bucket_id , module . ndr-document-pending-review-store . bucket_domain_name ]
17- web_acl_id = try (module. cloudfront_firewall_waf_v2 [0 ]. arn , " " )
18- has_secondary_bucket = local. is_production ? false : true
19- secondary_bucket_domain_name = module. ndr-document-pending-review-store . bucket_regional_domain_name
20- secondary_bucket_id = module. ndr-document-pending-review-store . bucket_id
21- secondary_bucket_path_pattern = " /review/*"
22- log_bucket_id = local. access_logs_bucket_id
23- }
24+ resource "aws_cloudfront_distribution" "s3_presign_mask" {
25+ price_class = " PriceClass_100"
26+
27+ origin {
28+ domain_name = module. ndr-lloyd-george-store . bucket_regional_domain_name
29+ origin_id = module. ndr-lloyd-george-store . bucket_id
30+ origin_access_control_id = aws_cloudfront_origin_access_control. s3 . id
31+ }
32+ enabled = true
33+ is_ipv6_enabled = true
34+
35+ default_cache_behavior {
36+ allowed_methods = [" HEAD" , " GET" , " OPTIONS" ]
37+ cached_methods = [" HEAD" , " GET" , " OPTIONS" ]
38+ target_origin_id = module. ndr-lloyd-george-store . bucket_id
39+ viewer_protocol_policy = " redirect-to-https"
40+ cache_policy_id = aws_cloudfront_cache_policy. nocache . id
41+ origin_request_policy_id = aws_cloudfront_origin_request_policy. viewer . id
42+
43+ lambda_function_association {
44+ event_type = " origin-request"
45+ lambda_arn = module. edge-presign-lambda . qualified_arn
46+ }
47+ }
48+
49+ origin {
50+ domain_name = module. ndr-document-pending-review-store . bucket_regional_domain_name
51+ origin_id = module. ndr-document-pending-review-store . bucket_id
52+ origin_access_control_id = aws_cloudfront_origin_access_control. s3 . id
53+ }
54+
55+ ordered_cache_behavior {
56+ allowed_methods = [" HEAD" , " GET" , " OPTIONS" ]
57+ cached_methods = [" HEAD" , " GET" , " OPTIONS" ]
58+ path_pattern = " /review/*"
59+ target_origin_id = module. ndr-document-pending-review-store . bucket_id
60+ viewer_protocol_policy = " redirect-to-https"
61+ cache_policy_id = aws_cloudfront_cache_policy. nocache . id
62+ origin_request_policy_id = aws_cloudfront_origin_request_policy. viewer . id
63+
64+ lambda_function_association {
65+ event_type = " origin-request"
66+ lambda_arn = module. edge-presign-lambda . qualified_arn
67+ }
68+ }
69+
70+ origin {
71+ domain_name = module. ndr-bulk-staging-store . bucket_regional_domain_name
72+ origin_id = module. ndr-bulk-staging-store . bucket_id
73+ origin_access_control_id = aws_cloudfront_origin_access_control. s3 . id
74+ }
75+
76+ ordered_cache_behavior {
77+ allowed_methods = [" DELETE" , " GET" , " HEAD" , " OPTIONS" , " PATCH" , " POST" , " PUT" ]
78+ cached_methods = [" HEAD" , " GET" , " OPTIONS" ]
79+ path_pattern = " /upload/*"
80+ target_origin_id = module. ndr-bulk-staging-store . bucket_id
81+ viewer_protocol_policy = " redirect-to-https"
82+ cache_policy_id = aws_cloudfront_cache_policy. nocache . id
83+ origin_request_policy_id = aws_cloudfront_origin_request_policy. viewer . id
84+
85+ lambda_function_association {
86+ event_type = " origin-request"
87+ lambda_arn = module. edge-presign-lambda . qualified_arn
88+ }
89+ }
90+
91+ viewer_certificate {
92+ cloudfront_default_certificate = true
93+ }
94+
95+ restrictions {
96+ geo_restriction {
97+ restriction_type = " whitelist"
98+ locations = local. allow_us_comms ? [" GB" , " US" ] : [" GB" ]
99+ }
100+ }
101+
102+ web_acl_id = try (module. cloudfront_firewall_waf_v2 [0 ]. arn , " " )
103+ }
104+
105+ resource "aws_cloudfront_origin_request_policy" "viewer" {
106+ name = " ${ terraform . workspace } _BlockQueriesAndAllowViewer"
107+
108+ query_strings_config {
109+ query_string_behavior = " whitelist"
110+ query_strings {
111+ items = [
112+ " X-Amz-Algorithm" ,
113+ " X-Amz-Credential" ,
114+ " X-Amz-Date" ,
115+ " X-Amz-Expires" ,
116+ " X-Amz-SignedHeaders" ,
117+ " X-Amz-Signature" ,
118+ " X-Amz-Security-Token"
119+ ]
120+ }
121+ }
122+
123+ headers_config {
124+ header_behavior = " whitelist"
125+ headers {
126+ items = [
127+ " Host" ,
128+ " CloudFront-Viewer-Country" ,
129+ " X-Forwarded-For"
130+ ]
131+ }
132+ }
133+
134+ cookies_config {
135+ cookie_behavior = " none"
136+ }
137+ }
138+
139+ resource "aws_cloudfront_cache_policy" "nocache" {
140+ name = " ${ terraform . workspace } _nocache_policy"
141+ default_ttl = 0
142+ max_ttl = 0
143+ min_ttl = 0
144+
145+ parameters_in_cache_key_and_forwarded_to_origin {
146+ cookies_config {
147+ cookie_behavior = " none"
148+ }
149+ headers_config {
150+ header_behavior = " none"
151+ }
152+ query_strings_config {
153+ query_string_behavior = " none"
154+ }
155+ }
156+ }
0 commit comments