-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
128 lines (112 loc) · 4.18 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
data "aws_iam_policy_document" "assume_role" {
count = var.create_role ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
dynamic "statement" {
for_each = length(var.additional_trust_roles) > 0 ? var.additional_trust_roles : []
content {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "AWS"
identifiers = [statement.value]
}
}
}
}
data "aws_iam_policy_document" "combined-assume-role-policy" {
count = var.create_role ? 1 : 0
source_policy_documents = concat([data.aws_iam_policy_document.assume_role[0].json], var.additional_trust_statements)
}
resource "aws_iam_role" "this" {
count = var.create_role ? 1 : 0
assume_role_policy = data.aws_iam_policy_document.combined-assume-role-policy[0].json
name = coalesce(var.role_name, var.function_name)
tags = var.tags
}
resource "aws_iam_policy" "policy_from_json" {
count = var.create_role && var.policy_json_attached ? 1 : 0
name = coalesce(var.policy_name, var.role_name, var.function_name)
policy = var.policy_json
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "policy_from_json" {
count = var.create_role && var.policy_json_attached ? 1 : 0
role = aws_iam_role.this[0].name
policy_arn = aws_iam_policy.policy_from_json[0].arn
}
resource "aws_iam_role_policy_attachment" "policy_arns" {
count = var.create_role && length(var.policy_arns) > 0 ? length(var.policy_arns) : 0
role = aws_iam_role.this[0].name
policy_arn = var.policy_arns[count.index]
}
resource "aws_lambda_function" "this" {
#checkov:skip=CKV_AWS_50: "X-ray tracing is not required"
#checkov:skip=CKV_AWS_116
#checkov:skip=CKV_AWS_117
#checkov:skip=CKV_AWS_272 "Code signing not required"
#checkov:skip=CKV_AWS_173 "These lambda envvars aren't sensitive and don't need a cmk. Default AWS KMS key is sufficient"
function_name = var.function_name
description = var.description
reserved_concurrent_executions = var.reserved_concurrent_executions
image_uri = var.image_uri
filename = var.filename
handler = var.handler
runtime = var.runtime
source_code_hash = var.source_code_hash
package_type = var.package_type
role = var.create_role ? aws_iam_role.this[0].arn : var.lambda_role
timeout = var.timeout
memory_size = var.memory_size
dynamic "tracing_config" {
for_each = var.tracing_mode != null ? [1] : []
content {
mode = var.tracing_mode
}
}
dynamic "environment" {
for_each = length(keys(var.environment_variables)) > 0 ? [1] : []
content {
variables = var.environment_variables
}
}
dynamic "vpc_config" {
for_each = var.vpc_subnet_ids != null && var.vpc_security_group_ids != null ? [true] : []
content {
security_group_ids = var.vpc_security_group_ids
subnet_ids = var.vpc_subnet_ids
}
}
}
resource "aws_lambda_permission" "allowed_triggers" {
for_each = { for k, v in var.allowed_triggers : k => v }
function_name = aws_lambda_function.this.function_name
statement_id = each.key
action = try(each.value.action, "lambda:InvokeFunction")
principal = try(each.value.principal, format("%s.amazonaws.com", try(each.value.service, "")))
source_arn = try(each.value.source_arn, null)
}
resource "aws_lambda_function_event_invoke_config" "this" {
count = var.sns_topic_on_success == "" && var.sns_topic_on_failure == "" ? 0 : 1
function_name = aws_lambda_function.this.function_name
destination_config {
dynamic "on_failure" {
for_each = var.sns_topic_on_failure != "" ? [1] : []
content {
destination = var.sns_topic_on_failure
}
}
dynamic "on_success" {
for_each = var.sns_topic_on_success != "" ? [1] : []
content {
destination = var.sns_topic_on_success
}
}
}
}