|
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 |
20 | 1 | #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" |
23 | 4 | 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. |
24 | 7 | provider = aws.ue1
|
25 | 8 | }
|
26 | 9 |
|
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" { |
38 | 11 | 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 |
42 | 13 | ]
|
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 |
51 | 22 | }
|
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 | + ) |
76 | 24 | }
|
0 commit comments