Skip to content

Commit 14fbd52

Browse files
authored
Merge pull request #16 from cookielab/cors
feat(cors): Introduce S3 CORS and CF response headers
2 parents a348e2d + 02c8ab1 commit 14fbd52

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ module "static-site" {
109109
| [aws_cloudfront_distribution.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution) | resource |
110110
| [aws_cloudfront_origin_access_control.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_access_control) | resource |
111111
| [aws_cloudfront_origin_access_identity.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_access_identity) | resource |
112+
| [aws_cloudfront_response_headers_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_response_headers_policy) | resource |
112113
| [aws_iam_access_key.deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_access_key) | resource |
113114
| [aws_iam_user.deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
114115
| [aws_iam_user_policy.deploy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_policy) | resource |
@@ -148,8 +149,11 @@ module "static-site" {
148149
| <a name="input_override_status_code_403"></a> [override\_status\_code\_403](#input\_override\_status\_code\_403) | Override status code for 403 error | `number` | `403` | no |
149150
| <a name="input_override_status_code_404"></a> [override\_status\_code\_404](#input\_override\_status\_code\_404) | Override status code for 404 error | `number` | `200` | no |
150151
| <a name="input_proxy_paths"></a> [proxy\_paths](#input\_proxy\_paths) | n/a | <pre>list(object({<br> origin_domain = string<br> path_prefix = string<br> }))</pre> | `[]` | no |
152+
| <a name="input_response_header_access_control_allow_credentials"></a> [response\_header\_access\_control\_allow\_credentials](#input\_response\_header\_access\_control\_allow\_credentials) | n/a | `bool` | `false` | no |
153+
| <a name="input_response_header_origin_override"></a> [response\_header\_origin\_override](#input\_response\_header\_origin\_override) | n/a | `bool` | `false` | no |
151154
| <a name="input_s3_bucket_name"></a> [s3\_bucket\_name](#input\_s3\_bucket\_name) | n/a | `string` | n/a | yes |
152155
| <a name="input_s3_bucket_policy"></a> [s3\_bucket\_policy](#input\_s3\_bucket\_policy) | Additional S3 bucket policy | `string` | `"{}"` | no |
156+
| <a name="input_s3_cors_rule"></a> [s3\_cors\_rule](#input\_s3\_cors\_rule) | List of maps containing rules for Cross-Origin Resource Sharing. | <pre>list(object({<br> allowed_headers = optional(list(string))<br> allowed_methods = optional(list(string))<br> allowed_origins = optional(list(string))<br> expose_headers = optional(list(string))<br> max_age_seconds = optional(number)<br> }))</pre> | `[]` | no |
153157
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `map(string)` | `{}` | no |
154158

155159
## Outputs

main.tf

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ module "s3_bucket" {
190190
}
191191
}
192192

193+
cors_rule = var.s3_cors_rule
194+
193195
tags = local.tags
194196
}
195197

@@ -248,9 +250,10 @@ resource "aws_cloudfront_distribution" "this" {
248250
}
249251

250252
default_cache_behavior {
251-
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
252-
cached_methods = ["GET", "HEAD"]
253-
target_origin_id = var.s3_bucket_name
253+
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
254+
cached_methods = ["GET", "HEAD"]
255+
target_origin_id = var.s3_bucket_name
256+
response_headers_policy_id = length(var.s3_cors_rule) > 0 ? aws_cloudfront_response_headers_policy.this[0].id : null
254257

255258
forwarded_values {
256259
query_string = false
@@ -346,6 +349,30 @@ resource "aws_route53_record" "this" {
346349
}
347350
}
348351

352+
resource "aws_cloudfront_response_headers_policy" "this" {
353+
count = length(var.s3_cors_rule) > 0 ? 1 : 0
354+
name = "${var.s3_bucket_name} - response headers"
355+
comment = "CloudFront response headers policy using S3 CORS rules"
356+
357+
cors_config {
358+
access_control_allow_credentials = var.response_header_access_control_allow_credentials
359+
360+
access_control_allow_headers {
361+
items = var.s3_cors_rule[0].allowed_headers
362+
}
363+
364+
access_control_allow_methods {
365+
items = var.s3_cors_rule[0].allowed_methods
366+
}
367+
368+
access_control_allow_origins {
369+
items = var.s3_cors_rule[0].allowed_origins
370+
}
371+
372+
origin_override = var.response_header_origin_override
373+
}
374+
}
375+
349376
moved {
350377
from = aws_kms_key.this
351378
to = aws_kms_key.this[0]

variables.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,25 @@ variable "aws_env_vars_suffix" {
137137
type = string
138138
default = ""
139139
}
140+
141+
variable "s3_cors_rule" {
142+
description = "List of maps containing rules for Cross-Origin Resource Sharing."
143+
type = list(object({
144+
allowed_headers = optional(list(string))
145+
allowed_methods = optional(list(string))
146+
allowed_origins = optional(list(string))
147+
expose_headers = optional(list(string))
148+
max_age_seconds = optional(number)
149+
}))
150+
default = []
151+
}
152+
153+
variable "response_header_origin_override" {
154+
type = bool
155+
default = false
156+
}
157+
158+
variable "response_header_access_control_allow_credentials" {
159+
type = bool
160+
default = false
161+
}

0 commit comments

Comments
 (0)