AWS Bites Podcast

66. AWS SAM v Serverless Framework

Discover the Ultimate Battle: Serverless Framework vs AWS SAM!

Are you building and deploying serverless applications and don't know which tool to choose? Look no further, as we dive into a comparison of the two heavyweights in the serverless world - AWS SAM and Serverless Framework. Find out their unique features, ease of use, and what the future holds for these Infrastructure as Code (IaC) tools. By the end of this episode, you will know which one is right for you and your projects!

Join us as we explore the pros and cons of each tool, from the flexibility and ease of use of Serverless Framework to the cloud-side deployment management of SAM. Learn about the different syntax options, supported languages, and credentials management (especially SSO).

Get the inside scoop on the installation process and build and deployment capabilities, including the new "sam accelerate" feature for faster development. Discover the difference between handling multiple components and stacks and how each tool keeps up with new AWS features.

Don't miss out on this exciting episode as we determine the winner in the ultimate battle of Serverless Framework vs AWS SAM!

AWS Bites is sponsored by fourTheorem, an AWS Consulting Partner offering training, cloud migration, and modern application architecture.

In this episode, we mentioned the following resources:

Let's talk!

Do you agree with our opinions? Do you have interesting AWS questions you'd like us to chat about? Leave a comment on YouTube or connect with us on:

00:01:21

CloudFormation is very explicit and it can be very verbose. So, with Serverless Framework and SAM, you're providing a more concise user-friendly interface that's just a little bit more implicit and provides a lot of shortcuts and defaults for you. They are designed, I would say, to make the process of writing FaaS functions, so Lambda functions easier, but they also provide some convenient wrappers for APIs and step functions and things like that. And they also not just allow you to write the infrastructure as code template in an easier way, but they provide certain tools for building, packaging, deployment and local invocation as well. Doing all that stuff manually is a lot of work. So, maybe at this point, we can just give a quick run through what are the highlights of Serverless Framework, what are the highlights of SAM, and then we can dive into some of the, I suppose, the more detailed differences between the two. So, let's talk about Serverless Framework and for many, this is still the original and best. It was very popular when it first came out and it was probably, I would say, very instrumental in helping many people get excited and start adopting serverless in the first place. I would say the community, all of us serverless developers owe quite a lot to it and to the maintainers. It supports multiple cloud providers, which sometimes we forget about, especially if you're working in AWS all the time, but it has backends for Azure and Alibaba cloud and Cloudflare as well. So, you can deploy to multiple cloud providers with it.

00:04:59

And this is not something you can work around. It's just the way that the framework is designed. While SLS will give you ways to do that if you need to. And there are other differences that go in this direction. For instance, a lot of things in serverless are done through plugins. So, there is a natural approach to just build a plugin and you can extend with additional functionality if you want to. It's not as easy to write plugins in SAM. And we will talk a little bit more about that as well. Another big difference is the template syntax. Even though they both use mainly YAML, or at least by default YAML is the main thing you would use to write the SAM templates or the Serverless Framework templates, I would say the Serverless Framework is, again, way more flexible than SAM. In Serverless Framework templates, you have a very expressive sublanguage. Like, you can nest expressions into the YAML. And these expressions can basically allow you to interpret data from different sources. And one of the most impressive ones, and you can just say, get data from this JavaScript file. And what happens behind the scenes is that it's going to literally invoke the JavaScript file so you can build your own custom logic. And then whatever the JavaScript file is exporting is the data that gets interpolated in your own template. So, that's one of the easiest ways that I've seen in any system to basically, in any configuration-based system to allow you to write your own dynamic code whenever you need something a little bit more bespoke. And what's even more interesting is that in recent versions of Serverless Framework, you can even avoid YAML entirely. And you can write the configuration template in JavaScript itself or even in TypeScript if you want to. And that opens up for even more dynamic scenarios, because of course that's JavaScript code that gets interpreted when the template is executed.

00:12:42

