Skip to content

Commit 35b37b1

Browse files
authored
Replaces Lambda@Edge with CloudFront Function (#62)
Replaces Lambda@Edge with CloudFront Function (#62) Co-authored-by: Kurt Gardiner
1 parent d9302fb commit 35b37b1

File tree

6 files changed

+68
-100
lines changed

6 files changed

+68
-100
lines changed

modules/cloudfront/distribution.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ resource "aws_cloudfront_distribution" "wordpress_distribution" {
6161
}
6262
}
6363

64-
lambda_function_association {
65-
event_type = "origin-request"
66-
lambda_arn = "${aws_lambda_function.object_redirect.arn}:${aws_lambda_function.object_redirect.version}"
64+
function_association {
65+
event_type = "viewer-request"
66+
function_arn = aws_cloudfront_function.object_rewrite.arn
6767
}
6868

6969
viewer_protocol_policy = "redirect-to-https"
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function handler(event) {
2+
var request = event.request;
3+
var uri = request.uri;
4+
5+
try {
6+
%{ for match, target in REDIRECTS }
7+
if (/${match}/.test(uri)) {
8+
return permanentRedirect(/${match}/, '${target}');
9+
}
10+
%{ endfor ~}
11+
12+
// Check whether the URI is missing a file name.
13+
if (uri.endsWith('/')) {
14+
request.uri += 'index.html';
15+
return request;
16+
}
17+
}
18+
catch (e) {
19+
// console.error is not supported
20+
console.log(e);
21+
}
22+
23+
return request;
24+
}
25+
26+
function permanentRedirect(match, target) {
27+
return {
28+
statusCode: 301,
29+
statusDescription: 'Moved Permanently',
30+
headers: {
31+
'location': { value: uri.replace(match, target) }
32+
}
33+
};
34+
}

modules/cloudfront/lambda_redirect/index_html/index.js

Lines changed: 0 additions & 30 deletions
This file was deleted.

modules/cloudfront/main.tf

Lines changed: 15 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,24 @@
1-
data "archive_file" "index_html" {
2-
type = "zip"
3-
source_dir = "${path.module}/lambda_redirect/index_html"
4-
output_path = "${path.module}/lambda_redirect/dst/index_html.zip"
5-
}
6-
7-
#tfsec:ignore:AWS089
8-
resource "aws_cloudwatch_log_group" "object_redirect" {
9-
name = "/aws/lambda/${var.site_name}_redirect_index_html"
10-
retention_in_days = 7
11-
}
12-
13-
#tfsec:ignore:AWS089
14-
resource "aws_cloudwatch_log_group" "object_redirect_ue1_local" {
15-
name = "/aws/lambda/us-east-1.${var.site_name}_redirect_index_html"
16-
retention_in_days = 7
17-
}
18-
19-
# TODO: A solution to create/manage default log groups in all Edge Cache Regions
201
#tfsec:ignore:AWS089
21-
resource "aws_cloudwatch_log_group" "object_redirect_ue1" {
22-
name = "/aws/lambda/us-east-1.${var.site_name}_redirect_index_html"
2+
resource "aws_cloudwatch_log_group" "object_rewrite" {
3+
name = "/aws/cloudfront/function/${var.site_name}_rewrite"
234
retention_in_days = 7
5+
# CloudFront Functions always creates log streams in us-east-1, no matter which edge location ran the function.
6+
# The purpose of this resource is to set the retention days.
247
provider = aws.ue1
258
}
269

27-
resource "aws_lambda_function" "object_redirect" {
28-
provider = aws.ue1
29-
filename = data.archive_file.index_html.output_path
30-
function_name = "${var.site_name}_redirect_index_html"
31-
role = aws_iam_role.lambda-edge.arn
32-
handler = "index.handler"
33-
source_code_hash = data.archive_file.index_html.output_base64sha256
34-
runtime = "nodejs12.x"
35-
publish = true
36-
memory_size = 128
37-
timeout = 3
10+
resource "aws_cloudfront_function" "object_rewrite" {
3811
depends_on = [
39-
aws_cloudwatch_log_group.object_redirect,
40-
aws_cloudwatch_log_group.object_redirect_ue1,
41-
aws_cloudwatch_log_group.object_redirect_ue1_local
12+
aws_cloudwatch_log_group.object_rewrite
4213
]
43-
}
44-
45-
data "aws_iam_policy_document" "lambda-edge-service-role" {
46-
statement {
47-
actions = ["sts:AssumeRole"]
48-
principals {
49-
type = "Service"
50-
identifiers = ["edgelambda.amazonaws.com", "lambda.amazonaws.com"]
14+
15+
name = "${var.site_name}_rewrite"
16+
runtime = "cloudfront-js-1.0"
17+
publish = true
18+
code = templatefile(
19+
"${path.module}/function_rewrite/index.js.tftpl",
20+
{
21+
REDIRECTS = var.cloudfront_function_301_redirects
5122
}
52-
}
53-
}
54-
55-
resource "aws_iam_role" "lambda-edge" {
56-
name = "${var.site_name}-lambda-edge-service-role"
57-
assume_role_policy = data.aws_iam_policy_document.lambda-edge-service-role.json
58-
}
59-
60-
resource "aws_iam_role_policy_attachment" "basic" {
61-
role = aws_iam_role.lambda-edge.name
62-
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
63-
}
64-
65-
data "aws_iam_policy_document" "lambda-edge-cloudwatch-logs" {
66-
statement {
67-
actions = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"]
68-
resources = ["arn:aws:logs:*:*:*"]
69-
}
70-
}
71-
72-
resource "aws_iam_role_policy" "lambda-edge-cloudwatch-logs" {
73-
name = "${var.site_name}-lambda-edge-cloudwatch-logs"
74-
role = aws_iam_role.lambda-edge.name
75-
policy = data.aws_iam_policy_document.lambda-edge-cloudwatch-logs.json
23+
)
7624
}

modules/cloudfront/variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,11 @@ variable "waf_acl_arn" {
3636
default = null
3737
description = "The ARN of the WAF ACL applied to the CloudFront distribution."
3838
}
39+
40+
variable "cloudfront_function_301_redirects" {
41+
type = map
42+
default = {
43+
"^(.*)index\\.php$": "$1"
44+
}
45+
description = "A list of key value pairs of Regex match and destination for 301 redirects at CloudFront."
46+
}

variables.tf

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ variable "cloudfront_class" {
104104
default = "PriceClass_All"
105105
}
106106

107+
variable "cloudfront_function_301_redirects" {
108+
type = map
109+
default = {
110+
"^(.*)index\\.php$": "$1"
111+
}
112+
description = "A list of key value pairs of Regex match and destination for 301 redirects at CloudFront."
113+
}
114+
107115
variable "hosted_zone_id" {
108116
type = string
109117
description = "The Route53 HostedZone ID to use to create records in."

0 commit comments

Comments
 (0)