Skip to content

Commit 0356439

Browse files
authored
CloudFront Function and WP Plugin fixes (#63)
* Passes cloudfront_function_301_redirects to cloudfront module * Fixes missing uri param * Passes plugin version from top level module * Updates WP2Static url as repo was migrated * Adds some help of permanent redirects * Removed bad paste
1 parent de42674 commit 0356439

File tree

5 files changed

+43
-5
lines changed

5 files changed

+43
-5
lines changed

.header.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,28 @@ in your module definition.
209209
Gentle reminder that no backup options are currently bundled with this module - the most effective means would be to
210210
generate and retain a backup from within Wordpress for maximum flexibility. We recommend the UpdraftPlus plugin.
211211

212+
## Permanent Redirects
213+
214+
Basic url path based permanent redirects are supported via the CloudFront function. The variable `cloudfront_function_301_redirects` can be set with a custom map of match to destination mappings.
215+
216+
Some aspects that need to be taken into consideration for the match:
217+
218+
* It's a regular expression
219+
* Group replacements are supported
220+
* Runs in a Javascript function, escaping needs to be taken into consideration
221+
* Passed through a TF var, so escaping that needs to be taking into account as well
222+
223+
An example to match a path like `/category-name`, a suitable match would be `"^\\/(category-name)$"`. Breaking down the `\\/` part, the first `\` tells TF to escape the second `\`, which is the Regex escape for the `/` character.
224+
225+
An example:
226+
227+
```
228+
cloudfront_function_301_redirects = {
229+
# Redirects /travel to /category/travel/
230+
"^\\/(travel)$": "/category/$1/",
231+
}
232+
```
233+
212234
## Troubleshooting
213235

214236
If you experience issues with the publish element of WP2Static, you can retry. It can be more reliable to proceed to

main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module "codebuild" {
1616
wordpress_ecr_repository = aws_ecr_repository.serverless_wordpress.name
1717
aws_account_id = var.aws_account_id
1818
container_memory = var.ecs_memory
19+
wp2static_version = var.wp2static_version
20+
wp2static_s3_addon_version = var.wp2static_s3_addon_version
1921
}
2022

2123
module "cloudfront" {
@@ -29,8 +31,10 @@ module "cloudfront" {
2931
}
3032
depends_on = [aws_acm_certificate_validation.wordpress_site,
3133
module.waf]
32-
cloudfront_class = var.cloudfront_class
33-
waf_acl_arn = var.waf_enabled ? module.waf[0].waf_acl_arn : null
34+
35+
cloudfront_class = var.cloudfront_class
36+
waf_acl_arn = var.waf_enabled ? module.waf[0].waf_acl_arn : null
37+
cloudfront_function_301_redirects = var.cloudfront_function_301_redirects
3438
}
3539

3640
module "waf" {

modules/cloudfront/function_rewrite/index.js.tftpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function handler(event) {
55
try {
66
%{ for match, target in REDIRECTS }
77
if (/${match}/.test(uri)) {
8-
return permanentRedirect(/${match}/, '${target}');
8+
return permanentRedirect(uri, /${match}/, '${target}');
99
}
1010
%{ endfor ~}
1111

@@ -23,7 +23,7 @@ function handler(event) {
2323
return request;
2424
}
2525

26-
function permanentRedirect(match, target) {
26+
function permanentRedirect(uri, match, target) {
2727
return {
2828
statusCode: 301,
2929
statusDescription: 'Moved Permanently',

modules/codebuild/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ variable "graviton_codebuild_enabled" {
6666

6767
variable "wp2static_version" {
6868
type = string
69-
description = "Version of WP2Static to use from https://github.com/leonstafford/wp2static/releases"
69+
description = "Version of WP2Static to use from https://github.com/WP2Static/wp2static/releases"
7070
default = "7.1.7"
7171
}
7272

variables.tf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,18 @@ variable "wordpress_memory_limit" {
154154
default = "256M"
155155
}
156156

157+
variable "wp2static_version" {
158+
type = string
159+
description = "Version of WP2Static to use from https://github.com/WP2Static/wp2static/releases"
160+
default = "7.1.7"
161+
}
162+
163+
variable "wp2static_s3_addon_version" {
164+
type = string
165+
description = "Version of the WP2Static S3 Add-on to use from https://github.com/leonstafford/wp2static-addon-s3/releases/"
166+
default = "1.0"
167+
}
168+
157169
variable "waf_acl_rules" {
158170
type = list(any)
159171
description = "List of WAF rules to apply. Can be customized to apply others created outside of module."

0 commit comments

Comments
 (0)