Zappa icon indicating copy to clipboard operation
Zappa copied to clipboard

Bad Request 400 on Boto Head Object operation during collectstatic when using zappa manage command

Open chrismaille opened this issue 1 year ago • 3 comments

Command:

zappa manage production "collectstatic --no-input"

Traceback:

[ERROR] ClientError: An error occurred (400) when calling the HeadObject operation: Bad Request
Traceback (most recent call last):
  File "/var/task/handler.py", line 657, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 244, in lambda_handler
    return handler.handler(event, context)
  File "/var/task/handler.py", line 454, in handler
    management.call_command(*event["manage"].split(" "))
  File "/tmp/onboard/django/core/management/__init__.py", line 194, in call_command
    return command.execute(*args, **defaults)
  File "/tmp/onboard/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/tmp/onboard/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
    collected = self.collect()
  File "/tmp/onboard/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "/tmp/onboard/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/tmp/onboard/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    if self.storage.exists(prefixed_path):
  File "/tmp/onboard/storages/backends/s3.py", line 586, in exists
    self.connection.meta.client.head_object(
  File "/var/lang/lib/python3.12/site-packages/botocore/client.py", line 569, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/lang/lib/python3.12/site-packages/botocore/client.py", line 1023, in _make_api_call
    raise error_class(parsed_response, operation_name)

The same command running without zappa works as expected: python manage.py collectstatic --no-input

chrismaille avatar Apr 11 '25 20:04 chrismaille

collectstatic collectsCollects the static files into the defiend STATIC_ROOT location. This typically is done on static server.

On lambda, you can use django-storages to serve static files from s3 buckets.
The error you have seems to suggest you may be using this. In which case you may not have the django-storages configured correclty.

You might want to try using whitenoise for serving static files to be cached by the client.

monkut avatar Apr 15 '25 01:04 monkut

Hi @monkut , thanks for the answer. The issue was in the s3.head_object call django-storages need to make during the collect static operation to check bucket info.

At first, I though the issue was in that library, but the collectionstatic --no-input command works on all scenarios except when called via zappa manage command.

(In fact, my workaround was create a new github actions workflow step installing the project and running the default collectstatic command.)

The whitenoise is a very good solution, but in my case, we need work the media files generated by this project on the S3.

chrismaille avatar Apr 16 '25 14:04 chrismaille

From zappa's perspective, it's passing the zappa manage production "collectstatic --no-input" command down to django, and django handles it from there.

You can see that the call is made out to django-storages:

  File "/tmp/onboard/storages/backends/s3.py", line 586, in exists
    self.connection.meta.client.head_object(

Where it's failing with the 400 call. I don't think zappa is causing an issue here. I suspect there's a django-storages configuration issue. The call above does an exist with prefixed_path. Maybe the resulting prefixed_path is None?

monkut avatar Apr 18 '25 00:04 monkut

@chrismaille were you able to fix the problem?

I am getting the same issue, I think it is correctly configured:

STORAGES = {
    "default": {
        "BACKEND": "storages.backends.s3.S3Storage",
        "OPTIONS": {
            "location": "media",
            "file_overwrite": False,
        },
    },
    "staticfiles": {
        "BACKEND": "storages.backends.s3.S3Storage",
        "OPTIONS": {
            "location": "static",
            "default_acl": "public-read",
        },
    },
}

The S3 bucket also exists I am not sure how to debug, zappa tail says nothing.

robertpro avatar Jul 07 '25 05:07 robertpro

@monkut I am able to run collect static from my own machine, and it uploads correctly to the S3 bucket, I am sharing the same configuration for lambda deployment and local development for this test, I am not sure how to debug this issue, this is what zappa tail is showing:

[1751906257820] [DEBUG] 2025-07-07T16:37:37.820Z da395c7e-9c05-4168-9c66-960c4f0d9d1d Zappa Event: {'manage': 'collectstatic --noinput'}
[1751906258336] LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a chan    raise error_class(parsed_response, operation_name)e/client.py", line 1074, in _make_api_callllectstatic.py", line 278, in delete_filenformation, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html   

And this is the output from the command:

❯ zappa manage staging "collectstatic --noinput"
[START] RequestId: da395c7e-9c05-4168-9c66-960c4f0d9d1d Version: $LATEST
[DEBUG] 2025-07-07T16:37:37.820Z da395c7e-9c05-4168-9c66-960c4f0d9d1d Zappa Event: {'manage': 'collectstatic --noinput'}
LAMBDA_WARNING: Unhandled exception. The most likely cause is an issue in the function code. However, in rare cases, a Lambda runtime update can cause unexpected function behavior. For functions using managed runtimes, runtime updates can be triggered by a function change, or can be applied automatically. To determine if the runtime has been updated, check the runtime version in the INIT_START log entry. If this error correlates with a change in the runtime version, you may be able to mitigate this error by temporarily rolling back to the previous runtime version. For more information, see https://docs.aws.amazon.com/lambda/latest/dg/runtimes-update.html
[ERROR] ClientError: An error occurred (400) when calling the HeadObject operation: Bad Request
Traceback (most recent call last):
  File "/var/task/handler.py", line 769, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 244, in lambda_handler
    return handler.handler(event, context)
  File "/var/task/handler.py", line 454, in handler
    management.call_command(*event["manage"].split(" "))
  File "/var/lang/lib/python3.12/site-packages/django/core/management/__init__.py", line 194, in call_command
    return command.execute(*args, **defaults)
  File "/var/lang/lib/python3.12/site-packages/django/core/management/base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "/var/lang/lib/python3.12/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 209, in handle
    collected = self.collect()
  File "/var/lang/lib/python3.12/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 135, in collect
    handler(path, prefixed_path, storage)
  File "/var/lang/lib/python3.12/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 368, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/var/lang/lib/python3.12/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    if self.storage.exists(prefixed_path):
  File "/var/lang/lib/python3.12/site-packages/storages/backends/s3.py", line 586, in exists
    self.connection.meta.client.head_object(
  File "/var/lang/lib/python3.12/site-packages/botocore/client.py", line 601, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/lang/lib/python3.12/site-packages/botocore/context.py", line 123, in wrapper
    return func(*args, **kwargs)
  File "/var/lang/lib/python3.12/site-packages/botocore/client.py", line 1074, in _make_api_call
    raise error_class(parsed_response, operation_name)
[END] RequestId: da395c7e-9c05-4168-9c66-960c4f0d9d1d
[REPORT] RequestId: da395c7e-9c05-4168-9c66-960c4f0d9d1d
Duration: 518.10 ms
Billed Duration: 519 ms
Memory Size: 512 MB
Max Memory Used: 177 MB

Any ideas?

robertpro avatar Jul 07 '25 16:07 robertpro

Hi there! Unfortunately, this Issue has not seen any activity for at least 90 days. If the Issue is still relevant to the latest version of Zappa, please comment within the next 10 days if you wish to keep it open. Otherwise, it will be automatically closed.

github-actions[bot] avatar Oct 05 '25 17:10 github-actions[bot]

Hi there! Unfortunately, this Issue was automatically closed as it had not seen any activity in at least 100 days. If the Issue is still relevant to the latest version of Zappa, please open a new Issue.

github-actions[bot] avatar Oct 15 '25 17:10 github-actions[bot]