node.jsamazon-web-servicesamazon-cloudfrontserverless-frameworkserverless-plugins

Serverless Framework - deploying multiple AWS cloudfront distributions


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


Solution

  • 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