Skip to content

Commit

Permalink
Add workaround to remove trailing slash in S3 redirect (#10)
Browse files Browse the repository at this point in the history
* Add a workaround to remove trailing slash
  • Loading branch information
mubarak-j authored Jul 11, 2024
1 parent 3554980 commit 992cd4c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.idea
.vscode
.vscode

.terraform.lock.hcl
.terraform/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### `1.2.8`

- Add variable `remove_trailing_slash` to allow removing trailing slash automatically added by S3 to the target URL.

### `1.2.7`

- specify minimum SSL protocol as `TLSv1.2_2021`
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,26 @@ No modules.
| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_tags"></a> [tags](#input\_tags) | n/a | `map(string)` | n/a | yes |
| <a name="input_target_url"></a> [target\_url](#input\_target\_url) | URL to redirect to | `string` | n/a | yes |
| <a name="input_zone"></a> [zone](#input\_zone) | Route53 zone name | `string` | n/a | yes |
| <a name="input_allow_overwrite"></a> [allow\_overwrite](#input\_allow\_overwrite) | Allow route53 to overwrite the current rule | `bool` | `false` | no |
| <a name="input_remove_trailing_slash"></a> [remove\_trailing\_slash](#input\_remove\_trailing\_slash) | Remove trailing slash automatically added by S3 to the target URL. Conflicts with target\_url. | `map(string)` | `{}` | no |
| <a name="input_source_subdomain"></a> [source\_subdomain](#input\_source\_subdomain) | FQDN of subdomain that we want to redirect from. | `string` | `""` | no |
| <a name="input_target_url"></a> [target\_url](#input\_target\_url) | URL to redirect to | `string` | `null` | no |

## Outputs

No outputs.

## Changelog

### `1.2.8`

- Add variable `remove_trailing_slash` to allow removing trailing slash automatically added by S3 to the target URL.

### `1.2.7`

- specify minimum SSL protocol as `TLSv1.2_2021`

### `1.2.6`

- ignore `web_acl_id` in Cloudfront
Expand Down
25 changes: 23 additions & 2 deletions s3.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,28 @@ resource "aws_s3_bucket_acl" "redirect_bucket" {

resource "aws_s3_bucket_website_configuration" "redirect_bucket" {
bucket = aws_s3_bucket.redirect_bucket.id
redirect_all_requests_to {
host_name = var.target_url
dynamic "redirect_all_requests_to" {
for_each = var.target_url != null ? [1] : []
content {
host_name = var.target_url
}
}

dynamic "index_document" {
for_each = length(keys(var.remove_trailing_slash)) > 0 ? [1] : []
content {
suffix = "index.html"
}
}

dynamic "routing_rule" {
for_each = length(keys(var.remove_trailing_slash)) > 0 ? [1] : []
content {
redirect {
host_name = try(var.remove_trailing_slash.host_name, null)
replace_key_with = try(var.remove_trailing_slash.replace_key_with, null)
}
}

}
}
7 changes: 7 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ variable "zone" {
variable "target_url" {
description = "URL to redirect to"
type = string
default = null
}

variable "source_subdomain" {
Expand All @@ -20,6 +21,12 @@ variable "allow_overwrite" {
type = bool
}

variable "remove_trailing_slash" {
description = "Remove trailing slash automatically added by S3 to the target URL. Conflicts with target_url."
type = map(string)
default = {}
}

variable "tags" {
type = map(string)
}

0 comments on commit 992cd4c

Please sign in to comment.