silvermine / serverless-plugin-cloudfront-lambda-edge

Adds Lambda@Edge support to Serverless
MIT License
296 stars 41 forks source link

What values for the Cloudfront resource in serverless.yml? #37

Closed gregmsanderson closed 5 years ago

gregmsanderson commented 5 years ago

Hello,

I have just been reading this in order to trigger a Lambda function as Lambda@Edge (so, from Cloudfront, rather than the normal API Gateway http request).

However I'm not sure what values to put for that Cloudfront distribution, in the resources. Particularly the Origin value. Since it will need to have one, presumably, however your example serverless yml only lists one for a S3 bucket. That makes total sense for that usage. However I want to have a Lambda which has no bucket or static backend. It's just a function, that runs on request. So what would I set as the Origin for the Cloudfront distribution?

I did wonder whether you are supposed to set the Origin as the API Gateway-style https:// ... URL? But if so, don't you defeat the point of Lambda@Edge as you'd incur API Gateway costs as the request gets passed along on a cache miss (as all mine would be)?

Therefore I'm assuming there is some kind of syntax where you can tell Cloudfront to use a Lambda directly as its Origin - the question is how ...

Thanks!

jthomerson commented 5 years ago

CloudFront can not use Lambda as an "origin". There are two services that allow you to have a Lambda origin: API Gateway and Application Load Balancer. (for more info, you can see a blog post I wrote).

But, you can configure a Lambda@Edge function to make responses (for viewer request and origin request type invocations), so that the request is handled entirely with CloudFront and Lambda@Edge, without going to any origin. So, that's kind of like an origin - I think it's your intent - but not technically an origin. See the CloudFront Lambda@Edge docs for how to generate responses.

In any event, you'll still need an origin, even if 100% of your CloudFront requests are handled by Lambda@Edge. Remember that CloudFront's entire purpose is to provide an edge distribution for an origin. So, that's why it requires one. You can easily just set up an empty S3 bucket as your origin and never put anything in it if Lambda@Edge will serve all your responses.

In any event, none of that has to do with this plugin, so closing this "issue".