Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CloudFront Function and WP Plugin fixes #63

Merged
merged 6 commits into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .header.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,28 @@ in your module definition.
Gentle reminder that no backup options are currently bundled with this module - the most effective means would be to
generate and retain a backup from within Wordpress for maximum flexibility. We recommend the UpdraftPlus plugin.

## Permanent Redirects

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.

Some aspects that need to be taken into consideration for the match:

* It's a regular expression
* Group replacements are supported
* Runs in a Javascript function, escaping needs to be taken into consideration
* Passed through a TF var, so escaping that needs to be taking into account as well

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.

An example:

```
cloudfront_function_301_redirects = {
# Redirects /travel to /category/travel/
"^\\/(travel)$": "/category/$1/",
}
```

## Troubleshooting

If you experience issues with the publish element of WP2Static, you can retry. It can be more reliable to proceed to
Expand Down
8 changes: 6 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ module "codebuild" {
wordpress_ecr_repository = aws_ecr_repository.serverless_wordpress.name
aws_account_id = var.aws_account_id
container_memory = var.ecs_memory
wp2static_version = var.wp2static_version
wp2static_s3_addon_version = var.wp2static_s3_addon_version
}

module "cloudfront" {
Expand All @@ -29,8 +31,10 @@ module "cloudfront" {
}
depends_on = [aws_acm_certificate_validation.wordpress_site,
module.waf]
cloudfront_class = var.cloudfront_class
waf_acl_arn = var.waf_enabled ? module.waf[0].waf_acl_arn : null

cloudfront_class = var.cloudfront_class
waf_acl_arn = var.waf_enabled ? module.waf[0].waf_acl_arn : null
cloudfront_function_301_redirects = var.cloudfront_function_301_redirects
}

module "waf" {
Expand Down
4 changes: 2 additions & 2 deletions modules/cloudfront/function_rewrite/index.js.tftpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function handler(event) {
try {
%{ for match, target in REDIRECTS }
if (/${match}/.test(uri)) {
return permanentRedirect(/${match}/, '${target}');
return permanentRedirect(uri, /${match}/, '${target}');
}
%{ endfor ~}

Expand All @@ -23,7 +23,7 @@ function handler(event) {
return request;
}

function permanentRedirect(match, target) {
function permanentRedirect(uri, match, target) {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
Expand Down
2 changes: 1 addition & 1 deletion modules/codebuild/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ variable "graviton_codebuild_enabled" {

variable "wp2static_version" {
type = string
description = "Version of WP2Static to use from https://github.com/leonstafford/wp2static/releases"
description = "Version of WP2Static to use from https://github.com/WP2Static/wp2static/releases"
default = "7.1.7"
}

Expand Down
12 changes: 12 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ variable "wordpress_memory_limit" {
default = "256M"
}

variable "wp2static_version" {
type = string
description = "Version of WP2Static to use from https://github.com/WP2Static/wp2static/releases"
default = "7.1.7"
}

variable "wp2static_s3_addon_version" {
type = string
description = "Version of the WP2Static S3 Add-on to use from https://github.com/leonstafford/wp2static-addon-s3/releases/"
default = "1.0"
}

variable "waf_acl_rules" {
type = list(any)
description = "List of WAF rules to apply. Can be customized to apply others created outside of module."
Expand Down