forked from anonfriese/terraform-aws-es-cleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda.tf
64 lines (54 loc) · 1.76 KB
/
lambda.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
data "http" "src" {
url = "https://raw.githubusercontent.com/cloudreach/aws-lambda-es-cleanup/master/es_cleanup.py"
}
resource "local_file" "src_local" {
content = "${data.http.src.body}"
filename = "${path.module}/es-cleanup.py"
}
data "archive_file" "es_cleanup_lambda" {
type = "zip"
source_file = "${path.module}/es-cleanup.py"
output_path = "${path.module}/es-cleanup.zip"
depends_on = [
local_file.src_local,
]
}
locals {
sg_ids = [element(concat(aws_security_group.lambda.*.id, [""]), 0)]
}
data "null_data_source" "lambda_file" {
inputs = {
filename = "${path.module}/es-cleanup.zip"
}
}
resource "aws_lambda_function" "es_cleanup" {
filename = data.null_data_source.lambda_file.outputs.filename
function_name = "${var.prefix}es-cleanup${var.suffix}"
description = "${var.prefix}es-cleanup${var.suffix}"
timeout = 300
runtime = "python${var.python_version}"
role = aws_iam_role.role.arn
handler = "es-cleanup.lambda_handler"
source_code_hash = data.archive_file.es_cleanup_lambda.output_base64sha256
environment {
variables = {
es_endpoint = var.es_endpoint
index = var.index
skip_index = var.skip_index
delete_after = var.delete_after
index_format = var.index_format
}
}
tags = merge(
var.tags,
{
"Scope" = "${var.prefix}lambda_function_to_elasticsearch${var.suffix}"
},
)
# This will be a code block with empty lists if we don't create a securitygroup and the subnet_ids are empty.
# When these lists are empty it will deploy the lambda without VPC support.
vpc_config {
subnet_ids = var.subnet_ids
security_group_ids = compact(concat(local.sg_ids, var.security_group_ids))
}
}