-
Notifications
You must be signed in to change notification settings - Fork 69
Replaces Lambda@Edge with CloudFront Function #62
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
abc903d
Better example objects links and bootstrapping (#45)
krutisfood 5dd07b8
feat(redirect): Refactors Lambda@Edge to CloudFront Function
nvnivs f6e1d52
Better wording
nvnivs fc84b2a
Explicit variable name
nvnivs 7b0e92b
Adds function for permanent redirect
nvnivs 0dc9314
Adds note one why log is being used for errors
nvnivs 9f99c3d
Merge remote-tracking branch 'origin/master' into cf-function
nvnivs 3e989c4
Adds dependency of rewrite function to log_group
nvnivs 2ff73f9
Revert "Merge remote-tracking branch 'origin/master' into cf-function"
nvnivs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function handler(event) { | ||
var request = event.request; | ||
var uri = request.uri; | ||
|
||
try { | ||
%{ for match, target in REDIRECTS } | ||
if (/${match}/.test(uri)) { | ||
return permanentRedirect(/${match}/, '${target}'); | ||
} | ||
%{ endfor ~} | ||
|
||
// Check whether the URI is missing a file name. | ||
if (uri.endsWith('/')) { | ||
request.uri += 'index.html'; | ||
return request; | ||
} | ||
} | ||
catch (e) { | ||
// console.error is not supported | ||
console.log(e); | ||
} | ||
|
||
return request; | ||
} | ||
|
||
function permanentRedirect(match, target) { | ||
return { | ||
statusCode: 301, | ||
statusDescription: 'Moved Permanently', | ||
headers: { | ||
'location': { value: uri.replace(match, target) } | ||
} | ||
}; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,24 @@ | ||
data "archive_file" "index_html" { | ||
type = "zip" | ||
source_dir = "${path.module}/lambda_redirect/index_html" | ||
output_path = "${path.module}/lambda_redirect/dst/index_html.zip" | ||
} | ||
|
||
#tfsec:ignore:AWS089 | ||
resource "aws_cloudwatch_log_group" "object_redirect" { | ||
name = "/aws/lambda/${var.site_name}_redirect_index_html" | ||
retention_in_days = 7 | ||
} | ||
|
||
#tfsec:ignore:AWS089 | ||
resource "aws_cloudwatch_log_group" "object_redirect_ue1_local" { | ||
name = "/aws/lambda/us-east-1.${var.site_name}_redirect_index_html" | ||
retention_in_days = 7 | ||
} | ||
|
||
# TODO: A solution to create/manage default log groups in all Edge Cache Regions | ||
#tfsec:ignore:AWS089 | ||
resource "aws_cloudwatch_log_group" "object_redirect_ue1" { | ||
name = "/aws/lambda/us-east-1.${var.site_name}_redirect_index_html" | ||
resource "aws_cloudwatch_log_group" "object_rewrite" { | ||
name = "/aws/cloudfront/function/${var.site_name}_rewrite" | ||
retention_in_days = 7 | ||
# CloudFront Functions always creates log streams in us-east-1, no matter which edge location ran the function. | ||
# The purpose of this resource is to set the retention days. | ||
provider = aws.ue1 | ||
} | ||
|
||
resource "aws_lambda_function" "object_redirect" { | ||
provider = aws.ue1 | ||
filename = data.archive_file.index_html.output_path | ||
function_name = "${var.site_name}_redirect_index_html" | ||
role = aws_iam_role.lambda-edge.arn | ||
handler = "index.handler" | ||
source_code_hash = data.archive_file.index_html.output_base64sha256 | ||
runtime = "nodejs12.x" | ||
publish = true | ||
memory_size = 128 | ||
timeout = 3 | ||
resource "aws_cloudfront_function" "object_rewrite" { | ||
depends_on = [ | ||
aws_cloudwatch_log_group.object_redirect, | ||
aws_cloudwatch_log_group.object_redirect_ue1, | ||
aws_cloudwatch_log_group.object_redirect_ue1_local | ||
aws_cloudwatch_log_group.object_rewrite | ||
] | ||
} | ||
|
||
data "aws_iam_policy_document" "lambda-edge-service-role" { | ||
statement { | ||
actions = ["sts:AssumeRole"] | ||
principals { | ||
type = "Service" | ||
identifiers = ["edgelambda.amazonaws.com", "lambda.amazonaws.com"] | ||
name = "${var.site_name}_rewrite" | ||
runtime = "cloudfront-js-1.0" | ||
publish = true | ||
code = templatefile( | ||
"${path.module}/function_rewrite/index.js.tftpl", | ||
{ | ||
REDIRECTS = var.cloudfront_function_301_redirects | ||
} | ||
} | ||
} | ||
|
||
resource "aws_iam_role" "lambda-edge" { | ||
name = "${var.site_name}-lambda-edge-service-role" | ||
assume_role_policy = data.aws_iam_policy_document.lambda-edge-service-role.json | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "basic" { | ||
role = aws_iam_role.lambda-edge.name | ||
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" | ||
} | ||
|
||
data "aws_iam_policy_document" "lambda-edge-cloudwatch-logs" { | ||
statement { | ||
actions = ["logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents"] | ||
resources = ["arn:aws:logs:*:*:*"] | ||
} | ||
} | ||
|
||
resource "aws_iam_role_policy" "lambda-edge-cloudwatch-logs" { | ||
name = "${var.site_name}-lambda-edge-cloudwatch-logs" | ||
role = aws_iam_role.lambda-edge.name | ||
policy = data.aws_iam_policy_document.lambda-edge-cloudwatch-logs.json | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.