Skip to content

Commit 2fa4b14

Browse files
authored
feat(kinesis): support dedicated-throughput consumer with enhanced fan-out. (#66)
1 parent 8abe029 commit 2fa4b14

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

README.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module "lambda" {
9292
9393
// optionally overwrite `cloudwatch_event_target_arn` in case an alias should be used for the event rule
9494
cloudwatch_event_target_arn = aws_lambda_alias.example.arn
95-
95+
9696
// optionally add `cloudwatch_event_target_input` for event input
9797
cloudwatch_event_target_input = jsonencode({"key": "value"})
9898
}
@@ -114,10 +114,14 @@ module "lambda" {
114114

115115
[Event Source Mappings](https://www.terraform.io/docs/providers/aws/r/lambda_event_source_mapping.html) to trigger your Lambda function by DynamoDb,
116116
Kinesis and SQS can be declared inline. The module will add the required read-only IAM permissions depending on the event source type to
117-
the function role automatically. In addition, permissions to send discarded batches to SNS or SQS will be added automatically, if `destination_arn_on_failure` is configured.
117+
the function role automatically (including support for [dedicated-throughput consumers](https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-configure) using enhanced fan-out).
118+
119+
Permissions to send discarded batches to SNS or SQS will be added automatically, if `destination_arn_on_failure` is configured.
118120

119121
see [examples](examples/with-event-source-mappings) for details
120122

123+
#### DynamoDb
124+
121125
```hcl
122126
module "lambda" {
123127
// see above
@@ -155,6 +159,27 @@ module "lambda" {
155159
}
156160
```
157161

162+
#### Kinesis
163+
164+
```hcl
165+
resource "aws_kinesis_stream_consumer" "this" {
166+
name = module.lambda.function_name
167+
stream_arn = aws_kinesis_stream.stream_2.arn
168+
}
169+
170+
module "lambda" {
171+
// see above
172+
173+
event_source_mappings = {
174+
stream_1 = {
175+
// To use a dedicated-throughput consumer with enhanced fan-out, specify the consumer's ARN instead of the stream's ARN, see https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-configure
176+
event_source_arn = aws_kinesis_stream_consumer.this.arn
177+
}
178+
}
179+
}
180+
181+
```
182+
158183
### with SNS subscriptions
159184

160185
[SNS Topic Subscriptions](https://www.terraform.io/docs/providers/aws/r/sns_topic_subscription.html) to trigger your Lambda function by SNS can de declared inline.

docs/part1.md

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ module "lambda" {
9292
9393
// optionally overwrite `cloudwatch_event_target_arn` in case an alias should be used for the event rule
9494
cloudwatch_event_target_arn = aws_lambda_alias.example.arn
95-
95+
9696
// optionally add `cloudwatch_event_target_input` for event input
9797
cloudwatch_event_target_input = jsonencode({"key": "value"})
9898
}
@@ -114,10 +114,14 @@ module "lambda" {
114114

115115
[Event Source Mappings](https://www.terraform.io/docs/providers/aws/r/lambda_event_source_mapping.html) to trigger your Lambda function by DynamoDb,
116116
Kinesis and SQS can be declared inline. The module will add the required read-only IAM permissions depending on the event source type to
117-
the function role automatically. In addition, permissions to send discarded batches to SNS or SQS will be added automatically, if `destination_arn_on_failure` is configured.
117+
the function role automatically (including support for [dedicated-throughput consumers](https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-configure) using enhanced fan-out).
118+
119+
Permissions to send discarded batches to SNS or SQS will be added automatically, if `destination_arn_on_failure` is configured.
118120

119121
see [examples](examples/with-event-source-mappings) for details
120122

123+
#### DynamoDb
124+
121125
```hcl
122126
module "lambda" {
123127
// see above
@@ -155,6 +159,27 @@ module "lambda" {
155159
}
156160
```
157161

162+
#### Kinesis
163+
164+
```hcl
165+
resource "aws_kinesis_stream_consumer" "this" {
166+
name = module.lambda.function_name
167+
stream_arn = aws_kinesis_stream.stream_2.arn
168+
}
169+
170+
module "lambda" {
171+
// see above
172+
173+
event_source_mappings = {
174+
stream_1 = {
175+
// To use a dedicated-throughput consumer with enhanced fan-out, specify the consumer's ARN instead of the stream's ARN, see https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-configure
176+
event_source_arn = aws_kinesis_stream_consumer.this.arn
177+
}
178+
}
179+
}
180+
181+
```
182+
158183
### with SNS subscriptions
159184

160185
[SNS Topic Subscriptions](https://www.terraform.io/docs/providers/aws/r/sns_topic_subscription.html) to trigger your Lambda function by SNS can de declared inline.

event_source_mappings.tf

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ locals {
99
for k, v in var.event_source_mappings : lookup(v, "event_source_arn", null) if length(regexall(".*:kinesis:.*", lookup(v, "event_source_arn", null))) > 0
1010
]
1111

12+
// compute all Kinesis consumers for enhanced fan-out
13+
kinesis_consumers = [
14+
for k, v in var.event_source_mappings : lookup(v, "event_source_arn", null) if length(regexall(".*:kinesis:.*/consumer/.*", lookup(v, "event_source_arn", null))) > 0
15+
]
16+
1217
// compute all event source mappings for SQS
1318
sqs_event_sources = [
1419
for k, v in var.event_source_mappings : lookup(v, "event_source_arn", null) if length(regexall(".*:sqs:.*", lookup(v, "event_source_arn", null))) > 0
@@ -113,7 +118,7 @@ data "aws_iam_policy_document" "event_sources" {
113118
resources = [
114119
// extracting 'arn:${Partition}:kinesis:${Region}:${Account}:stream/' from the kinesis stream ARN
115120
// see https://docs.aws.amazon.com/IAM/latest/UserGuide/list_amazonkinesis.html#amazonkinesis-resources-for-iam-policies
116-
length(regexall("arn.*\\/", local.kinesis_event_sources[0])) > 0 ? "${regex("arn.*\\/", local.kinesis_event_sources[0])}*" : ""
121+
length(regexall("arn:.*:kinesis:.*:.*:stream/", local.kinesis_event_sources[0])) > 0 ? "${regex("arn:.*:kinesis:.*:.*:stream/", local.kinesis_event_sources[0])}*" : ""
117122
]
118123
}
119124
}
@@ -130,11 +135,19 @@ data "aws_iam_policy_document" "event_sources" {
130135
]
131136

132137
resources = [
133-
for arn in local.kinesis_event_sources : arn
138+
for arn in local.kinesis_event_sources : replace(arn, "/\\/consumer.*/", "")
134139
]
135140
}
136141
}
137142

143+
dynamic "statement" {
144+
for_each = length(local.kinesis_consumers) > 0 ? [true] : []
145+
content {
146+
actions = ["kinesis:SubscribeToShard"]
147+
resources = [for arn in local.kinesis_consumers : arn]
148+
}
149+
}
150+
138151
// SQS permission for on-failure destinations
139152
dynamic "statement" {
140153
for_each = length(local.on_failure_sqs_destination_arns) > 0 ? [true] : []

examples/with-event-source-mappings/kinesis/main.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,14 @@ module "lambda" {
4242
}
4343

4444
stream_2 = {
45-
event_source_arn = aws_kinesis_stream.stream_2.arn
45+
// To use a dedicated-throughput consumer with enhanced fan-out, specify the consumer's ARN instead of the stream's ARN, see https://docs.aws.amazon.com/lambda/latest/dg/with-kinesis.html#services-kinesis-configure
46+
event_source_arn = aws_kinesis_stream_consumer.this.arn
4647
}
4748
}
4849
}
50+
51+
resource "aws_kinesis_stream_consumer" "this" {
52+
name = module.lambda.function_name
53+
stream_arn = aws_kinesis_stream.stream_2.arn
54+
}
55+

modules/deployment/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ resource "aws_s3_bucket_object" "source" {
173173

174174
| Name | Version |
175175
|------|---------|
176-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.9 |
176+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.33.0 |
177177

178178
## Modules
179179

0 commit comments

Comments
 (0)