Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions EXAMPLE.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,22 @@ module "lambda_function" {
}
}
}
```

## Lambda Event Source Mapping
The example below demonstrates how to configure Lambda Event source Mapping
```
module "lambda_function" {
source = "git::https://github.com/TechHoldingLLC/terraform-aws-lambda-function.git?ref=v1.0.11"

# ...omitted for brevity

event_source_mapping = {
event_source_arn = module.sqs.arn
batch_size = 10
scaling_config = {
maximum_concurrency = 10
}
}
}
```
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ No modules.
| [aws_iam_role.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
| [aws_iam_role_policy_attachment.lambda_basic_execution](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_iam_role_policy_attachment.lambda_vpc_access](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
| [aws_lambda_event_source_mapping.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping) | resource |
| [aws_lambda_function.lambda](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) | resource |
| [aws_lambda_function_url.function_url](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function_url) | resource |
| [aws_lambda_permission.triggers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
Expand All @@ -40,10 +41,14 @@ No modules.
| <a name="input_description"></a> [description](#input\_description) | Lambda function description | `any` | `null` | no |
| <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 |
| <a name="input_environment_variables"></a> [environment\_variables](#input\_environment\_variables) | Environment Variables for Lambda Functions | `map(any)` | `{}` | no |
| <a name="input_event_source_mapping"></a> [event\_source\_mapping](#input\_event\_source\_mapping) | Map of event source mapping | `any` | `{}` | no |
| <a name="input_function_name"></a> [function\_name](#input\_function\_name) | Lambda function name | `any` | n/a | yes |
| <a name="input_function_url"></a> [function\_url](#input\_function\_url) | Create lambda function url | `bool` | `false` | no |
| <a name="input_function_url_cors"></a> [function\_url\_cors](#input\_function\_url\_cors) | Function url cors | `any` | `{}` | no |
| <a name="input_handler"></a> [handler](#input\_handler) | Name of Handler | `any` | `null` | no |
| <a name="input_image_config_command"></a> [image\_config\_command](#input\_image\_config\_command) | The CMD for the docker image | `list(string)` | `[]` | no |
| <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 |
| <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 |
| <a name="input_image_uri"></a> [image\_uri](#input\_image\_uri) | uri of image | `any` | `null` | no |
| <a name="input_lambda_memory"></a> [lambda\_memory](#input\_lambda\_memory) | Required Memory for Lambda function | `number` | `128` | no |
| <a name="input_lambda_runtime"></a> [lambda\_runtime](#input\_lambda\_runtime) | Lambda language | `any` | `null` | no |
Expand All @@ -60,6 +65,7 @@ No modules.
| <a name="input_source_file"></a> [source\_file](#input\_source\_file) | Lambda source file | `string` | `""` | no |
| <a name="input_subnets"></a> [subnets](#input\_subnets) | Subnets | `list(any)` | `null` | no |
| <a name="input_tags"></a> [tags](#input\_tags) | Tags | `map` | `{}` | no |
| <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 |

## Outputs

Expand Down
101 changes: 99 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ resource "aws_lambda_function" "lambda" {
s3_bucket = try(data.aws_s3_object.lambda[0].bucket, null)
s3_key = try(data.aws_s3_object.lambda[0].key, null)
s3_object_version = try(data.aws_s3_object.lambda[0].version_id, null)
source_code_hash = try(data.aws_s3_object.lambda[0].metadata.source_code_hash, null)
source_code_hash = try(data.aws_s3_object.lambda[0].metadata.source_code_hash, data.archive_file.lambda[0].output_base64sha256, null)
filename = try(data.archive_file.lambda[0].output_path, null)
image_uri = var.image_uri
package_type = var.package_type
Expand All @@ -67,6 +67,22 @@ resource "aws_lambda_function" "lambda" {
}
}

dynamic "tracing_config" {
for_each = var.tracing_mode == null ? [] : [true]
content {
mode = var.tracing_mode
}
}

dynamic "image_config" {
for_each = length(var.image_config_entry_point) > 0 || length(var.image_config_command) > 0 || var.image_config_working_directory != null ? [true] : []
content {
entry_point = var.image_config_entry_point
command = var.image_config_command
working_directory = var.image_config_working_directory
}
}

tags = var.tags
}

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

function_name = aws_lambda_function.lambda.function_name

statement_id = try(each.value.statement_id ,format("Allow%sLambdaInvoke", try(each.key, "")))
statement_id = try(each.value.statement_id, format("Allow%sLambdaInvoke", try(each.key, "")))
action = "lambda:InvokeFunction"
principal = try(each.value.principal, format("%s.amazonaws.com", try(each.value.service, "")))
source_arn = try(each.value.source_arn, null)
Expand Down Expand Up @@ -119,3 +135,84 @@ resource "aws_lambda_function_url" "function_url" {
}
}
}

# ------------------------------------------------------------------------------
# LAMBDA EVENT SOURCE MAPPING
# ------------------------------------------------------------------------------
resource "aws_lambda_event_source_mapping" "this" {
for_each = { for k, v in var.event_source_mapping : k => v }

function_name = aws_lambda_function.lambda.arn

event_source_arn = try(each.value.event_source_arn, null)

batch_size = try(each.value.batch_size, null)
maximum_batching_window_in_seconds = try(each.value.maximum_batching_window_in_seconds, null)
enabled = try(each.value.enabled, true)
starting_position = try(each.value.starting_position, null)
starting_position_timestamp = try(each.value.starting_position_timestamp, null)
parallelization_factor = try(each.value.parallelization_factor, null)
maximum_retry_attempts = try(each.value.maximum_retry_attempts, null)
maximum_record_age_in_seconds = try(each.value.maximum_record_age_in_seconds, null)
bisect_batch_on_function_error = try(each.value.bisect_batch_on_function_error, null)
function_response_types = try(each.value.function_response_types, null)
tumbling_window_in_seconds = try(each.value.tumbling_window_in_seconds, null)

dynamic "destination_config" {
for_each = try(each.value.destination_arn_on_failure, null) != null ? [true] : []
content {
on_failure {
destination_arn = each.value["destination_arn_on_failure"]
}
}
}

dynamic "scaling_config" {
for_each = try([each.value.scaling_config], [])
content {
maximum_concurrency = try(scaling_config.value.maximum_concurrency, null)
}
}

dynamic "filter_criteria" {
for_each = try(each.value.filter_criteria, null) != null ? [true] : []

content {
dynamic "filter" {
for_each = try(flatten([each.value.filter_criteria]), [])

content {
pattern = try(filter.value.pattern, null)
}
}
}
}

dynamic "document_db_event_source_config" {
for_each = try(each.value.document_db_event_source_config, [])

content {
database_name = document_db_event_source_config.value.database_name
collection_name = try(document_db_event_source_config.value.collection_name, null)
full_document = try(document_db_event_source_config.value.full_document, null)
}
}

dynamic "metrics_config" {
for_each = try([each.value.metrics_config], [])

content {
metrics = metrics_config.value.metrics
}
}

dynamic "provisioned_poller_config" {
for_each = try([each.value.provisioned_poller_config], [])
content {
maximum_pollers = try(provisioned_poller_config.value.maximum_pollers, null)
minimum_pollers = try(provisioned_poller_config.value.minimum_pollers, null)
}
}

tags = merge(var.tags, try(each.value.tags, {}))
}
34 changes: 34 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,37 @@ variable "tags" {
description = "Tags"
default = {}
}

variable "tracing_mode" {
description = "Tracing mode of the Lambda Function. Valid value can be either PassThrough or Active."
type = string
default = null
}

variable "image_config_entry_point" {
description = "The ENTRYPOINT for the docker image"
type = list(string)
default = []
}

variable "image_config_command" {
description = "The CMD for the docker image"
type = list(string)
default = []
}

variable "image_config_working_directory" {
description = "The working directory for the docker image"
type = string
default = null
}

############################################
# Lambda Event Source Mapping
############################################

variable "event_source_mapping" {
description = "Map of event source mapping"
type = any
default = {}
}