CloudFront is a Content Delivery Network (CDN). It takes the content from S3 bucket and transfers it to different edge locations. Let me explain how it works.
When a user tries to access a website and requests for some data
DNS routes your request to the nearest CloudFront edge location to serve the user request.
At edge location, CloudFront checks its cache for the requested files. If found, then returns it to the user otherwise-
- First CloudFront compares the request with the specifications and forwards it to the applicable origin server for the corresponding file type.
- The origin servers send the files back to the CloudFront edge location.
- As soon as the first byte arrives from the origin, CloudFront starts forwarding it to the user and adds the files to the cache in the edge location for the next time when someone again requests for the same file.