Build and deployment might be another interesting topic and in this case I think SAM is probably a little bit ahead than Serverless Framework because the first thing is that SAM uses CloudFormation change sets and this is something that serverless didn't do for a long time, only recently they added support for that but there are important differences there. If you don't know, CloudFormation change set is basically a feature of CloudFormation that allows you to, rather than just deploying the differences in a template, it will basically do a preview for you, it's basically going to assess what is going to change and then it's something that you can effectively decide I want to go ahead and deploy this or maybe you realize it's not really doing what you expected so you can just stop that particular deployment. So with SAM, basically in your deployment process through SAM it will very obviously show exactly the effect of this change set so it's very easy for you to visualize that something has changed which is actually going to be deployed and decide whether you want to proceed or not. With SLS that kind of happens as well but a little bit more behind the scenes so you'll need to go into the AWS console to actually see what's going to be the effect of the change set so it's a little bit more involved while I think the experience with SAM is much smoother and user-friendly. Another interesting thing in terms of building is that SAM uses Docker out of the box which can be very convenient when in your project you have native dependencies for instance in Python or Node.js. If you are working on a Mac machine or a Windows machine probably the local files that you have for these native dependencies are not going to work out of the box in a Lambda environment which is a Linux-based environment and maybe with a very specific architecture that might be different from the one in your particular development machine. So when you use Docker the build process is actually done in an environment that is going to be much closer to the environment of Lambda so the risk of packaging something that is not going to work in the target architecture is much more reduced. So this is very convenient. You could do the same thing with serverless but it's not really a well-known feature because you'll need to explicitly pass a flag to enable that. Another final point about deployment which is really interesting I think is one of the most innovative features of SAM and one of the strong points of SAM is a feature called SAM Accelerate which is relatively new. We mentioned it in one of our previous episodes, episode 19 where we talk about the serverless development experience. So basically SAM Accelerate is something that is meant to reduce the feedback loop that you have when you are developing a new feature. You do some changes and in order to see if these changes in your code actually do what you are expecting them to do you probably need to do a deployment and test it in the cloud and this is generally something that takes minutes so it's kind of interrupting your development loop. It's distracting and it might be frustrating if you have to do it over and over. What SAM Accelerate does is basically automatically syncing your code as you do changes. You lose some of the guarantees like you are not for instance able to do rollbacks as easily as you would do with a full deployment but it gives you a very quick feedback loop to see these changes almost in real time so it might be really a big accelerator in that development life cycle. So definitely a really interesting feature that if you haven't tried yet I really recommend you to try because you can have that feeling that you are developing locally while almost immediately being able to see the changes reflected in a real cloud environment. What do you think? Is there anything else worth mentioning in the difference between SLS and SAM?

00:18:21

When it comes to permissions, getting permissions right is always a tricky thing about building AWS applications and serverless applications. Now SAM has a few things that are attempts at making this easier. One is they have some pre-baked policy templates. So these are just ways of kind of reducing the amount of boilerplate you have to write for IAM. And the other one is a new feature called SAM connectors and this is when you've got this kind of pseudo resource called our connector that you create and it allows you to say for this DynamoDB table give access to this function and make it give it write access or read access. So the idea is to make it a little bit more understandable I suppose for people who don't understand IAM. I'm kind of in two minds about it really I'm not sure I like any of these things. I think when it comes to IAM it's much better to be explicit even if it means more verbose syntax and just understand what you're doing and be very fine-grained. With the Serverless Framework you've got a nice way to specify IAM statements for functions especially if you add in the very common IAM roles per function plugin. So I would much favor that approach to it and not to try and dumb it down and provide this kind of half solution to simplifying IAM. Let's talk you mentioned JavaScript so I might just talk about transpilation. If we've got esbuild that's supported in SAM already for TypeScript and JavaScript new versions of the language so the Serverless Framework you can add the serverless esbuild plugin and it works too. So you're generally set for both of them and then both of them provide local development experience so you can do local invoke on both of them and you can use a local API gateway simulation. The only difference with Serverless Framework is that the API simulation is provided with a plugin serverless offline where SAM runs it locally for you automatically and then I suppose there's a big standout one with CI-CD for SAM and I really like this one now because it allows you to basically bootstrap a complete continuous integration pipeline and deployment pipeline using any of a large set of providers so CodeBuild and CodePipeline within AWS but it can also support Jenkins or GitLab CI-CD, Bitbucket and GitHub Actions. So this will create a pipeline which will deploy to multiple environments and also supports feature branch deployments. We had an episode a while back about OIDC providers and why you should use that method for getting your GitHub Actions to authenticate with AWS. SAM will create that for you so all of the complexity of creating that and creating the associated role is managed when you bootstrap your SAM pipeline so that's really nice one. And maybe just lastly before we start wrapping up and giving our more opinionated view on which you should choose when it comes to just extensibility you mentioned the plugin ecosystem in Serverless piano and we can give an example because you and I and a few other the fourTheorem team are working on a really significant plugin for Serverless Framework which now supports SAM as well it's called SLIC Watch and it's for adding alarms and dashboards into these projects automatically. It has to basically look at your whole template find out what functions API step functions DynamoDB tables you have and creates alarms and dashboards for you. When it comes to Serverless Framework it's pretty easy to write a plugin. The documentation is a bit hairy but there's enough examples out there where you can figure out how to use it. When it comes to SAM your only option really is to create another CloudFormation macro and then I suppose that's in it's nice in some ways because it's similar syntax to SAM itself you just add another macro and CloudFormation service is going to do that transformation for you but the thing is deploying macros into people's accounts it takes a bit of work a little bit more work than just adding a serverless plugin because you have to deploy them we can't just publish a macro to the public registry in CloudFormation you can't do that only AWS can do that so it's a pity we can't do that and it just makes it a little bit more of a step but once you do that it works the same you know we can use 90 of the same code to deliver to deliver a slick watch whether you're using Serverless Framework or SAM because the macros are just implemented as Lambda functions and we can run them as JavaScript, same JavaScript code, so I think we've covered fairly exhaustively all the differences between these two tools so maybe this is the part that people really want to know which should they use which direct which which one is the one that people should choose for the project today and what's the future look like, where are they going?

Luciano
00:22:58

