-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.tf
123 lines (100 loc) · 3.48 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
locals {
policy_lambda_vpc = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
policy_lambda_basic = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
policy_xray = "arn:aws:iam::aws:policy/AWSXrayWriteOnlyAccess"
}
data "aws_iam_policy_document" "this" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "archive_file" "function_zip" {
count = var.package_type == "Zip" ? 1 : 0
type = "zip"
source_dir = format("%s/function", var.code_directory)
output_path = format("%s/function.zip", var.code_directory)
}
data "archive_file" "layer_zip" {
count = var.layer_enabled ? 1 : 0
type = "zip"
source_dir = format("%s/layer", var.code_directory)
output_path = format("%s/layer.zip", var.code_directory)
}
resource "aws_iam_role" "this" {
name = var.function_name
description = var.description
assume_role_policy = data.aws_iam_policy_document.this.json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "lambda" {
role = aws_iam_role.this.name
policy_arn = length(var.subnet_ids) > 0 ? local.policy_lambda_vpc : local.policy_lambda_basic
}
resource "aws_iam_role_policy_attachment" "xray" {
count = var.tracing_mode == "Active" ? 1 : 0
role = aws_iam_role.this.name
policy_arn = local.policy_xray
}
resource "aws_cloudwatch_event_rule" "this" {
name = var.function_name
description = var.description
schedule_expression = var.cron
tags = var.tags
}
resource "aws_cloudwatch_event_target" "this" {
rule = aws_cloudwatch_event_rule.this.name
arn = aws_lambda_function.this.arn
input = var.input
}
resource "aws_cloudwatch_log_group" "this" {
name = "/aws/lambda/${var.function_name}"
retention_in_days = 1
tags = var.tags
}
resource "aws_lambda_permission" "this" {
function_name = aws_lambda_function.this.function_name
principal = "events.amazonaws.com"
action = "lambda:InvokeFunction"
source_arn = aws_cloudwatch_event_rule.this.arn
}
resource "aws_lambda_layer_version" "this" {
count = var.layer_enabled ? 1 : 0
layer_name = var.function_name
filename = data.archive_file.layer_zip[0].output_path
source_code_hash = data.archive_file.layer_zip[0].output_base64sha256
description = var.description
compatible_runtimes = [var.runtime]
}
resource "aws_lambda_function" "this" {
function_name = var.function_name
package_type = var.package_type
image_uri = var.package_type == "Image" ? var.image_uri : null
filename = var.package_type == "Zip" ? data.archive_file.function_zip[0].output_path : null
source_code_hash = var.package_type == "Zip" ? data.archive_file.function_zip[0].output_base64sha256 : null
layers = var.layer_enabled ? [aws_lambda_layer_version.this[0].arn] : null
description = var.description
handler = var.handler
runtime = var.runtime
memory_size = var.memory_size
timeout = var.timeout
role = aws_iam_role.this.arn
tags = var.tags
dynamic "environment" {
for_each = var.vars[*]
content {
variables = environment.value
}
}
tracing_config {
mode = var.tracing_mode
}
vpc_config {
subnet_ids = var.subnet_ids
security_group_ids = var.security_group_ids
}
}