Skip to content

Commit

Permalink
Merge pull request hashicorp#40322 from amardatar/f-aws_lambda_event_…
Browse files Browse the repository at this point in the history
…source_mapping-metrics_config

Adds `metrics_config` to aws_lambda_event_source_mapping
  • Loading branch information
ewbankkit authored Nov 27, 2024
2 parents 908d232 + 4622d3d commit 398878c
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/40322.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_lambda_event_source_mapping: Add `metrics_config` argument
```
65 changes: 65 additions & 0 deletions internal/service/lambda/event_source_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,24 @@ func resourceEventSourceMapping() *schema.Resource {
Computed: true,
ValidateFunc: validation.IntBetween(-1, 10_000),
},
"metrics_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"metrics": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateDiagFunc: enum.Validate[awstypes.EventSourceMappingMetric](),
},
},
},
},
},
"parallelization_factor": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -447,6 +465,10 @@ func resourceEventSourceMappingCreate(ctx context.Context, d *schema.ResourceDat
input.MaximumRetryAttempts = aws.Int32(int32(v.(int)))
}

if v, ok := d.GetOk("metrics_config"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.MetricsConfig = expandEventSourceMappingMetricsConfig(v.([]interface{})[0].(map[string]interface{}))
}

if v, ok := d.GetOk("parallelization_factor"); ok {
input.ParallelizationFactor = aws.Int32(int32(v.(int)))
}
Expand Down Expand Up @@ -584,6 +606,13 @@ func resourceEventSourceMappingRead(ctx context.Context, d *schema.ResourceData,
d.Set("maximum_batching_window_in_seconds", output.MaximumBatchingWindowInSeconds)
d.Set("maximum_record_age_in_seconds", output.MaximumRecordAgeInSeconds)
d.Set("maximum_retry_attempts", output.MaximumRetryAttempts)
if v := output.MetricsConfig; v != nil {
if err := d.Set("metrics_config", []interface{}{flattenEventSourceMappingMetricsConfig(v)}); err != nil {
return sdkdiag.AppendErrorf(diags, "setting metrics_config: %s", err)
}
} else {
d.Set("metrics_config", nil)
}
d.Set("parallelization_factor", output.ParallelizationFactor)
d.Set("queues", output.Queues)
if v := output.ScalingConfig; v != nil {
Expand Down Expand Up @@ -701,6 +730,14 @@ func resourceEventSourceMappingUpdate(ctx context.Context, d *schema.ResourceDat
input.MaximumRetryAttempts = aws.Int32(int32(d.Get("maximum_retry_attempts").(int)))
}

if d.HasChange("metrics_config") {
if v, ok := d.GetOk("metrics_config"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.MetricsConfig = expandEventSourceMappingMetricsConfig((v.([]interface{})[0].(map[string]interface{})))
} else {
input.MetricsConfig = &awstypes.EventSourceMappingMetricsConfig{}
}
}

if d.HasChange("parallelization_factor") {
input.ParallelizationFactor = aws.Int32(int32(d.Get("parallelization_factor").(int)))
}
Expand Down Expand Up @@ -1305,3 +1342,31 @@ func flattenScalingConfig(apiObject *awstypes.ScalingConfig) map[string]interfac

return tfMap
}

func expandEventSourceMappingMetricsConfig(tfMap map[string]interface{}) *awstypes.EventSourceMappingMetricsConfig {
if tfMap == nil {
return nil
}

apiObject := &awstypes.EventSourceMappingMetricsConfig{}

if v, ok := tfMap["metrics"].(*schema.Set); ok && v.Len() > 0 {
apiObject.Metrics = flex.ExpandStringyValueSet[awstypes.EventSourceMappingMetric](v)
}

return apiObject
}

func flattenEventSourceMappingMetricsConfig(apiObject *awstypes.EventSourceMappingMetricsConfig) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}

if v := apiObject.Metrics; v != nil {
tfMap["metrics"] = flex.FlattenStringyValueSet(v)
}

return tfMap
}
65 changes: 65 additions & 0 deletions internal/service/lambda/event_source_mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func TestAccLambdaEventSourceMapping_Kinesis_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "function_response_types.#", "0"),
resource.TestCheckResourceAttr(resourceName, names.AttrKMSKeyARN, ""),
acctest.CheckResourceAttrRFC3339(resourceName, "last_modified"),
resource.TestCheckResourceAttr(resourceName, "metrics_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "tumbling_window_in_seconds", "0"),
),
},
Expand Down Expand Up @@ -1292,6 +1293,48 @@ func TestAccLambdaEventSourceMapping_documentDB(t *testing.T) {
})
}

func TestAccLambdaEventSourceMapping_SQS_metricsConfig(t *testing.T) {
ctx := acctest.Context(t)
var v lambda.GetEventSourceMappingOutput
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_lambda_event_source_mapping.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, names.LambdaServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckEventSourceMappingDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccEventSourceMappingConfig_sqsMetricsConfig1(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckEventSourceMappingExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metrics_config.0.metrics.0", "EventCount"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"last_modified"},
},
{
Config: testAccEventSourceMappingConfig_sqsMetricsConfig2(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckEventSourceMappingExists(ctx, resourceName, &v),
resource.TestCheckResourceAttr(resourceName, "metrics_config.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"last_modified"},
},
},
})
}

func testAccPreCheckMQ(ctx context.Context, t *testing.T) {
conn := acctest.Provider.Meta().(*conns.AWSClient).MQClient(ctx)

Expand Down Expand Up @@ -2673,3 +2716,25 @@ resource "aws_lambda_event_source_mapping" "test" {
}
`)
}

