-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.tf
114 lines (103 loc) · 3.21 KB
/
main.tf
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
module "lambda_proxy_i" {
source = "terraform-aws-modules/lambda/aws"
count = var.num_proxies
function_name = "proxy-${count.index}"
create_package = false
image_uri = module.ecr_proxy_i.image_uri
package_type = "Image"
timeout = 600
publish = true
}
resource "local_file" "proxy_urls" {
content = jsonencode(aws_lambda_function_url.lambda_proxy_i[*].function_url)
filename = "${path.module}/lambda/proxy-urls.json"
}
module "lambda_proxy" {
source = "terraform-aws-modules/lambda/aws"
function_name = "proxy"
create_package = false
image_uri = module.ecr_proxy.image_uri
package_type = "Image"
timeout = 600
publish = true
attach_policy_json = true
policy_json = jsonencode({
Version = "2012-10-17"
Statement = [{
Effect = "Allow"
Action = "lambda:InvokeFunctionUrl"
Resource = aws_lambda_function_url.lambda_proxy_i[*].function_arn
}]
})
}
module "ecr_proxy_i" {
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
create_ecr_repo = true
ecr_repo = "lambda-proxy-i"
source_path = "${path.module}/lambda"
docker_file_path = "Dockerfile-i"
platform = "linux/amd64"
image_tag = sha1(join("", [
filesha1("${path.module}/lambda/package.json"),
filesha1("${path.module}/lambda/proxy-i.js"),
filesha1("${path.module}/lambda/Dockerfile-i"),
]))
ecr_repo_lifecycle_policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep only the last 1 image",
"selection" : {
"tagStatus" : "any",
"countType" : "imageCountMoreThan",
"countNumber" : 1
},
"action" : {
"type" : "expire"
}
}
]
})
}
module "ecr_proxy" {
source = "terraform-aws-modules/lambda/aws//modules/docker-build"
create_ecr_repo = true
ecr_repo = "lambda-proxy"
source_path = "${path.module}/lambda"
docker_file_path = "Dockerfile"
platform = "linux/amd64"
depends_on = [local_file.proxy_urls]
image_tag = sha1(join("", [
filesha1("${path.module}/lambda/package.json"),
filesha1("${path.module}/lambda/proxy.js"),
fileexists("${path.module}/lambda/proxy-urls.json") ? filesha1("${path.module}/lambda/proxy-urls.json") : "",
filesha1("${path.module}/lambda/Dockerfile"),
]))
ecr_repo_lifecycle_policy = jsonencode({
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep only the last 1 image",
"selection" : {
"tagStatus" : "any",
"countType" : "imageCountMoreThan",
"countNumber" : 1
},
"action" : {
"type" : "expire"
}
}
]
})
}
resource "aws_lambda_function_url" "lambda_proxy_i" {
count = var.num_proxies
function_name = module.lambda_proxy_i[count.index].lambda_function_name
authorization_type = "AWS_IAM"
invoke_mode = "RESPONSE_STREAM"
}
resource "aws_lambda_function_url" "lambda_proxy" {
function_name = module.lambda_proxy.lambda_function_name
authorization_type = "NONE"
invoke_mode = "RESPONSE_STREAM"
}