Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
External SQS trigger (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
Puneeth-n authored Nov 16, 2021
1 parent 98c088f commit 2f0dd4f
Show file tree
Hide file tree
Showing 8 changed files with 209 additions and 16 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ MIT Licensed. See LICENSE for full details.
| cloudwatch\_log\_subscription | Cloudwatch log stream configuration | <pre>object({<br> enable : bool<br> filter_pattern : string<br> destination_arn : string<br> })</pre> | <pre>{<br> "destination_arn": "",<br> "enable": false,<br> "filter_pattern": ""<br>}</pre> | no |
| description | Lambda function description | `string` | `"Managed by Terraform"` | no |
| environment | Lambda environment variables | `map(string)` | `null` | no |
| file\_name | Lambda function filename name | `string` | n/a | yes |
| file\_name | Lambda function filename name | `string` | `null` | no |
| function\_name | Lambda function name | `string` | n/a | yes |
| handler | Lambda function handler | `string` | n/a | yes |
| image\_config | Container image configuration values that override the values in the container image Dockerfile. | `map(string)` | `{}` | no |
| image\_config | Container image configuration values that override the values in the container image Dockerfile. | <pre>object({<br> command = list(string)<br> entry_point = list(string)<br> working_directory = string<br> })</pre> | `null` | no |
| image\_uri | ECR image URI containing the function's deployment package | `string` | `null` | no |
| kinesis\_configuration | https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_event_source_mapping | <pre>map(object({<br> batch_size = number<br> bisect_batch_on_function_error = bool<br> destination_config__on_failure__destination_arn = string<br> event_source_arn = string<br> maximum_batching_window_in_seconds = number<br> maximum_record_age_in_seconds = number<br> maximum_retry_attempts = number<br> parallelization_factor = number<br> starting_position = string<br> starting_position_timestamp = string<br> tumbling_window_in_seconds = number<br> }))</pre> | `{}` | no |
| layers | List of layers for this lambda function | `list(string)` | `[]` | no |
Expand All @@ -47,6 +47,7 @@ MIT Licensed. See LICENSE for full details.
| reserved\_concurrent\_executions | Reserved concurrent executions for this lambda function | `number` | `-1` | no |
| role | Lambda function role | `string` | n/a | yes |
| runtime | Lambda function runtime | `string` | `"nodejs14.x"` | no |
| sqs\_external | External SQS to consume | <pre>object({<br> batch_size = number<br> sqs_arns = list(string)<br> })</pre> | `null` | no |
| tags | Tags for this lambda function | `map(string)` | `{}` | no |
| timeout | Lambda function runtime | `number` | `300` | no |
| tracing\_config | https://www.terraform.io/docs/providers/aws/r/lambda_function.html | <pre>object({<br> mode : string<br> })</pre> | <pre>{<br> "mode": "PassThrough"<br>}</pre> | no |
Expand Down
6 changes: 3 additions & 3 deletions examples/docker_image_override_image_config/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ module "null_trigger" {

source = "../../"

image_uri = "636553281721.dkr.ecr.us-east-1.amazonaws.com/test-lambda:latest"
image_uri = "636553281721.dkr.ecr.us-east-1.amazonaws.com/test-lambda:latest"
image_config = {
command = ["index.anotherHandler"]
command = ["index.anotherHandler"]
working_directory = "/var/task"
entry_point = ["/lambda-entrypoint.sh"]
entry_point = ["/lambda-entrypoint.sh"]
}
function_name = var.function_name
handler = "index.handler"
Expand Down
116 changes: 116 additions & 0 deletions examples/sqs_external_trigger/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
variable "function_name" {
type = string
}

data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}

# Do not use the below policy anywhere
data "aws_iam_policy_document" "policy" {
statement {
actions = ["*"]
resources = ["*"]
}
}

resource "aws_iam_role" "lambda" {
name = var.function_name
assume_role_policy = data.aws_iam_policy_document.assume_role.json
force_detach_policies = true
}

resource "aws_iam_role_policy" "lambda" {
name = var.function_name
role = aws_iam_role.lambda.id

policy = data.aws_iam_policy_document.policy.json
}


resource "aws_sqs_queue" "fifo" {
name = "my-queue.fifo"
fifo_queue = true
}

resource "aws_sqs_queue" "one" {
name = "one"
}

resource "aws_sqs_queue" "two" {
name = "two"
}

resource "aws_sqs_queue" "three" {
name = "three"
}

module "sqs" {

source = "../../"

file_name = "${path.module}/../../test/fixtures/foo.zip"
function_name = var.function_name
handler = "index.handler"
publish = true
role = aws_iam_role.lambda.arn
timeout = 10

trigger = {
type = "sqs-external"
}
sqs_external = {
batch_size = 1
sqs_arns = [
aws_sqs_queue.one.arn,
aws_sqs_queue.two.arn,
aws_sqs_queue.three.arn,
aws_sqs_queue.fifo.arn,
]
}
environment = {
"LOREM" = "IPSUM"
}
region = "us-east-1"
tags = {
"Foo" : var.function_name
}
}

output "arn" {
description = "AWS lambda arn"
value = module.sqs.arn
}

output "qualified_arn" {
description = "AWS lambda qualified_arn"
value = module.sqs.qualified_arn
}

output "invoke_arn" {
description = "AWS lambda invoke_arn"
value = module.sqs.invoke_arn
}

output "version" {
description = "AWS lambda version"
value = module.sqs.version
}

output "dlq" {
description = "AWS lambda Dead Letter Queue details"
value = module.sqs.dlq
}

output "queue" {
description = "AWS lambda SQS details"
value = module.sqs.queue
}

9 changes: 8 additions & 1 deletion main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ resource "aws_lambda_function" "lambda" {
publish = var.publish
source_code_hash = local.source_code_hash
image_uri = var.image_uri
package_type = var.file_name != null ? "Zip" : "Image"
package_type = var.file_name != null ? "Zip" : "Image"

dynamic "image_config" {
for_each = var.image_config == null ? [] : [var.image_config]
Expand Down Expand Up @@ -155,6 +155,13 @@ module "triggered_by_kinesis" {
kinesis_configuration = var.kinesis_configuration
}

module "sqs_external" {
source = "./triggers/sqs_external/"

lambda_function_arn = aws_lambda_function.lambda.arn
sqs_external = var.sqs_external
}

module "cloudwatch-log-subscription" {
enable = var.cloudwatch_log_subscription.enable

Expand Down
26 changes: 22 additions & 4 deletions test/lambda_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,32 @@ func TestLambda_sqsTriggerExample(t *testing.T) {
ValidateSQSTriggerOutputs(t, terraformOptions, false)
}

func TestLambda_sqsExternalTriggerExample(t *testing.T) {
t.Parallel()

functionName := fmt.Sprintf("lambda-%s", random.UniqueId())
exampleDir := "../examples/sqs_external_trigger/"

sqsTargets := []string{"aws_sqs_queue.one", "aws_sqs_queue.two", "aws_sqs_queue.three", "aws_sqs_queue.fifo"}
sqsTerraformOptions := SetupExample(t, functionName, exampleDir, sqsTargets)
t.Logf("Terraform module inputs: %+v", *sqsTerraformOptions)
terraform.InitAndApply(t, sqsTerraformOptions)

terraformOptions := SetupExample(t, functionName, exampleDir, nil)
t.Logf("Terraform module inputs: %+v", *terraformOptions)
defer terraform.Destroy(t, terraformOptions)

TerraformApplyAndValidateOutputs(t, terraformOptions)
}

func TestLambda_sqsSnsTriggerExample(t *testing.T) {
t.Parallel()

functionName := fmt.Sprintf("lambda-%s", random.UniqueId())
exampleDir := "../examples/sqs_sns_trigger/"
sns_targets := []string{"aws_sns_topic.foo", "aws_sns_topic.bar", "aws_sns_topic.baz"}
snsTargets := []string{"aws_sns_topic.foo", "aws_sns_topic.bar", "aws_sns_topic.baz"}

snsTerraformOptions := SetupExample(t, functionName, exampleDir, sns_targets)
snsTerraformOptions := SetupExample(t, functionName, exampleDir, snsTargets)
t.Logf("Terraform module inputs: %+v", *snsTerraformOptions)
terraform.InitAndApply(t, snsTerraformOptions)

Expand Down Expand Up @@ -256,9 +274,9 @@ func TestLambda_sqsFifoSnsTriggerExample(t *testing.T) {
functionName := fmt.Sprintf("lambda-%s", random.UniqueId())
exampleDir := "../examples/sqs_fifo_sns_trigger/"

sns_targets := []string{"aws_sns_topic.foo", "aws_sns_topic.bar", "aws_sns_topic.baz"}
snsTargets := []string{"aws_sns_topic.foo", "aws_sns_topic.bar", "aws_sns_topic.baz"}

snsTerraformOptions := SetupExample(t, functionName, exampleDir, sns_targets)
snsTerraformOptions := SetupExample(t, functionName, exampleDir, snsTargets)
t.Logf("Terraform module inputs: %+v", *snsTerraformOptions)
terraform.InitAndApply(t, snsTerraformOptions)

Expand Down
20 changes: 20 additions & 0 deletions triggers/sqs_external/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Requirements

No requirements.

## Providers

| Name | Version |
|------|---------|
| aws | n/a |

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| lambda\_function\_arn | Lambda arn | `string` | n/a | yes |
| sqs\_external | External SQS to consume | <pre>object({<br> batch_size = number<br> sqs_arns = list(string)<br> })</pre> | `null` | no |

## Outputs

No output.
21 changes: 21 additions & 0 deletions triggers/sqs_external/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
variable "lambda_function_arn" {
type = string
description = "Lambda arn"
}

variable "sqs_external" {
description = "External SQS to consume"
type = object({
batch_size = number
sqs_arns = list(string)
})
default = null
}

resource "aws_lambda_event_source_mapping" "this" {
for_each = var.sqs_external == null ? toset([]) : toset(var.sqs_external.sqs_arns)

function_name = var.lambda_function_arn
batch_size = var.sqs_external.batch_size
event_source_arn = each.value
}
22 changes: 16 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ variable "image_uri" {

variable "image_config" {
description = "Container image configuration values that override the values in the container image Dockerfile."
type = object({
command = list(string)
entry_point = list(string)
type = object({
command = list(string)
entry_point = list(string)
working_directory = string
})
default = null
default = null
}

variable "layers" {
Expand Down Expand Up @@ -101,9 +101,10 @@ variable "trigger" {
"cloudwatch-event-schedule",
"cloudwatch-event-trigger",
"sqs",
"sqs-external",
"step-function",
"kinesis",
"null"
"null",
], var.trigger.type)

error_message = "Unknown trigger type."
Expand Down Expand Up @@ -156,7 +157,7 @@ locals {
}

locals {
source_code_hash = var.file_name != null ? filebase64sha256(var.file_name) : null
source_code_hash = var.file_name != null ? filebase64sha256(var.file_name) : null
tags = merge(var.tags, local._tags)
cloudwatch_log_group_name = "/aws/lambda/${var.function_name}"
}
Expand Down Expand Up @@ -189,3 +190,12 @@ variable "kinesis_configuration" {
}))
default = {}
}

variable "sqs_external" {
description = "External SQS to consume"
type = object({
batch_size = number
sqs_arns = list(string)
})
default = null
}

0 comments on commit 2f0dd4f

Please sign in to comment.