func testAccEventSourceMappingConfig_sqsMetricsConfig1(rName string) string {
return acctest.ConfigCompose(testAccEventSourceMappingConfig_sqsBase(rName), `
resource "aws_lambda_event_source_mapping" "test" {
event_source_arn = aws_sqs_queue.test.arn
function_name = aws_lambda_function.test.arn
metrics_config {
metrics = ["EventCount"]
}
}
`)
}

func testAccEventSourceMappingConfig_sqsMetricsConfig2(rName string) string {
return acctest.ConfigCompose(testAccEventSourceMappingConfig_sqsBase(rName), `
resource "aws_lambda_event_source_mapping" "test" {
event_source_arn = aws_sqs_queue.test.arn
function_name = aws_lambda_function.test.arn
}
`)
}
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ class MyConvertedCode(TerraformStack):
* `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. Only available for stream sources (DynamoDB and Kinesis) and SQS standard queues.
* `maximum_record_age_in_seconds`: - (Optional) The maximum age of a record that Lambda sends to a function for processing. Only available for stream sources (DynamoDB and Kinesis). Must be either -1 (forever, and the default value) or between 60 and 604800 (inclusive).
* `maximum_retry_attempts`: - (Optional) The maximum number of times to retry when the function returns an error. Only available for stream sources (DynamoDB and Kinesis). Minimum and default of -1 (forever), maximum of 10000.
* `metrics_config`: - (Optional) CloudWatch metrics configuration of the event source. Only available for stream sources (DynamoDB and Kinesis) and SQS queues. Detailed below.
* `parallelization_factor`: - (Optional) The number of batches to process from each shard concurrently. Only available for stream sources (DynamoDB and Kinesis). Minimum and default of 1, maximum of 10.
* `queues` - (Optional) The name of the Amazon MQ broker destination queue to consume. Only available for MQ sources. The list must contain exactly one queue name.
* `scaling_config` - (Optional) Scaling configuration of the event source. Only available for SQS queues. Detailed below.
Expand Down Expand Up @@ -289,6 +290,10 @@ class MyConvertedCode(TerraformStack):

* `pattern` - (Optional) A filter pattern up to 4096 characters. See [Filter Rule Syntax](https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html#filtering-syntax).

### metrics_config Configuration Block

* `metrics` - (Required) A list containing the metrics to be produced by the event source mapping. Valid values: `EventCount`.

### scaling_config Configuration Block

* `maximum_concurrency` - (Optional) Limits the number of concurrent instances that the Amazon SQS event source can invoke. Must be greater than or equal to `2`. See [Configuring maximum concurrency for Amazon SQS event sources](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#events-sqs-max-concurrency). You need to raise a [Service Quota Ticket](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html) to increase the concurrency beyond 1000.
Expand Down
5 changes: 5 additions & 0 deletions website/docs/r/lambda_event_source_mapping.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ resource "aws_lambda_event_source_mapping" "example" {
* `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. Only available for stream sources (DynamoDB and Kinesis) and SQS standard queues.
* `maximum_record_age_in_seconds`: - (Optional) The maximum age of a record that Lambda sends to a function for processing. Only available for stream sources (DynamoDB and Kinesis). Must be either -1 (forever, and the default value) or between 60 and 604800 (inclusive).
* `maximum_retry_attempts`: - (Optional) The maximum number of times to retry when the function returns an error. Only available for stream sources (DynamoDB and Kinesis). Minimum and default of -1 (forever), maximum of 10000.
* `metrics_config`: - (Optional) CloudWatch metrics configuration of the event source. Only available for stream sources (DynamoDB and Kinesis) and SQS queues. Detailed below.
* `parallelization_factor`: - (Optional) The number of batches to process from each shard concurrently. Only available for stream sources (DynamoDB and Kinesis). Minimum and default of 1, maximum of 10.
* `queues` - (Optional) The name of the Amazon MQ broker destination queue to consume. Only available for MQ sources. The list must contain exactly one queue name.
* `scaling_config` - (Optional) Scaling configuration of the event source. Only available for SQS queues. Detailed below.
Expand Down Expand Up @@ -203,6 +204,10 @@ resource "aws_lambda_event_source_mapping" "example" {

* `pattern` - (Optional) A filter pattern up to 4096 characters. See [Filter Rule Syntax](https://docs.aws.amazon.com/lambda/latest/dg/invocation-eventfiltering.html#filtering-syntax).

### metrics_config Configuration Block

* `metrics` - (Required) A list containing the metrics to be produced by the event source mapping. Valid values: `EventCount`.

### scaling_config Configuration Block

* `maximum_concurrency` - (Optional) Limits the number of concurrent instances that the Amazon SQS event source can invoke. Must be greater than or equal to `2`. See [Configuring maximum concurrency for Amazon SQS event sources](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#events-sqs-max-concurrency). You need to raise a [Service Quota Ticket](https://docs.aws.amazon.com/general/latest/gr/aws_service_limits.html) to increase the concurrency beyond 1000.
Expand Down

0 comments on commit 398878c

Please sign in to comment.