title: Lambda batch item failures not working with Terraform
body: <p>I'm trying to use the option to return just the failed messages back to the SQS for re-processing with the <a href="https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#:%7E:text=%22USER_INITIATED%22%0A%7D-,Reporting%20batch%20item%20failures,-When%20your%20Lambda" rel="nofollow noreferrer">Reporting batch item failures feature</a>.</p>
  <p>The Lambda terraform code:</p>
  <pre><code>module &quot;lambda&quot; {
    source  = &quot;xxxx&quot;
    version = &quot;0.0.0&quot;
  
    lambda_function_name    = &quot;${local.basename}-lambda&quot;
    lambda_function_handler = &quot;lambda.handler&quot;
    lambda_payload_path     = var.lambda_payload_path
    role                    = aws_iam_role.xxxxxxxx.arn
    timeout                 = var.lambda_timeout
    memory_size             = var.lambda_memory
  }
  
  resource &quot;aws_lambda_event_source_mapping&quot; &quot;trigger&quot; {
    event_source_arn = aws_sqs_queue.queue.arn
    function_name    = module.lambda.lambda_arn
    function_response_types = [&quot;ReportBatchItemFailures&quot;]
  }
  </code></pre>
  <p>The SQS terraform code:</p>
  <pre><code>resource &quot;aws_sqs_queue&quot; &quot;queue&quot; {
    name              = &quot;${lower(local.basename)}&quot;
    kms_master_key_id = var.kms_sqs
    visibility_timeout_seconds = 300
  }
  
  resource &quot;aws_sqs_queue_policy&quot; &quot;allow-s3-notification-policy&quot; {
    queue_url = aws_sqs_queue.queue.id
  
    policy = &lt;&lt;POLICY
  {
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
      {
        &quot;Effect&quot;: &quot;Allow&quot;,
        &quot;Principal&quot;: &quot;*&quot;,
        &quot;Action&quot;: &quot;sqs:SendMessage&quot;,
        &quot;Resource&quot;: &quot;${aws_sqs_queue.queue.arn}&quot;,
        &quot;Condition&quot;: {
          &quot;ArnEquals&quot;: { &quot;aws:SourceArn&quot;: &quot;${aws_s3_bucket.bucket.arn}&quot; }
        }
      }
    ]
  }
  POLICY
  }
  </code></pre>
  <p>This is my Lambda handler Typescript code that returns the response back to the SQS:</p>
  <pre><code>const batchItemFailures = [];
  const batchItemFailures: SQSBatchItemFailure[] = [];
  if(failedMessageIds.length === 0){
     return {&quot;batchItemFailures&quot;: batchItemFailures};
  } else if(records.length === failedMessageIds.length){
      console.error(`Failed to handle messages: ${failedMessageIds}: ${JSON.stringify(records)}`);
      batchItemFailures.push({ itemIdentifier: null });
      return {&quot;batchItemFailures&quot;: batchItemFailures};
  } 
  
  const failedMessages = records.filter(record =&gt; failedMessageIds.includes(record.messageId));
  failedMessageIds.forEach(id =&gt; batchItemFailures.push({ itemIdentifier: id }));
  console.error(`Failed to handle messages: ${failedMessageIds}: ${JSON.stringify(failedMessages)}`);
  return {&quot;batchItemFailures&quot;: batchItemFailures};
  </code></pre>
  <p>On the other hand, when I'm throwing a regular error to the queue, it re-process all the batch over again.</p>
  <p>Any ideas?</p>