Moving Static Assets Out of Lambda Functions
Here, you'll learn how to write code to move static assets out of Lambda functions.
We'll cover the following...
Fetching static assets #
Traditional web applications bundle code and assets, and web servers are responsible for sending both to client devices. Translated to the Lambda world, that would mean including client-side JavaScript and web assets in a Lambda function, and creating at least another API endpoint and a new function to send those files to clients on demand.
Serving application contents such as images, style sheets, client-side JavaScript and HTML files through a Lambda function is a bad idea. Those files are typically public and require no authorisation. They do not change depending on individual users or requests. Paying for a Lambda function and an API call for every file request would increase operational costs significantly and introduce two unnecessary intermediaries between the user and file contents, increasing latency and degrading the user experience.
With serverless applications, it’s much more usual to put static assets somewhere for clients to fetch them directly, for example on S3. In fact, this is so common that S3 can pretend to be a web server. It even offers some basic web serving ...