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

Commit 2f0dd4f

Browse files
authored
External SQS trigger (#34)
1 parent 98c088f commit 2f0dd4f

File tree

8 files changed

+209
-16
lines changed

8 files changed

+209
-16
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ MIT Licensed. See LICENSE for full details.
3434
| 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 |
3535
| description | Lambda function description | `string` | `"Managed by Terraform"` | no |
3636
| environment | Lambda environment variables | `map(string)` | `null` | no |
37-
| file\_name | Lambda function filename name | `string` | n/a | yes |
37+
| file\_name | Lambda function filename name | `string` | `null` | no |
3838
| function\_name | Lambda function name | `string` | n/a | yes |
3939
| handler | Lambda function handler | `string` | n/a | yes |
40-
| image\_config | Container image configuration values that override the values in the container image Dockerfile. | `map(string)` | `{}` | no |
40+
| 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 |
4141
| image\_uri | ECR image URI containing the function's deployment package | `string` | `null` | no |
4242
| 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 |
4343
| layers | List of layers for this lambda function | `list(string)` | `[]` | no |
@@ -47,6 +47,7 @@ MIT Licensed. See LICENSE for full details.
4747
| reserved\_concurrent\_executions | Reserved concurrent executions for this lambda function | `number` | `-1` | no |
4848
| role | Lambda function role | `string` | n/a | yes |
4949
| runtime | Lambda function runtime | `string` | `"nodejs14.x"` | no |
50+
| sqs\_external | External SQS to consume | <pre>object({<br> batch_size = number<br> sqs_arns = list(string)<br> })</pre> | `null` | no |
5051
| tags | Tags for this lambda function | `map(string)` | `{}` | no |
5152
| timeout | Lambda function runtime | `number` | `300` | no |
5253
| 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 |

examples/docker_image_override_image_config/main.tf

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ module "null_trigger" {
3939

4040
source = "../../"
4141

42-
image_uri = "636553281721.dkr.ecr.us-east-1.amazonaws.com/test-lambda:latest"
42+
image_uri = "636553281721.dkr.ecr.us-east-1.amazonaws.com/test-lambda:latest"
4343
image_config = {
44-
command = ["index.anotherHandler"]
44+
command = ["index.anotherHandler"]
4545
working_directory = "/var/task"
46-
entry_point = ["/lambda-entrypoint.sh"]
46+
entry_point = ["/lambda-entrypoint.sh"]
4747
}
4848
function_name = var.function_name
4949
handler = "index.handler"
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
variable "function_name" {
2+
type = string
3+
}
4+
5+
data "aws_iam_policy_document" "assume_role" {
6+
statement {
7+
actions = ["sts:AssumeRole"]
8+
9+
principals {
10+
type = "Service"
11+
identifiers = ["lambda.amazonaws.com"]
12+
}
13+
}
14+
}
15+
16+
# Do not use the below policy anywhere
17+
data "aws_iam_policy_document" "policy" {
18+
statement {
19+
actions = ["*"]
20+
resources = ["*"]
21+
}
22+
}
23+
24+
resource "aws_iam_role" "lambda" {
25+
name = var.function_name
26+
assume_role_policy = data.aws_iam_policy_document.assume_role.json
27+
force_detach_policies = true
28+
}
29+
30+
resource "aws_iam_role_policy" "lambda" {
31+
name = var.function_name
32+
role = aws_iam_role.lambda.id
33+
34+
policy = data.aws_iam_policy_document.policy.json
35+
}
36+
37+
38+
resource "aws_sqs_queue" "fifo" {
39+
name = "my-queue.fifo"
40+
fifo_queue = true
41+
}
42+
43+
resource "aws_sqs_queue" "one" {
44+
name = "one"
45+
}
46+
47+
resource "aws_sqs_queue" "two" {
48+
name = "two"
49+
}
50+
51+
resource "aws_sqs_queue" "three" {
52+
name = "three"
53+
}
54+
55+
module "sqs" {
56+
57+
source = "../../"
58+
59+
file_name = "${path.module}/../../test/fixtures/foo.zip"
60+
function_name = var.function_name
61+
handler = "index.handler"
62+
publish = true
63+
role = aws_iam_role.lambda.arn
64+
timeout = 10
65+
66+
trigger = {
67+
type = "sqs-external"
68+
}
69+
sqs_external = {
70+
batch_size = 1
71+
sqs_arns = [
72+
aws_sqs_queue.one.arn,
73+
aws_sqs_queue.two.arn,
74+
aws_sqs_queue.three.arn,
75+
aws_sqs_queue.fifo.arn,
76+
]
77+
}
78+
environment = {
79+
"LOREM" = "IPSUM"
80+
}
81+
region = "us-east-1"
82+
tags = {
83+
"Foo" : var.function_name
84+
}
85+
}
86+
87+
output "arn" {
88+
description = "AWS lambda arn"
89+
value = module.sqs.arn
90+
}
91+
92+
output "qualified_arn" {
93+
description = "AWS lambda qualified_arn"
94+
value = module.sqs.qualified_arn
95+
}
96+
97+
output "invoke_arn" {
98+
description = "AWS lambda invoke_arn"
99+
value = module.sqs.invoke_arn
100+
}
101+
102+
output "version" {
103+
description = "AWS lambda version"
104+
value = module.sqs.version
105+
}
106+
107+
output "dlq" {
108+
description = "AWS lambda Dead Letter Queue details"
109+
value = module.sqs.dlq
110+
}
111+
112+
output "queue" {
113+
description = "AWS lambda SQS details"
114+
value = module.sqs.queue
115+
}
116+

main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ resource "aws_lambda_function" "lambda" {
3131
publish = var.publish
3232
source_code_hash = local.source_code_hash
3333
image_uri = var.image_uri
34-
package_type = var.file_name != null ? "Zip" : "Image"
34+
package_type = var.file_name != null ? "Zip" : "Image"
3535

3636
dynamic "image_config" {
3737
for_each = var.image_config == null ? [] : [var.image_config]
@@ -155,6 +155,13 @@ module "triggered_by_kinesis" {
155155
kinesis_configuration = var.kinesis_configuration
156156
}
157157

158+
module "sqs_external" {
159+
source = "./triggers/sqs_external/"
160+
161+
lambda_function_arn = aws_lambda_function.lambda.arn
162+
sqs_external = var.sqs_external
163+
}
164+
158165
module "cloudwatch-log-subscription" {
159166
enable = var.cloudwatch_log_subscription.enable
160167

test/lambda_aws_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,14 +218,32 @@ func TestLambda_sqsTriggerExample(t *testing.T) {
218218
ValidateSQSTriggerOutputs(t, terraformOptions, false)
219219
}
220220

221+
func TestLambda_sqsExternalTriggerExample(t *testing.T) {
222+
t.Parallel()
223+
224+
functionName := fmt.Sprintf("lambda-%s", random.UniqueId())
225+
exampleDir := "../examples/sqs_external_trigger/"
226+
227+
sqsTargets := []string{"aws_sqs_queue.one", "aws_sqs_queue.two", "aws_sqs_queue.three", "aws_sqs_queue.fifo"}
228+
sqsTerraformOptions := SetupExample(t, functionName, exampleDir, sqsTargets)
229+
t.Logf("Terraform module inputs: %+v", *sqsTerraformOptions)
230+
terraform.InitAndApply(t, sqsTerraformOptions)
231+
232+
terraformOptions := SetupExample(t, functionName, exampleDir, nil)
233+
t.Logf("Terraform module inputs: %+v", *terraformOptions)
234+
defer terraform.Destroy(t, terraformOptions)
235+
236+
TerraformApplyAndValidateOutputs(t, terraformOptions)
237+
}
238+
221239
func TestLambda_sqsSnsTriggerExample(t *testing.T) {
222240
t.Parallel()
223241

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

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

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

259-
sns_targets := []string{"aws_sns_topic.foo", "aws_sns_topic.bar", "aws_sns_topic.baz"}
277+
snsTargets := []string{"aws_sns_topic.foo", "aws_sns_topic.bar", "aws_sns_topic.baz"}
260278

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

triggers/sqs_external/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Requirements
2+
3+
No requirements.
4+
5+
## Providers
6+
7+
| Name | Version |
8+
|------|---------|
9+
| aws | n/a |
10+
11+
## Inputs
12+
13+
| Name | Description | Type | Default | Required |
14+
|------|-------------|------|---------|:--------:|
15+
| lambda\_function\_arn | Lambda arn | `string` | n/a | yes |
16+
| sqs\_external | External SQS to consume | <pre>object({<br> batch_size = number<br> sqs_arns = list(string)<br> })</pre> | `null` | no |
17+
18+
## Outputs
19+
20+
No output.

triggers/sqs_external/main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
variable "lambda_function_arn" {
2+
type = string
3+
description = "Lambda arn"
4+
}
5+
6+
variable "sqs_external" {
7+
description = "External SQS to consume"
8+
type = object({
9+
batch_size = number
10+
sqs_arns = list(string)
11+
})
12+
default = null
13+
}
14+
15+
resource "aws_lambda_event_source_mapping" "this" {
16+
for_each = var.sqs_external == null ? toset([]) : toset(var.sqs_external.sqs_arns)
17+
18+
function_name = var.lambda_function_arn
19+
batch_size = var.sqs_external.batch_size
20+
event_source_arn = each.value
21+
}

variables.tf

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ variable "image_uri" {
1212

1313
variable "image_config" {
1414
description = "Container image configuration values that override the values in the container image Dockerfile."
15-
type = object({
16-
command = list(string)
17-
entry_point = list(string)
15+
type = object({
16+
command = list(string)
17+
entry_point = list(string)
1818
working_directory = string
1919
})
20-
default = null
20+
default = null
2121
}
2222

2323
variable "layers" {
@@ -101,9 +101,10 @@ variable "trigger" {
101101
"cloudwatch-event-schedule",
102102
"cloudwatch-event-trigger",
103103
"sqs",
104+
"sqs-external",
104105
"step-function",
105106
"kinesis",
106-
"null"
107+
"null",
107108
], var.trigger.type)
108109

109110
error_message = "Unknown trigger type."
@@ -156,7 +157,7 @@ locals {
156157
}
157158

158159
locals {
159-
source_code_hash = var.file_name != null ? filebase64sha256(var.file_name) : null
160+
source_code_hash = var.file_name != null ? filebase64sha256(var.file_name) : null
160161
tags = merge(var.tags, local._tags)
161162
cloudwatch_log_group_name = "/aws/lambda/${var.function_name}"
162163
}
@@ -189,3 +190,12 @@ variable "kinesis_configuration" {
189190
}))
190191
default = {}
191192
}
193+
194+
variable "sqs_external" {
195+
description = "External SQS to consume"
196+
type = object({
197+
batch_size = number
198+
sqs_arns = list(string)
199+
})
200+
default = null
201+
}

0 commit comments

Comments
 (0)