I'm creating a service where a client needs to get some data stored in a DynamoDB table. I'm using Cloudfront as a CDN for reducing latency. I have 2 methods of obtaining the data from the DynamoDB table.
Method 1: The client sends a request to Cloudfront. Cloudfront checks for a cached response. Assuming a cached response is not available, Cloudfront invokes a Lambda@Edge function with an Origin Request trigger. The Lambda@Edge function queries the DynamoDB table, gets the data and sends it to the Cloudfront edge location. Cloudfront caches the response from the Lambda@Edge function and responds to the client. Question: what should the Cloudfront origin be set in this case?
Method 2: Client sends a request to Cloudfront. Cloudfront checks for cached response. Assuming a cached response is not available, Cloudfront forwards the request to the origin, which in this case will be a Lambda function. The Lambda receives the Cloudfront request and queries the DynamoDB table, gets the data and sends it back to the Cloudfront distribution. Cloudfront edge server caches the response, and forwards it to the client.
Which method would be better in terms of latency, and in terms of cost. Also is there a better way to solve this problem?