0

I want to have multiple (based on stage param) AWS cloudfront distribution. So if --stage dev passed it would deploy a different CloudFront dist than --stage qa. Each stage should update/create a distribution based on the stage params.

Is it possible to achieve with serverless.yml?

I have found the following plugin serverless-api-cloudfront , not sure it will create additional distribution if domain name is changed, and not sure it is possible to add origins with this plugin.

Thanks

Alex Portnoy
  • 224
  • 4
  • 8

1 Answers1

1

Is it possible to achieve with serverless.yml?

Yes. Yes it's possible and it's already built in. Your config will be simpler if you use the plugin serverless-api-cloudfront (rather than declaring a AWS::CloudFront::Distribution resource yourself). You will get one distribution-per-stage, without having to jump through any hoops.

** EDIT

Expanding on my original post: It does seem that the serverless-api-cloudfront plugin's README is lacking in usage examples (at the time of this writing). I do suggest that anyone reading this post should go to the plugin's github and help the author by PR'ing a better README.

Here's a link to a blog article I found that shows an example of how to write up a AWS::CloudFront::Distribution directly in your serverless.yml. It's only 1 resource, so you may not even want to depend on a plugin anyway. https://medium.com/yld-blog/caching-in-with-cloudfront-using-serverless-5a174651ab14

solsglasses
  • 177
  • 5
  • thanks! but how can I add origins and behaviours to this distribution? not much documentation there? – Alex Portnoy Nov 24 '19 at 13:45
  • @AlexPortnoy updated my original answer, but TL;DR go to this link for inspiration on configuring a `AWS::CloudFront::Distribution` directly in your serverless.yml without any plugins. https://medium.com/yld-blog/caching-in-with-cloudfront-using-serverless-5a174651ab14 – solsglasses Nov 27 '19 at 16:44
  • I also noticed that the plugin does create an api-gateway with the distribution. That I do not need / want. But the blog is great! – Alex Portnoy Nov 28 '19 at 17:27