Search code examples
amazon-web-servicesaws-lambdaaws-api-gatewayamazon-cloudfrontcdn

What gets cached if I use AWS Cloudfront in front of AWS API Gateway that is integrated with AWS Lambda Function?


i heard that cloudfront caches origin response to reduce latency, so does it mean that the lambda function won't get invoked each time there is a request because the response has already been cached? i don't think that that's the case because then what's the use of having lambda function if its logic is not executed on each request. So what exactly is cached in this case?


Solution

  • i heard that cloudfront caches origin response to reduce latency, so does it mean that the lambda function won't get invoked each time there is a request because the response has already been cached?

    Yes that is the entire purpose of a CDN like CloudFront. Imagine you have a website with lots of pages that don't change very often. If thousands of people hit that same page on your website, CloudFront could call your server once to get the page contents, and then serve it to those thousands of people without having to go back to your web server again, thus reducing load on your server.

    i don't think that that's the case because then what's the use of having lambda function if its logic is not executed on each request. So what exactly is cached in this case?

    Obviously you would not enable this if you want your Lambda function to be called for each request. It is up to you to configure a cache like CloudFront. If you don't want your backend responses to be cached, then configure it accordingly, or just don't use it at all.