Just a quick question: Can Varnish cache S3 objects? Or it only works for static files on the server?
Varnish will cache files that are served through it (provided it is setup to do so).
A typical setup will have a backend server provide content, and Varnish setup as a caching layer in front of the server. In this way, Varnish can be configured to cache both static and dynamic content.
In the case of S3, it varies. If you serve your content directly from S3 (i.e. the URL for your resource points to S3) then Varnish is not involved and will not cache the content.
On the other hand, if your server accesses the S3 files locally (e.g. your application reads the file or you use something such as s3fs to mount an S3 bucket locally) and serves the files from it, then Varnish can be used to cache the files.
Arguably though, the question of purpose comes to mind. Serving from S3 should be much more scalable than attempting to implement your own caching using Varnish. The one advantage might be saving a DNS lookup (so possibly good for a small number of small items). (A better caching alternative might be Cloudfront, but for few/small files S3 often performs better).