Repository: localstack/localstack

Issue:
bug: SQS queue - MaximumMessageSize parameter not being respected for individual messages in batch
### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

When I try to send an SQS message batch to localstack, it accept messages that surpasse the MaximumMessageSize limit (it's not the same issue as #6740).

```json
{
    "Successful": [
        {
            "Id": "a4cff0d1-1961-44bd-ae53-c6d5ed71ed08",
            "MessageId": "f4b6b57e-0f56-42a6-8d4b-df3b6d2d0bb0",
            "MD5OfMessageBody": "197c4e03eeff64b4e7bc4ad934e425f9"
        },
        {
            "Id": "35b535ed-b76a-4ebd-b749-6eb35cdb55ee",
            "MessageId": "57d98a94-e718-4a13-80d8-be35666c159d",
            "MD5OfMessageBody": "5fac72a66fb05539a171a91883dc60e4"
        }
    ]
}
```

### Expected Behavior

Localstack should fail the messages in the batch that are over the MaximumMessageSize parameter.
Below is what AWS returns when I issue the ```send-message-batch``` command against a real SQS queue.

```json
{
    "Successful": [
        {
            "Id": "a4cff0d1-1961-44bd-ae53-c6d5ed71ed08",
            "MessageId": "3bbcb7e2-ef54-4440-a467-1ee8019a282a",
            "MD5OfMessageBody": "197c4e03eeff64b4e7bc4ad934e425f9"
        }
    ],
    "Failed": [
        {
            "Id": "35b535ed-b76a-4ebd-b749-6eb35cdb55ee",
            "SenderFault": true,
            "Code": "InvalidParameterValue",
            "Message": "One or more parameters cannot be validated. Reason: Message must be shorter than 1024 bytes."
        }
    ]
}
```

### How are you starting LocalStack?

With a `docker run` command

### Steps To Reproduce

```shell
docker run -e "SERVICES=sqs" -e "DEFAULT_REGION=us-east-1" -p 4566:4566 localstack/localstack
```
First I create the queue with the parameter MaximumMessageSize as 1KiB using AWS CLI:
```shell
aws sqs create-queue --endpoint-url http://localhost:4566 --queue-name MyQueue --attributes "MaximumMessageSize=1024"
``` 
Then I send the message batch:

```shell
aws sqs send-message-batch --endpoint-url http://localhost:4566 --queue-url http://localhost:4566/000000000000/MyQueue --entries file://send-message-batch.json
```
This is the content of the send-message-batch.json file. Note that the second message surpasses 1KiB size limit in 1 byte (it has 1025 bytes).

```json
[
  {
    "Id": "a4cff0d1-1961-44bd-ae53-c6d5ed71ed08",
    "MessageBody": "abcd012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  },
  {
    "Id": "35b535ed-b76a-4ebd-b749-6eb35cdb55ee",
    "MessageBody": "abcde012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
  }
]
```


### Environment

```markdown
- OS:Ubuntu 22.04.2 LTS
- LocalStack:latest
```


### Anything else?

_No response_


Discussion:
Welcome to LocalStack! Thanks for reporting your first issue and our team will be working towards fixing the issue for you or reach out for more background information. We recommend joining our [Slack Community](https://localstack.cloud/contact/) for real-time help and drop a message to LocalStack Pro Support if you are a Pro user! If you are willing to contribute towards fixing this issue, please have a look at our [contributing guidelines](https://github.com/localstack/.github/blob/main/CONTRIBUTING.md) and our [contributing guide](https://docs.localstack.cloud/contributing/).
Hello 👋! It looks like this issue hasn’t been active in longer than five months. We encourage you to check if this is still an issue in the latest release. In the absence of more information, we will be closing this issue soon. If you find that this is still a problem, please feel free to provide a comment or upvote with a reaction on the initial post to prevent automatic closure. If the issue is already closed, please feel free to open a new one.
Hello! I've tried the latest version (3.0.3, if I'm not mistaken) and this issue is still there...

Resolving patch:
diff --git a/localstack/services/sqs/provider.py b/localstack/services/sqs/provider.py
--- a/localstack/services/sqs/provider.py
+++ b/localstack/services/sqs/provider.py
@@ -909,10 +909,6 @@ def send_message(
     ) -> SendMessageResult:
         queue = self._resolve_queue(context, queue_url=queue_url)
 
-        # Have to check the message size here, rather than in _put_message
-        # to avoid multiple calls for batch messages.
-        check_message_size(message_body, message_attributes, queue.maximum_message_size)
-
         queue_item = self._put_message(
             queue,
             context,
@@ -977,6 +973,15 @@ def send_message_batch(
                             SequenceNumber=queue_item.sequence_number,
                         )
                     )
+                except ServiceException as e:
+                    failed.append(
+                        BatchResultErrorEntry(
+                            Id=entry["Id"],
+                            SenderFault=e.sender_fault,
+                            Code=e.code,
+                            Message=e.message,
+                        )
+                    )
                 except Exception as e:
                     failed.append(
                         BatchResultErrorEntry(
@@ -1003,6 +1008,7 @@ def _put_message(
         message_deduplication_id: String = None,
         message_group_id: String = None,
     ) -> SqsMessage:
+        check_message_size(message_body, message_attributes, queue.maximum_message_size)
         check_message_content(message_body)
         check_attributes(message_attributes)
         check_attributes(message_system_attributes)


Test patch:
diff --git a/tests/aws/services/sqs/test_sqs.py b/tests/aws/services/sqs/test_sqs.py
--- a/tests/aws/services/sqs/test_sqs.py
+++ b/tests/aws/services/sqs/test_sqs.py
@@ -2059,6 +2059,22 @@ def test_send_batch_missing_deduplication_id_for_fifo_queue(
             aws_client.sqs.send_message_batch(QueueUrl=queue_url, Entries=message_batch)
         snapshot.match("test_missing_deduplication_id_for_fifo_queue", e.value.response)
 
+    @markers.aws.validated
+    @markers.snapshot.skip_snapshot_verify(paths=["$..Failed..Code", "$..Failed..Message"])
+    def test_send_batch_message_size(self, sqs_create_queue, snapshot, aws_client):
+        # message and code are slightly different because of the way SQS exception serialization works
+        queue_url = sqs_create_queue(
+            Attributes={
+                "MaximumMessageSize": "1024",
+            },
+        )
+        message_batch = [
+            {"Id": "a4cff0d1-1961-44bd-ae53-c6d5ed71ed08", "MessageBody": "a" * 1024},
+            {"Id": "35b535ed-b76a-4ebd-b749-6eb35cdb55ee", "MessageBody": "a" * 1025},
+        ]
+        response = aws_client.sqs.send_message_batch(QueueUrl=queue_url, Entries=message_batch)
+        snapshot.match("send-message-batch-result", response)
+
     @markers.parity.aws_validated
     def test_send_batch_missing_message_group_id_for_fifo_queue(
         self, sqs_create_queue, snapshot, aws_client
diff --git a/tests/aws/services/sqs/test_sqs.snapshot.json b/tests/aws/services/sqs/test_sqs.snapshot.json
--- a/tests/aws/services/sqs/test_sqs.snapshot.json
+++ b/tests/aws/services/sqs/test_sqs.snapshot.json
@@ -1380,5 +1380,31 @@
         }
       }
     }
+  },
+  "tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_send_batch_message_size": {
+    "recorded-date": "03-01-2024, 01:58:35",
+    "recorded-content": {
+      "send-message-batch-result": {
+        "Failed": [
+          {
+            "Code": "InvalidParameterValue",
+            "Id": "<uuid:1>",
+            "Message": "One or more parameters cannot be validated. Reason: Message must be shorter than 1024 bytes.",
+            "SenderFault": true
+          }
+        ],
+        "Successful": [
+          {
+            "Id": "<uuid:2>",
+            "MD5OfMessageBody": "c9a34cfc85d982698c6ac89f76071abd",
+            "MessageId": "<uuid:3>"
+          }
+        ],
+        "ResponseMetadata": {
+          "HTTPHeaders": {},
+          "HTTPStatusCode": 200
+        }
+      }
+    }
   }
 }
diff --git a/tests/aws/services/sqs/test_sqs.validation.json b/tests/aws/services/sqs/test_sqs.validation.json
--- a/tests/aws/services/sqs/test_sqs.validation.json
+++ b/tests/aws/services/sqs/test_sqs.validation.json
@@ -89,6 +89,9 @@
   "tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_receive_message_message_attribute_names_filters": {
     "last_validated_date": "2023-11-14T11:00:14+00:00"
   },
+  "tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_send_batch_message_size": {
+    "last_validated_date": "2024-01-03T01:58:49+00:00"
+  },
   "tests/aws/services/sqs/test_sqs.py::TestSqsProvider::test_send_batch_missing_deduplication_id_for_fifo_queue": {
     "last_validated_date": "2023-11-14T10:58:55+00:00"
   },