Skip to content

Commit a028ede

Browse files
Merge pull request #12 from TechHoldingLLC/feat/tracing_config
feat: Tracing config, Event source mapping, and Image config
2 parents 028309d + d5965bb commit a028ede

File tree

4 files changed

+157
-2
lines changed

4 files changed

+157
-2
lines changed

EXAMPLE.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,4 +141,22 @@ module "lambda_function" {
141141
}
142142
}
143143
}
144+
```
145+
146+
## Lambda Event Source Mapping
147+
The example below demonstrates how to configure Lambda Event source Mapping
148+
```
149+
module "lambda_function" {
150+
source = "git::https://github.com/TechHoldingLLC/terraform-aws-lambda-function.git?ref=v1.0.11"
151+
152+
# ...omitted for brevity
153+
154+
event_source_mapping = {
155+
event_source_arn = module.sqs.arn
156+
batch_size = 10
157+
scaling_config = {
158+
maximum_concurrency = 10
159+
}
160+
}
161+
}
144162
```

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ No modules.
2424
| [aws_iam_role.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
2525
| [aws_iam_role_policy_attachment.lambda_basic_execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
2626
| [aws_iam_role_policy_attachment.lambda_vpc_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
27+
| [aws_lambda_event_source_mapping.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping) | resource |
2728
| [aws_lambda_function.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
2829
| [aws_lambda_function_url.function_url](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function_url) | resource |
2930
| [aws_lambda_permission.triggers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
@@ -40,10 +41,14 @@ No modules.
4041
| <a name="input_description"></a> [description](#input\_description) | Lambda function description | `any` | `null` | no |
4142
| <a name="input_env_vars_from_parameter_store"></a> [env\_vars\_from\_parameter\_store](#input\_env\_vars\_from\_parameter\_store) | Lambda environment variables from SSM parameter store | `map(any)` | `{}` | no |
4243
| <a name="input_environment_variables"></a> [environment\_variables](#input\_environment\_variables) | Environment Variables for Lambda Functions | `map(any)` | `{}` | no |
44+
| <a name="input_event_source_mapping"></a> [event\_source\_mapping](#input\_event\_source\_mapping) | Map of event source mapping | `any` | `{}` | no |
4345
| <a name="input_function_name"></a> [function\_name](#input\_function\_name) | Lambda function name | `any` | n/a | yes |
4446
| <a name="input_function_url"></a> [function\_url](#input\_function\_url) | Create lambda function url | `bool` | `false` | no |
4547
| <a name="input_function_url_cors"></a> [function\_url\_cors](#input\_function\_url\_cors) | Function url cors | `any` | `{}` | no |
4648
| <a name="input_handler"></a> [handler](#input\_handler) | Name of Handler | `any` | `null` | no |
49+
| <a name="input_image_config_command"></a> [image\_config\_command](#input\_image\_config\_command) | The CMD for the docker image | `list(string)` | `[]` | no |
50+
| <a name="input_image_config_entry_point"></a> [image\_config\_entry\_point](#input\_image\_config\_entry\_point) | The ENTRYPOINT for the docker image | `list(string)` | `[]` | no |
51+
| <a name="input_image_config_working_directory"></a> [image\_config\_working\_directory](#input\_image\_config\_working\_directory) | The working directory for the docker image | `string` | `null` | no |
4752
| <a name="input_image_uri"></a> [image\_uri](#input\_image\_uri) | uri of image | `any` | `null` | no |
4853
| <a name="input_lambda_memory"></a> [lambda\_memory](#input\_lambda\_memory) | Required Memory for Lambda function | `number` | `128` | no |
4954
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | Lambda language | `any` | `null` | no |
@@ -60,6 +65,7 @@ No modules.
6065
| <a name="input_source_file"></a> [source\_file](#input\_source\_file) | Lambda source file | `string` | `""` | no |
6166
| <a name="input_subnets"></a> [subnets](#input\_subnets) | Subnets | `list(any)` | `null` | no |
6267
| <a name="input_tags"></a> [tags](#input\_tags) | Tags | `map` | `{}` | no |
68+
| <a name="input_tracing_mode"></a> [tracing\_mode](#input\_tracing\_mode) | Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active. | `string` | `null` | no |
6369

6470
## Outputs
6571

main.tf

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ resource "aws_lambda_function" "lambda" {
4545
s3_bucket = try(data.aws_s3_object.lambda[0].bucket, null)
4646
s3_key = try(data.aws_s3_object.lambda[0].key, null)
4747
s3_object_version = try(data.aws_s3_object.lambda[0].version_id, null)
48-
source_code_hash = try(data.aws_s3_object.lambda[0].metadata.source_code_hash, null)
48+
source_code_hash = try(data.aws_s3_object.lambda[0].metadata.source_code_hash, data.archive_file.lambda[0].output_base64sha256, null)
4949
filename = try(data.archive_file.lambda[0].output_path, null)
5050
image_uri = var.image_uri
5151
package_type = var.package_type
@@ -67,6 +67,22 @@ resource "aws_lambda_function" "lambda" {
6767
}
6868
}
6969

70+
dynamic "tracing_config" {
71+
for_each = var.tracing_mode == null ? [] : [true]
72+
content {
73+
mode = var.tracing_mode
74+
}
75+
}
76+
77+
dynamic "image_config" {
78+
for_each = length(var.image_config_entry_point) > 0 || length(var.image_config_command) > 0 || var.image_config_working_directory != null ? [true] : []
79+
content {
80+
entry_point = var.image_config_entry_point
81+
command = var.image_config_command
82+
working_directory = var.image_config_working_directory
83+
}
84+
}
85+
7086
tags = var.tags
7187
}
7288

@@ -79,7 +95,7 @@ resource "aws_lambda_permission" "triggers" {
7995

8096
function_name = aws_lambda_function.lambda.function_name
8197

82-
statement_id = try(each.value.statement_id ,format("Allow%sLambdaInvoke", try(each.key, "")))
98+
statement_id = try(each.value.statement_id, format("Allow%sLambdaInvoke", try(each.key, "")))
8399
action = "lambda:InvokeFunction"
84100
principal = try(each.value.principal, format("%s.amazonaws.com", try(each.value.service, "")))
85101
source_arn = try(each.value.source_arn, null)
@@ -119,3 +135,84 @@ resource "aws_lambda_function_url" "function_url" {
119135
}
120136
}
121137
}
138+
139+
# ------------------------------------------------------------------------------
140+
# LAMBDA EVENT SOURCE MAPPING
141+
# ------------------------------------------------------------------------------
142+
resource "aws_lambda_event_source_mapping" "this" {
143+
for_each = { for k, v in var.event_source_mapping : k => v }
144+
145+
function_name = aws_lambda_function.lambda.arn
146+
147+
event_source_arn = try(each.value.event_source_arn, null)
148+
149+
batch_size = try(each.value.batch_size, null)
150+
maximum_batching_window_in_seconds = try(each.value.maximum_batching_window_in_seconds, null)
151+
enabled = try(each.value.enabled, true)
152+
starting_position = try(each.value.starting_position, null)
153+
starting_position_timestamp = try(each.value.starting_position_timestamp, null)
154+
parallelization_factor = try(each.value.parallelization_factor, null)
155+
maximum_retry_attempts = try(each.value.maximum_retry_attempts, null)
156+
maximum_record_age_in_seconds = try(each.value.maximum_record_age_in_seconds, null)
157+
bisect_batch_on_function_error = try(each.value.bisect_batch_on_function_error, null)
158+
function_response_types = try(each.value.function_response_types, null)
159+
tumbling_window_in_seconds = try(each.value.tumbling_window_in_seconds, null)
160+
161+
dynamic "destination_config" {
162+
for_each = try(each.value.destination_arn_on_failure, null) != null ? [true] : []
163+
content {
164+
on_failure {
165+
destination_arn = each.value["destination_arn_on_failure"]
166+
}
167+
}
168+
}
169+
170+
dynamic "scaling_config" {
171+
for_each = try([each.value.scaling_config], [])
172+
content {
173+
maximum_concurrency = try(scaling_config.value.maximum_concurrency, null)
174+
}
175+
}
176+
177+
dynamic "filter_criteria" {
178+
for_each = try(each.value.filter_criteria, null) != null ? [true] : []
179+
180+
content {
181+
dynamic "filter" {
182+
for_each = try(flatten([each.value.filter_criteria]), [])
183+
184+
content {
185+
pattern = try(filter.value.pattern, null)
186+
}
187+
}
188+
}
189+
}
190+
191+
dynamic "document_db_event_source_config" {
192+
for_each = try(each.value.document_db_event_source_config, [])
193+
194+
content {
195+
database_name = document_db_event_source_config.value.database_name
196+
collection_name = try(document_db_event_source_config.value.collection_name, null)
197+
full_document = try(document_db_event_source_config.value.full_document, null)
198+
}
199+
}
200+
201+
dynamic "metrics_config" {
202+
for_each = try([each.value.metrics_config], [])
203+
204+
content {
205+
metrics = metrics_config.value.metrics
206+
}
207+
}
208+
209+
dynamic "provisioned_poller_config" {
210+
for_each = try([each.value.provisioned_poller_config], [])
211+
content {
212+
maximum_pollers = try(provisioned_poller_config.value.maximum_pollers, null)
213+
minimum_pollers = try(provisioned_poller_config.value.minimum_pollers, null)
214+
}
215+
}
216+
217+
tags = merge(var.tags, try(each.value.tags, {}))
218+
}

variables.tf

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,37 @@ variable "tags" {
126126
description = "Tags"
127127
default = {}
128128
}
129+
130+
variable "tracing_mode" {
131+
description = "Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active."
132+
type = string
133+
default = null
134+
}
135+
136+
variable "image_config_entry_point" {
137+
description = "The ENTRYPOINT for the docker image"
138+
type = list(string)
139+
default = []
140+
}
141+
142+
variable "image_config_command" {
143+
description = "The CMD for the docker image"
144+
type = list(string)
145+
default = []
146+
}
147+
148+
variable "image_config_working_directory" {
149+
description = "The working directory for the docker image"
150+
type = string
151+
default = null
152+
}
153+
154+
############################################
155+
# Lambda Event Source Mapping
156+
############################################
157+
158+
variable "event_source_mapping" {
159+
description = "Map of event source mapping"
160+
type = any
161+
default = {}
162+
}

0 commit comments

Comments
 (0)