Newer
Older
"auth",
"elasticsearch",
"kibana",
function_names = [for name in local.functions : "${local.function_name_prefix}-${replace(name, "_", "")}"]
}
data "aws_iam_policy_document" "lambda_assume_policy" {
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
locals {
lambda_assume_policy_doc = data.aws_iam_policy_document.lambda_assume_policy.json
}
resource "aws_iam_role" "lambda" {
assume_role_policy = local.lambda_assume_policy_doc
}
locals {
lambda_role_arn = aws_iam_role.lambda.arn
lambda_role_name = aws_iam_role.lambda.name
}
output "lambda_role_arn" {
description = "ARN of the Lambda function IAM role."
value = local.lambda_role_arn
}
output "lambda_role_name" {
description = "Name of the Lambda function IAM role."
value = local.lambda_role_name
}
locals {
function_role_policies = [
local.publish_policy_arn,
#local.log_policy_arn,
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole",
]
}
resource "aws_iam_role_policy_attachment" "function" {
count = length(local.function_role_policies)
role = local.lambda_role_name
policy_arn = local.function_role_policies[count.index]
}
variable "runtime" {
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
description = "The Lambda function runtime."
type = string
}
output "runtime" {
description = "The Lambda function runtime."
value = var.runtime
}
variable "memory_size" {
default = 128
description = "Amount of memory in MB allocated to the function invocation."
type = number
}
output "memory_size" {
description = "Amount of memory in MB allocated to the function invocation."
value = var.memory_size
}
variable "timeout" {
default = 30
description = "Amouyynt of time in seconds the function has to run."
type = number
}
output "timeout" {
description = "Amount of memory in MB allocated to the function invocation."
value = var.timeout
}
resource "aws_lambda_function" "function" {
# checkov:skip=CKV_AWS_50
# checkov:skip=CKV_AWS_116
# checkov:skip=CKV_AWS_117
# checkov:skip=CKV_AWS_173
count = length(local.functions)
runtime = var.runtime
function_name = local.function_names[count.index]
role = local.lambda_role_arn
source_code_hash = filebase64sha256("payload.zip")
s3_bucket = local.payloads_bucket_name
s3_key = local.payload_object_name
s3_object_version = local.payload_object_version
package_type = "Zip"
handler = "${local.functions[count.index]}.handler"
description = "${local.module} ${local.functions[count.index]} check in ${local.env}."
memory_size = var.memory_size
reserved_concurrent_executions = -1
timeout = var.timeout
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
environment {
variables = {
ENV = local.env
MODULE = local.module
TOPIC_ARN = local.topic_arn
VERSION = local.payload_object_version
}
}
# Create the log group with retention before the function is created.
# Otherwise it's created without retention and need to be imported.
depends_on = [
aws_cloudwatch_log_group.lambda,
]
}
locals {
function_arns = aws_lambda_function.function.*.arn
function_versions = aws_lambda_function.function.*.version
}
output "function_arns" {
description = "ARNs of the Lambda functions."
value = local.function_arns
}
output "function_names" {
description = "Names of the Lambda functions."
value = local.function_names
}
output "function_versions" {
description = "Versions of the Lambda functions."
value = local.function_versions
}
output "function_version_mapping" {
description = "Mapping of the function and version."
value = zipmap(local.functions, local.function_versions)
}
resource "aws_lambda_alias" "function" {
count = length(local.functions)
name = "${local.function_name_prefix}_${local.functions[count.index]}"
function_name = local.function_arns[count.index]
function_version = local.function_versions[count.index]
}
locals {
function_alias_arns = aws_lambda_alias.function.*.arn
function_alias_names = aws_lambda_alias.function.*.name
}
output "function_alias_arns" {
description = "ARNs of the Lambda functions aliases."
value = local.function_alias_arns
}
output "function_alias_names" {
description = "Names of the Lambda functions aliases."
value = local.function_alias_names
}
resource "aws_lambda_permission" "cloudwatch" {
count = length(local.function_arns)
statement_id = "AllowExecutionFromCloudWatch"
action = "lambda:InvokeFunction"
principal = "events.amazonaws.com"
function_name = local.function_names[count.index]
qualifier = local.function_alias_names[count.index]
}