-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
160 lines (139 loc) · 4.43 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
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
locals {
lambda_name = "${var.prefix}${var.name}${var.suffix}"
bundled_source = var.type == "go" ? "${var.bundle.source_folder}/bootstrap" : var.bundle.source_folder
empty_source = "${path.module}/README.md"
source = var.bundle.enabled ? local.bundled_source : local.empty_source
inVpc = var.vpc != null ? 1 : 0
latest_runtimes = {
"go" : "provided.al2",
"node" : "nodejs16.x",
"python" : "python3.9",
"ruby" : "ruby3.2",
"java" : "java11",
".net" : "dotnet6",
"custom" : "provided.al2"
}
runtime = local.latest_runtimes[var.type]
handlers = {
"go" : "bootstrap"
"node" : "index.handler"
"custom" : "bootstrap"
}
handler = var.handler != null ? var.handler : local.handlers[var.type]
architecture = var.type == "go" ? ["x86_64"] : ["arm64"]
}
data "archive_file" "code" {
type = "zip"
source_file = var.type == "go" ? local.source : null
source_dir = var.type == "go" ? null : local.source
output_path = "${var.name}.zip"
}
data "aws_iam_policy_document" "lambda_assume_role_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "lambda_policies" {
count = length(var.inline_policies) == 0 ? 0 : 1
override_policy_documents = var.inline_policies
}
resource "aws_iam_role" "lambda" {
name = "${local.lambda_name}-role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
}
resource "aws_iam_role_policy_attachment" "lambda_policies" {
for_each = var.managed_policies
policy_arn = each.value
role = aws_iam_role.lambda.name
}
resource "aws_iam_role_policy" "lambda_policies" {
count = length(var.inline_policies) == 0 ? 0 : 1
name = "${local.lambda_name}-policies"
role = aws_iam_role.lambda.id
policy = data.aws_iam_policy_document.lambda_policies[0].json
}
resource "aws_lambda_function" "lambda" {
count = var.bundle.enabled ? 1 : 0
description = var.description
function_name = local.lambda_name
runtime = local.runtime
memory_size = var.memory
filename = data.archive_file.code.output_path
source_code_hash = data.archive_file.code.output_base64sha256
handler = local.handler
timeout = var.timeout
role = aws_iam_role.lambda.arn
architectures = local.architecture
dynamic "vpc_config" {
for_each = range(local.inVpc)
content {
subnet_ids = var.vpc.subnet_ids
security_group_ids = var.vpc.security_group_ids
}
}
dynamic "environment" {
for_each = length(var.environment_vars) > 0 ? [1] : []
content {
variables = var.environment_vars
}
}
depends_on = [
data.archive_file.code,
aws_iam_role_policy_attachment.lambda_logs,
aws_cloudwatch_log_group.lambda,
]
}
resource "aws_lambda_function" "empty_lambda" {
count = var.bundle.enabled ? 0 : 1
description = var.description
function_name = local.lambda_name
runtime = local.runtime
memory_size = var.memory
filename = data.archive_file.code.output_path
source_code_hash = data.archive_file.code.output_base64sha256
handler = local.handler
timeout = var.timeout
role = aws_iam_role.lambda.arn
dynamic "vpc_config" {
for_each = range(local.inVpc)
content {
subnet_ids = var.vpc.subnet_ids
security_group_ids = var.vpc.security_group_ids
}
}
dynamic "environment" {
for_each = length(var.environment_vars) > 0 ? [1] : []
content {
variables = var.environment_vars
}
}
depends_on = [
data.archive_file.code,
aws_iam_role_policy_attachment.lambda_logs,
aws_cloudwatch_log_group.lambda,
]
lifecycle {
ignore_changes = [
filename,
source_code_hash]
}
}
resource "aws_cloudwatch_log_group" "lambda" {
count = var.logs.enabled ? 1 : 0
name = "/aws/lambda/${local.lambda_name}"
retention_in_days = var.logs.retention
}
data "aws_iam_policy" "logging_policy" {
count = var.logs.enabled ? 1 : 0
name = "AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "lambda_logs" {
count = var.logs.enabled ? 1 : 0
policy_arn = data.aws_iam_policy.logging_policy[0].arn
role = aws_iam_role.lambda.name
}