forked from moritzzimmer/terraform-aws-lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
159 lines (141 loc) · 5.74 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
data "aws_region" "current" {}
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
locals {
function_arn = "arn:${data.aws_partition.current.partition}:lambda:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:function:${var.function_name}"
handler = var.package_type != "Zip" ? null : var.handler
publish = var.lambda_at_edge || var.snap_start ? true : var.publish
runtime = var.package_type != "Zip" ? null : var.runtime
timeout = var.lambda_at_edge ? min(var.timeout, 5) : var.timeout
}
resource "aws_lambda_function" "lambda" {
count = var.ignore_external_function_updates ? 0 : 1
depends_on = [aws_cloudwatch_log_group.lambda]
architectures = var.architectures
description = var.description
filename = var.filename
function_name = var.function_name
handler = local.handler
image_uri = var.image_uri
kms_key_arn = var.kms_key_arn
layers = var.layers
memory_size = var.memory_size
package_type = var.package_type
publish = local.publish
reserved_concurrent_executions = var.reserved_concurrent_executions
role = aws_iam_role.lambda.arn
runtime = local.runtime
s3_bucket = var.s3_bucket
s3_key = var.s3_key
s3_object_version = var.s3_object_version
source_code_hash = var.source_code_hash
tags = var.tags
timeout = local.timeout
ephemeral_storage {
size = var.ephemeral_storage_size
}
dynamic "environment" {
for_each = var.environment == null ? [] : [var.environment]
content {
variables = environment.value.variables
}
}
dynamic "image_config" {
for_each = length(var.image_config) > 0 ? [true] : []
content {
command = lookup(var.image_config, "command", null)
entry_point = lookup(var.image_config, "entry_point", null)
working_directory = lookup(var.image_config, "working_directory", null)
}
}
dynamic "tracing_config" {
for_each = var.tracing_config_mode == null ? [] : [true]
content {
mode = var.tracing_config_mode
}
}
dynamic "vpc_config" {
for_each = var.vpc_config == null ? [] : [var.vpc_config]
content {
ipv6_allowed_for_dual_stack = vpc_config.value.ipv6_allowed_for_dual_stack
security_group_ids = vpc_config.value.security_group_ids
subnet_ids = vpc_config.value.subnet_ids
}
}
dynamic "snap_start" {
for_each = var.snap_start ? [true] : []
content {
apply_on = "PublishedVersions"
}
}
}
// Copy of the original Lambda resource plus lifecycle configuration ignoring
// external changes executed by CodeDeploy, aws CLI and others.
// We need this copy workaround, since lifecycle configuration must be static,
// see https://github.com/hashicorp/terraform/issues/24188.
resource "aws_lambda_function" "lambda_external_lifecycle" {
count = var.ignore_external_function_updates ? 1 : 0
depends_on = [aws_cloudwatch_log_group.lambda]
architectures = var.architectures
description = var.description
filename = var.filename
function_name = var.function_name
handler = local.handler
image_uri = var.image_uri
kms_key_arn = var.kms_key_arn
layers = var.layers
memory_size = var.memory_size
package_type = var.package_type
publish = local.publish
replace_security_groups_on_destroy = var.replace_security_groups_on_destroy
replacement_security_group_ids = var.replacement_security_group_ids
reserved_concurrent_executions = var.reserved_concurrent_executions
role = aws_iam_role.lambda.arn
runtime = local.runtime
s3_bucket = var.s3_bucket
s3_key = var.s3_key
s3_object_version = var.s3_object_version
source_code_hash = var.source_code_hash
tags = var.tags
timeout = local.timeout
ephemeral_storage {
size = var.ephemeral_storage_size
}
dynamic "environment" {
for_each = var.environment == null ? [] : [var.environment]
content {
variables = environment.value.variables
}
}
dynamic "image_config" {
for_each = length(var.image_config) > 0 ? [true] : []
content {
command = lookup(var.image_config, "command", null)
entry_point = lookup(var.image_config, "entry_point", null)
working_directory = lookup(var.image_config, "working_directory", null)
}
}
dynamic "tracing_config" {
for_each = var.tracing_config_mode == null ? [] : [true]
content {
mode = var.tracing_config_mode
}
}
dynamic "vpc_config" {
for_each = var.vpc_config == null ? [] : [var.vpc_config]
content {
ipv6_allowed_for_dual_stack = vpc_config.value.ipv6_allowed_for_dual_stack
security_group_ids = vpc_config.value.security_group_ids
subnet_ids = vpc_config.value.subnet_ids
}
}
dynamic "snap_start" {
for_each = var.snap_start ? [true] : []
content {
apply_on = "PublishedVersions"
}
}
lifecycle {
ignore_changes = [image_uri, s3_object_version]
}
}