diff --git a/terraform/cloudfront.tf b/terraform/cloudfront.tf index 0bf266f..7c112a7 100644 --- a/terraform/cloudfront.tf +++ b/terraform/cloudfront.tf @@ -25,6 +25,11 @@ resource "aws_cloudfront_distribution" "www" { cached_methods = ["GET", "HEAD"] target_origin_id = aws_s3_bucket.www.bucket_regional_domain_name + function_association { + event_type = "viewer-request" + function_arn = aws_cloudfront_function.index_rewrite.arn + } + forwarded_values { query_string = true @@ -90,4 +95,18 @@ data "aws_iam_policy_document" "cloudfront_oac_access" { values = [aws_cloudfront_distribution.www.arn] } } -} \ No newline at end of file +} + + +resource "aws_cloudfront_function" "index_rewrite" { + name = "stumblefunk-org-uk-index-rewrite-${var.environment}" + runtime = "cloudfront-js-2.0" + comment = "Index.html rewrite" + publish = true + code = file("${path.module}/src/index_rewrite.js") + + lifecycle { + create_before_destroy = true + } +} + diff --git a/terraform/lambda.tf b/terraform/lambda.tf index adaf994..1fb8a5d 100644 --- a/terraform/lambda.tf +++ b/terraform/lambda.tf @@ -1,6 +1,6 @@ data "archive_file" "lambda" { type = "zip" - source_file = "lambda/lambda.py" + source_file = "src/lambda.py" output_path = "lambda_function_payload.zip" } diff --git a/terraform/public_html/artist-info/index.html b/terraform/public_html/artist-info/index.html index 38db0c7..d7c7083 100644 --- a/terraform/public_html/artist-info/index.html +++ b/terraform/public_html/artist-info/index.html @@ -1,11 +1,9 @@ - -
-Le fluff diabolique!
- - - + + + + + + +Loading artist-info
+ + \ No newline at end of file diff --git a/terraform/src/index_rewrite.js b/terraform/src/index_rewrite.js new file mode 100644 index 0000000..73a766b --- /dev/null +++ b/terraform/src/index_rewrite.js @@ -0,0 +1,22 @@ +function handler(event) { + var request = event.request; + var uri = request.uri; + + // Check whether the URI is missing a file name. + if (uri.endsWith('/')) { + request.uri += 'index.html'; + } + // Check whether the URI is missing a file extension. + else if (!uri.includes('.')) { + var response = { + statusCode: 302, + statusDescription: 'Moved Permanently', + headers: { + 'location': { value: uri + '/' } + } + }; + return response; + } + + return request; +} \ No newline at end of file diff --git a/terraform/lambda/lambda.py b/terraform/src/lambda.py similarity index 100% rename from terraform/lambda/lambda.py rename to terraform/src/lambda.py diff --git a/terraform/lambda/tests.py b/terraform/src/tests.py similarity index 100% rename from terraform/lambda/tests.py rename to terraform/src/tests.py