Yeah that's that's definitely I know opinionated piece but we're going to try to bring some evidence into the answer like we're not going to say pick one or the other but we are just going to give you some indications like if you favor a specific thing maybe one tool is better than the other so definitely I like the sum generation of pipelines that you mentioned this is a really amazing feature it is always quite painful to generate pipeline codes and then testing it you probably end up doing millions of deployments just trying to figure out is my syntax correct does it really do what it wants it to do so the opportunity to just bootstrap something that it's reasonably doing most of that work and for different kind of cicds it's an amazing feature so if that's something that you really care about and something that you have been spending a lot of time in the past probably going with some and leveraging this feature can be a big enabler for you and your team if you are targeting other cloud providers not just AWS that's an argument for for Serverless Framework because of course some doesn't even try to support other cloud providers so your only option really is to use Serverless Framework there between the two so again worth remarking that your code and your configuration are going to be significantly different there is no magic cloud abstraction cloud provider abstraction but the tooling remains most of the same so you could be using the same tool for both let's say AWS and Azure if you go with Serverless Framework then if you really want flexibility of configuration again this is another point in favor of Serverless Framework like if you really have requirements that force you to have very dynamic data that gets interpolated into your templates you're going to have an easier time just going with Serverless Framework because with some you'll need to figure out your own pre-processing logic if you want to do something like that and this is also similar if you need to write custom extensions maybe you want to do something special at build time you have I don't know very specific build requirements or maybe you need to generate pieces of CloudFormation dynamically depending on different parameters it would be much easier to write your own custom plugin for Serverless Framework rather than having to create your own CloudFormation macro get it that deployed and then integrate it with some so another point in favor of Serverless Framework if you care about that kind of degree of extensibility so I would say that all in all Serverless Framework used to be a lot more mature than some so there might be a little bit of an opinion if you search around that some still new and not mature enough but i think this is kind of slowly changing because these days some really has caught up with all the features of well at least most of the features or the main features of Serverless Framework and it has even some new features like the pipelines that we mentioned or the ability to synchronize your code with the cloud in real time that makes it really a well-worthy choice I personally used to choose a lot Serverless Framework as a default in the past these days i'm kind of always thinking a little bit more about it like if I don't really need that level of extensibility i'm probably going to default to some because it feels like a safer choice going forward and this is probably a good pivot point where we try to speculate a little bit more about what is going to be the future of these two projects because this might inform your decision on whether you want to pick one or the other for a given project so again this is probably the most opinionated piece of this episode so take it with a pinch of salt but our opinion is that it feels that AWS is investing a lot on some so we expected that it's only going to get better and better and they are both open source projects but it's important to see that ownership piece like while some is clearly owned by AWS and it's in the best interest of AWS to keep it up to date and add more feature and make that development experience as nicer as possible because it brings more business to AWS itself the Serverless Framework is a lot more open and as a community but at the same time the project itself is backed by a company so it's a little bit difficult to understand how is this company going to get a return there are investors of course they need to pay back on that investment and even if they care about the community and the user experience there might be decisions there that we cannot predict that will affect negatively or positively the experience of the user in the end so I think there is a bigger question mark there on what's going to be the future of Serverless Framework when we compare it with some and I have a slightly related note to that point which again is just my own bugbear with Serverless Framework I used to think that the serverless documentation was quite good like the documentation itself on the website but also a lot of related blog posts and it was very easy when I was doing serverless in the very early days and using Serverless Framework to just search for anything and you will easily find either a documentation or a blog post and find the answers you were looking for these days for some reason and quite unexpectedly it is not as easy anymore I find myself myself ending up more and more on the commercial side of the offering that Serverless Framework has to give to users rather than actually finding the documentation for the open source tool that I'm looking for I don't know if that's intentional just to drive more business to the the commercial offering or it's just a side effect of trying to combine a commercial offer together with an open source project so that the website itself becomes a little bit more convoluted and even organizing the content gets of course more difficult so I don't want to blame necessarily the serverless company for trying to make revenue out of this project but the net effect on the users is that it gets more confusing to figure out how to use the open source part of the project so just to conclude I will say that it is also fair to say that SAM doesn't really have a great documentation either it's probably okay but sometimes when you want to do something that is slightly off the standard it's not very easy to understand how to do that so maybe this is just a suggestion for AWS to try to invest a little bit more time in trying to document different kinds of setups different more advanced features that you might have with SAM and maybe provide a lot more examples because those will will definitely help people to get started with all the different features that you might have issues and all the different kinds of permutations of projects. Okay I think that that's probably more than enough that we want to cover for this episode and we gave you a lot of opinions but of course we will be curious to know what is your opinion do you prefer some do you prefer serverless what are you using for your current projects and are you going to be using something different in your next project and of course this is only a portion of this conversation because of course there are other tools there is CDK there is Pulumi there is Terraform so if you don't use either SAM or Serverless and you end up using other tools, we would love to know why what are your requirements why did you make this choice. And maybe we can do another episode in the future where we try to explore some other of these tools and compare the differences. So thank you very much for being with us today and we look forward to seeing you in the next episode.

Help us to make this transcription better! If you find an error, please submit a PR with your corrections.