From 74f033f123b94763bfe9cba008d1e08b9e478272 Mon Sep 17 00:00:00 2001 From: joyarackal Date: Tue, 1 Dec 2020 23:39:47 +0100 Subject: [PATCH 1/7] add maximum_batching_window_in_seconds support for sqs as well --- aws/resource_aws_lambda_event_source_mapping.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_lambda_event_source_mapping.go b/aws/resource_aws_lambda_event_source_mapping.go index b75bc6b3bb7..f695ad28c3c 100644 --- a/aws/resource_aws_lambda_event_source_mapping.go +++ b/aws/resource_aws_lambda_event_source_mapping.go @@ -364,9 +364,9 @@ func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta inte return fmt.Errorf("Error updating event source mapping: %s", err) } - if eventSourceArn.Service != "sqs" { - params.MaximumBatchingWindowInSeconds = aws.Int64(int64(d.Get("maximum_batching_window_in_seconds").(int))) + params.MaximumBatchingWindowInSeconds = aws.Int64(int64(d.Get("maximum_batching_window_in_seconds").(int))) + if eventSourceArn.Service != "sqs" { if parallelizationFactor, ok := d.GetOk("parallelization_factor"); ok { params.SetParallelizationFactor(int64(parallelizationFactor.(int))) } From b54829b578e444484344fd8cdd57738ba5f7fe74 Mon Sep 17 00:00:00 2001 From: joyarackal Date: Tue, 1 Dec 2020 23:42:07 +0100 Subject: [PATCH 2/7] add acceptance test --- ...ce_aws_lambda_event_source_mapping_test.go | 104 ++++++++++++++++++ 1 file changed, 104 insertions(+) diff --git a/aws/resource_aws_lambda_event_source_mapping_test.go b/aws/resource_aws_lambda_event_source_mapping_test.go index d146a252da2..a25b2d89158 100644 --- a/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/aws/resource_aws_lambda_event_source_mapping_test.go @@ -209,6 +209,43 @@ func TestAccAWSLambdaEventSourceMapping_sqs_withFunctionName(t *testing.T) { }) } +func TestAccAWSLambdaEventSourceMapping_SQSBatchWindow(t *testing.T) { + var conf lambda.EventSourceMappingConfiguration + + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_lambda_event_source_mapping.test" + batchWindow := int64(0) + batchWindowUpdate := int64(100) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckLambdaEventSourceMappingDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSLambdaEventSourceMappingConfigSqsWithBatchWindow(rName, batchWindow), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "maximum_batching_window_in_seconds", strconv.Itoa(int(batchWindow))), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"enabled", "starting_position"}, + }, + { + Config: testAccAWSLambdaEventSourceMappingConfigSqsWithBatchWindow(rName, batchWindowUpdate), + Check: resource.ComposeTestCheckFunc( + testAccCheckAwsLambdaEventSourceMappingExists(resourceName, &conf), + resource.TestCheckResourceAttr(resourceName, "maximum_batching_window_in_seconds", strconv.Itoa(int(batchWindowUpdate))), + ), + }, + }, + }) +} + func TestAccAWSLambdaEventSourceMapping_kinesis_disappears(t *testing.T) { var conf lambda.EventSourceMappingConfiguration @@ -1451,3 +1488,70 @@ resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { } `, roleName, policyName, attName, streamName, funcName) } + +func testAccAWSLambdaEventSourceMappingConfigSQSBase(rName string) string { + return fmt.Sprintf(` +resource "aws_iam_role" "test" { + name = %q + + assume_role_policy = < Date: Tue, 1 Dec 2020 23:52:10 +0100 Subject: [PATCH 3/7] fix fmt --- aws/resource_aws_lambda_event_source_mapping_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_lambda_event_source_mapping_test.go b/aws/resource_aws_lambda_event_source_mapping_test.go index a25b2d89158..703757fa23a 100644 --- a/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/aws/resource_aws_lambda_event_source_mapping_test.go @@ -1531,7 +1531,7 @@ EOF } resource "aws_sqs_queue" "test" { - name = %q + name = %q } resource "aws_lambda_function" "test" { From 2c2d9073b985dac083ec3dbd3a97ead55b5aaaff Mon Sep 17 00:00:00 2001 From: joyarackal Date: Wed, 2 Dec 2020 11:41:37 +0100 Subject: [PATCH 4/7] update documentation --- website/docs/r/lambda_event_source_mapping.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/lambda_event_source_mapping.html.markdown b/website/docs/r/lambda_event_source_mapping.html.markdown index fb58116772d..57689abf4fe 100644 --- a/website/docs/r/lambda_event_source_mapping.html.markdown +++ b/website/docs/r/lambda_event_source_mapping.html.markdown @@ -47,7 +47,7 @@ resource "aws_lambda_event_source_mapping" "example" { ## Argument Reference * `batch_size` - (Optional) The largest number of records that Lambda will retrieve from your event source at the time of invocation. Defaults to `100` for DynamoDB and Kinesis, `10` for SQS. -* `maximum_batching_window_in_seconds` - (Optional) The maximum amount of time to gather records before invoking the function, in seconds. Records will continue to buffer until either `maximum_batching_window_in_seconds` expires or `batch_size` has been met. Defaults to as soon as records are available in the stream. If the batch it reads from the stream only has one record in it, Lambda only sends one record to the function. +* `maximum_batching_window_in_seconds` - (Optional) The maximum amount of time to gather records before invoking the function, in seconds (between 0 and 300). Records will continue to buffer (or accumulate in the case of an SQS queue event source) until either `maximum_batching_window_in_seconds` expires or `batch_size` has been met. For streaming event sources, defaults to as soon as records are available in the stream. If the batch it reads from the stream/queue only has one record in it, Lambda only sends one record to the function. * `event_source_arn` - (Required) The event source ARN - can be a Kinesis stream, DynamoDB stream, or SQS queue. * `enabled` - (Optional) Determines if the mapping will be enabled on creation. Defaults to `true`. * `function_name` - (Required) The name or the ARN of the Lambda function that will be subscribing to events. From 4384fc8c9351497406dc0a14ee56a4e89568fc10 Mon Sep 17 00:00:00 2001 From: joyarackal Date: Wed, 2 Dec 2020 12:31:32 +0100 Subject: [PATCH 5/7] Update aws/resource_aws_lambda_event_source_mapping_test.go --- aws/resource_aws_lambda_event_source_mapping_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_lambda_event_source_mapping_test.go b/aws/resource_aws_lambda_event_source_mapping_test.go index 703757fa23a..ed05e3af631 100644 --- a/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/aws/resource_aws_lambda_event_source_mapping_test.go @@ -1492,7 +1492,7 @@ resource "aws_lambda_event_source_mapping" "lambda_event_source_mapping_test" { func testAccAWSLambdaEventSourceMappingConfigSQSBase(rName string) string { return fmt.Sprintf(` resource "aws_iam_role" "test" { - name = %q + name = %[1]q assume_role_policy = < Date: Wed, 2 Dec 2020 12:32:46 +0100 Subject: [PATCH 6/7] move MaximumBatchingWindowInSeconds into params instantiation --- aws/resource_aws_lambda_event_source_mapping.go | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/aws/resource_aws_lambda_event_source_mapping.go b/aws/resource_aws_lambda_event_source_mapping.go index f695ad28c3c..55e2426c92b 100644 --- a/aws/resource_aws_lambda_event_source_mapping.go +++ b/aws/resource_aws_lambda_event_source_mapping.go @@ -352,10 +352,11 @@ func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta inte log.Printf("[DEBUG] Updating Lambda event source mapping: %s", d.Id()) params := &lambda.UpdateEventSourceMappingInput{ - UUID: aws.String(d.Id()), - BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), - FunctionName: aws.String(d.Get("function_name").(string)), - Enabled: aws.Bool(d.Get("enabled").(bool)), + UUID: aws.String(d.Id()), + BatchSize: aws.Int64(int64(d.Get("batch_size").(int))), + FunctionName: aws.String(d.Get("function_name").(string)), + Enabled: aws.Bool(d.Get("enabled").(bool)), + MaximumBatchingWindowInSeconds: aws.Int64(int64(d.Get("maximum_batching_window_in_seconds").(int))), } // AWS API will fail if this parameter is set (even as default value) for sqs event source. Ideally this should be implemented in GO SDK or AWS API itself. @@ -364,8 +365,6 @@ func resourceAwsLambdaEventSourceMappingUpdate(d *schema.ResourceData, meta inte return fmt.Errorf("Error updating event source mapping: %s", err) } - params.MaximumBatchingWindowInSeconds = aws.Int64(int64(d.Get("maximum_batching_window_in_seconds").(int))) - if eventSourceArn.Service != "sqs" { if parallelizationFactor, ok := d.GetOk("parallelization_factor"); ok { params.SetParallelizationFactor(int64(parallelizationFactor.(int))) From e4c52ce2e5fcc0b031d9c4e0e9702ef9d5a59b83 Mon Sep 17 00:00:00 2001 From: joyarackal Date: Wed, 2 Dec 2020 12:34:45 +0100 Subject: [PATCH 7/7] rename TestAccAWSLambdaEventSourceMapping_BatchWindow -> TestAccAWSLambdaEventSourceMapping_KinesisBatchWindow --- aws/resource_aws_lambda_event_source_mapping_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_lambda_event_source_mapping_test.go b/aws/resource_aws_lambda_event_source_mapping_test.go index ed05e3af631..11eb857de2f 100644 --- a/aws/resource_aws_lambda_event_source_mapping_test.go +++ b/aws/resource_aws_lambda_event_source_mapping_test.go @@ -362,7 +362,7 @@ func TestAccAWSLambdaEventSourceMapping_StartingPositionTimestamp(t *testing.T) }) } -func TestAccAWSLambdaEventSourceMapping_BatchWindow(t *testing.T) { +func TestAccAWSLambdaEventSourceMapping_KinesisBatchWindow(t *testing.T) { var conf lambda.EventSourceMappingConfiguration rName := acctest.RandomWithPrefix("tf-acc-test") resourceName := "aws_lambda_event_source_mapping.test"