-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusertest.tf
More file actions
162 lines (140 loc) · 4.27 KB
/
usertest.tf
File metadata and controls
162 lines (140 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# Purpose: Run moderated tests
# URL: usertest.phlask.me
# Firebase Name: phlask-usertest
data "aws_cloudfront_cache_policy" "caching_disabled" {
name = "Managed-CachingDisabled"
}
module "usertest_site" {
source = "./modules/phlask-baseline-resources"
env_name = "usertest"
default_cache_behavior = {
default_ttl = 300
max_ttl = 600
function_association = [
{
event_type = "viewer-request"
function_arn = aws_cloudfront_function.react-url-rewrite.arn
}
]
}
ordered_cache_behavior = [
{
path_pattern = "reset-data"
allowed_methods = ["GET", "HEAD"]
cache_policy_id = data.aws_cloudfront_cache_policy.caching_disabled.id
lambda_function_association = [
{
event_type = "viewer-request"
include_body = false
lambda_arn = aws_lambda_function.usertest_data_reset.qualified_arn
}
]
},
{
path_pattern = "submit-image"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
cache_policy_id = data.aws_cloudfront_cache_policy.caching_optimized.id
lambda_function_association = [
{
event_type = "viewer-request"
include_body = false
lambda_arn = aws_lambda_function.image_submission_handler.qualified_arn
}
]
},
{
path_pattern = "/images/*"
allowed_methods = ["GET","HEAD"]
cached_methods = ["GET","HEAD"]
cache_policy_id = data.aws_cloudfront_cache_policy.caching_optimized.id
target_origin_id = local.images_origin_id
function_association = [
{
event_type = "viewer-request"
function_arn = aws_cloudfront_function.image-path-cleanup.arn
}
]
}
]
common_domain = var.common_domain
origin_access_control_id_images = aws_cloudfront_origin_access_control.images.id
phlask_images_bucket_name = aws_s3_bucket.images.id
phlask_logs_bucket_name = aws_s3_bucket.logs.id
providers = {
aws.us-east-1 = aws.us-east-1
}
}
# Mechanism to reset the usertest site DB between tests
# lambda function
# extra behavior to invoke the function, no caching
resource "aws_iam_role" "usertest_data_reset" {
name = "usertest-data-reset"
path = "/service-role/"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": [
"lambda.amazonaws.com",
"edgelambda.amazonaws.com"
]
},
"Effect": "Allow"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "usertest_data_reset" {
role = aws_iam_role.usertest_data_reset.name
policy_arn = data.aws_iam_policy.AWSLambdaBasicExecutionRole.arn
}
resource "aws_iam_role_policy" "usertest_data_reset" {
name = "parameter_access"
role = aws_iam_role.usertest_data_reset.id
# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"kms:Decrypt"
]
Effect = "Allow"
Resource = "arn:aws:kms:us-east-1:${data.aws_caller_identity.current.account_id}:key/5a1de7cb-a906-45ae-bf2f-e8a105ccae19"
},
{
Action = [
"ssm:GetParameter"
]
Effect = "Allow"
Resource = "arn:aws:ssm:us-east-1:${data.aws_caller_identity.current.account_id}:parameter/firebase/sdk-credentials"
}
]
})
}
data "archive_file" "usertest_data_reset" {
type = "zip"
source_dir = "${path.module}/src_code/usertest-data-reset"
output_file_mode = "0666"
output_path = "${path.module}/local_output/usertest-data-reset.zip"
}
resource "aws_lambda_function" "usertest_data_reset" {
provider = aws.us-east-1
filename = data.archive_file.usertest_data_reset.output_path
function_name = "usertest-data-reset"
role = aws_iam_role.usertest_data_reset.arn
handler = "lambda_function.lambda_handler"
publish = true
source_code_hash = filebase64sha256(data.archive_file.usertest_data_reset.output_path)
runtime = "python3.12"
timeout = 5
tags = {
Terraform = true
}
}