From af22d006c0b771809a0bf7a7a2bda49dafabb6a5 Mon Sep 17 00:00:00 2001 From: Matt Empson Date: Fri, 17 Jun 2022 05:38:50 +1000 Subject: [PATCH] feat: Added support for event source mapping in alias submodule (#320) --- examples/alias/README.md | 5 ++++ examples/alias/main.tf | 27 ++++++++++++++++++ examples/alias/outputs.tf | 20 ++++++++++++++ modules/alias/README.md | 6 ++++ modules/alias/main.tf | 56 ++++++++++++++++++++++++++++++++++++++ modules/alias/outputs.tf | 20 ++++++++++++++ modules/alias/variables.tf | 10 +++++++ 7 files changed, 144 insertions(+) diff --git a/examples/alias/README.md b/examples/alias/README.md index c57e10b8..115cbcb7 100644 --- a/examples/alias/README.md +++ b/examples/alias/README.md @@ -37,6 +37,7 @@ Note that this example may create resources which cost money. Run `terraform des | [alias\_no\_refresh](#module\_alias\_no\_refresh) | ../../modules/alias | n/a | | [alias\_refresh](#module\_alias\_refresh) | ../../modules/alias | n/a | | [lambda\_function](#module\_lambda\_function) | ../../ | n/a | +| [sqs\_events](#module\_sqs\_events) | terraform-aws-modules/sqs/aws | ~> 3.0 | ## Resources @@ -54,6 +55,10 @@ No inputs. |------|-------------| | [lambda\_alias\_arn](#output\_lambda\_alias\_arn) | The ARN of the Lambda Function Alias | | [lambda\_alias\_description](#output\_lambda\_alias\_description) | Description of alias | +| [lambda\_alias\_event\_source\_mapping\_function\_arn](#output\_lambda\_alias\_event\_source\_mapping\_function\_arn) | The the ARN of the Lambda function the event source mapping is sending events to | +| [lambda\_alias\_event\_source\_mapping\_state](#output\_lambda\_alias\_event\_source\_mapping\_state) | The state of the event source mapping | +| [lambda\_alias\_event\_source\_mapping\_state\_transition\_reason](#output\_lambda\_alias\_event\_source\_mapping\_state\_transition\_reason) | The reason the event source mapping is in its current state | +| [lambda\_alias\_event\_source\_mapping\_uuid](#output\_lambda\_alias\_event\_source\_mapping\_uuid) | The UUID of the created event source mapping | | [lambda\_alias\_function\_version](#output\_lambda\_alias\_function\_version) | Lambda function version which the alias uses | | [lambda\_alias\_invoke\_arn](#output\_lambda\_alias\_invoke\_arn) | The ARN to be used for invoking Lambda Function from API Gateway | | [lambda\_alias\_name](#output\_lambda\_alias\_name) | The name of the Lambda Function Alias | diff --git a/examples/alias/main.tf b/examples/alias/main.tf index f429bdff..d39322de 100644 --- a/examples/alias/main.tf +++ b/examples/alias/main.tf @@ -13,6 +13,13 @@ resource "random_pet" "this" { length = 2 } +module "sqs_events" { + source = "terraform-aws-modules/sqs/aws" + version = "~> 3.0" + + name = "${random_pet.this.id}-events" +} + module "lambda_function" { source = "../../" @@ -29,6 +36,12 @@ module "lambda_function" { provisioned_concurrent_executions = 1 + attach_policies = true + policies = [ + "arn:aws:iam::aws:policy/service-role/AWSLambdaSQSQueueExecutionRole", + ] + number_of_policies = 1 + allowed_triggers = { APIGatewayAny = { service = "apigateway" @@ -60,6 +73,13 @@ module "alias_no_refresh" { # create_async_event_config = true # maximum_event_age_in_seconds = 130 + event_source_mapping = { + sqs = { + service = "sqs" + event_source_arn = module.sqs_events.sqs_queue_arn + } + } + allowed_triggers = { AnotherAPIGatewayAny = { # keys should be unique service = "apigateway" @@ -92,6 +112,13 @@ module "alias_existing" { create_async_event_config = true maximum_event_age_in_seconds = 100 + event_source_mapping = { + sqs = { + service = "sqs" + event_source_arn = module.sqs_events.sqs_queue_arn + } + } + allowed_triggers = { ThirdAPIGatewayAny = { service = "apigateway" diff --git a/examples/alias/outputs.tf b/examples/alias/outputs.tf index a2b13ac9..30d33e80 100644 --- a/examples/alias/outputs.tf +++ b/examples/alias/outputs.tf @@ -119,3 +119,23 @@ output "lambda_alias_function_version" { description = "Lambda function version which the alias uses" value = module.alias_refresh.lambda_alias_function_version } + +output "lambda_alias_event_source_mapping_function_arn" { + description = "The the ARN of the Lambda function the event source mapping is sending events to" + value = module.alias_no_refresh.lambda_alias_event_source_mapping_function_arn +} + +output "lambda_alias_event_source_mapping_state" { + description = "The state of the event source mapping" + value = module.alias_no_refresh.lambda_alias_event_source_mapping_state +} + +output "lambda_alias_event_source_mapping_state_transition_reason" { + description = "The reason the event source mapping is in its current state" + value = module.alias_no_refresh.lambda_alias_event_source_mapping_state_transition_reason +} + +output "lambda_alias_event_source_mapping_uuid" { + description = "The UUID of the created event source mapping" + value = module.alias_no_refresh.lambda_alias_event_source_mapping_uuid +} diff --git a/modules/alias/README.md b/modules/alias/README.md index 303c314d..9466f51c 100644 --- a/modules/alias/README.md +++ b/modules/alias/README.md @@ -134,6 +134,7 @@ No modules. |------|------| | [aws_lambda_alias.no_refresh](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_alias) | resource | | [aws_lambda_alias.with_refresh](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_alias) | 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_event_invoke_config.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function_event_invoke_config) | resource | | [aws_lambda_permission.qualified_alias_triggers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource | | [aws_lambda_permission.version_triggers](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource | @@ -153,6 +154,7 @@ No modules. | [description](#input\_description) | Description of the alias. | `string` | `""` | no | | [destination\_on\_failure](#input\_destination\_on\_failure) | Amazon Resource Name (ARN) of the destination resource for failed asynchronous invocations | `string` | `null` | no | | [destination\_on\_success](#input\_destination\_on\_success) | Amazon Resource Name (ARN) of the destination resource for successful asynchronous invocations | `string` | `null` | no | +| [event\_source\_mapping](#input\_event\_source\_mapping) | Map of event source mapping | `any` | `{}` | no | | [function\_name](#input\_function\_name) | The function ARN of the Lambda function for which you want to create an alias. | `string` | `""` | no | | [function\_version](#input\_function\_version) | Lambda function version for which you are creating the alias. Pattern: ($LATEST\|[0-9]+). | `string` | `""` | no | | [maximum\_event\_age\_in\_seconds](#input\_maximum\_event\_age\_in\_seconds) | Maximum age of a request that Lambda sends to a function for processing in seconds. Valid values between 60 and 21600. | `number` | `null` | no | @@ -168,6 +170,10 @@ No modules. |------|-------------| | [lambda\_alias\_arn](#output\_lambda\_alias\_arn) | The ARN of the Lambda Function Alias | | [lambda\_alias\_description](#output\_lambda\_alias\_description) | Description of alias | +| [lambda\_alias\_event\_source\_mapping\_function\_arn](#output\_lambda\_alias\_event\_source\_mapping\_function\_arn) | The the ARN of the Lambda function the event source mapping is sending events to | +| [lambda\_alias\_event\_source\_mapping\_state](#output\_lambda\_alias\_event\_source\_mapping\_state) | The state of the event source mapping | +| [lambda\_alias\_event\_source\_mapping\_state\_transition\_reason](#output\_lambda\_alias\_event\_source\_mapping\_state\_transition\_reason) | The reason the event source mapping is in its current state | +| [lambda\_alias\_event\_source\_mapping\_uuid](#output\_lambda\_alias\_event\_source\_mapping\_uuid) | The UUID of the created event source mapping | | [lambda\_alias\_function\_version](#output\_lambda\_alias\_function\_version) | Lambda function version which the alias uses | | [lambda\_alias\_invoke\_arn](#output\_lambda\_alias\_invoke\_arn) | The ARN to be used for invoking Lambda Function from API Gateway | | [lambda\_alias\_name](#output\_lambda\_alias\_name) | The name of the Lambda Function Alias | diff --git a/modules/alias/main.tf b/modules/alias/main.tf index 859c125a..823a4f7b 100644 --- a/modules/alias/main.tf +++ b/modules/alias/main.tf @@ -1,4 +1,5 @@ locals { + alias_arn = try(data.aws_lambda_alias.existing[0].arn, aws_lambda_alias.no_refresh[0].arn, aws_lambda_alias.with_refresh[0].arn, "") version = try(data.aws_lambda_alias.existing[0].function_version, aws_lambda_alias.with_refresh[0].function_version, aws_lambda_alias.no_refresh[0].function_version, "") qualifiers = zipmap(["version", "qualified_alias"], [var.create_version_async_event_config ? true : null, var.create_qualified_alias_async_event_config ? true : null]) } @@ -108,3 +109,58 @@ resource "aws_lambda_permission" "qualified_alias_triggers" { source_account = try(each.value.source_account, null) event_source_token = try(each.value.event_source_token, null) } + +resource "aws_lambda_event_source_mapping" "this" { + for_each = { for k, v in var.event_source_mapping : k => v if var.create } + + function_name = local.alias_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, null) + 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) + topics = try(each.value.topics, null) + queues = try(each.value.queues, null) + function_response_types = try(each.value.function_response_types, 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 "self_managed_event_source" { + for_each = try(each.value.self_managed_event_source, []) + content { + endpoints = self_managed_event_source.value.endpoints + } + } + + dynamic "source_access_configuration" { + for_each = try(each.value.source_access_configuration, []) + content { + type = source_access_configuration.value["type"] + uri = source_access_configuration.value["uri"] + } + } + + dynamic "filter_criteria" { + for_each = try(each.value.filter_criteria, null) != null ? [true] : [] + + content { + filter { + pattern = try(each.value["filter_criteria"].pattern, null) + } + } + } +} diff --git a/modules/alias/outputs.tf b/modules/alias/outputs.tf index 4a774faf..b1a29153 100644 --- a/modules/alias/outputs.tf +++ b/modules/alias/outputs.tf @@ -23,3 +23,23 @@ output "lambda_alias_function_version" { description = "Lambda function version which the alias uses" value = try(data.aws_lambda_alias.existing[0].function_version, aws_lambda_alias.with_refresh[0].function_version, aws_lambda_alias.no_refresh[0].function_version, "") } + +output "lambda_alias_event_source_mapping_function_arn" { + description = "The the ARN of the Lambda function the event source mapping is sending events to" + value = { for k, v in aws_lambda_event_source_mapping.this : k => v.function_arn } +} + +output "lambda_alias_event_source_mapping_state" { + description = "The state of the event source mapping" + value = { for k, v in aws_lambda_event_source_mapping.this : k => v.state } +} + +output "lambda_alias_event_source_mapping_state_transition_reason" { + description = "The reason the event source mapping is in its current state" + value = { for k, v in aws_lambda_event_source_mapping.this : k => v.state_transition_reason } +} + +output "lambda_alias_event_source_mapping_uuid" { + description = "The UUID of the created event source mapping" + value = { for k, v in aws_lambda_event_source_mapping.this : k => v.uuid } +} diff --git a/modules/alias/variables.tf b/modules/alias/variables.tf index d5601998..732067f6 100644 --- a/modules/alias/variables.tf +++ b/modules/alias/variables.tf @@ -117,3 +117,13 @@ variable "allowed_triggers" { type = map(any) default = {} } + +############################################ +# Lambda Event Source Mapping +############################################ + +variable "event_source_mapping" { + description = "Map of event source mapping" + type = any + default = {} +}