AWS Bites Podcast

125. A first look at CloudFront Hosting Toolkit

In this episode, we discuss the newly announced CloudFront Hosting Toolkit from AWS. We provide an overview of the tool, which aims to simplify deploying modern front-end applications to AWS while retaining infrastructure control. We discuss the current capabilities and limitations and share our hands-on experiences trying out the tool. We also talk about alternatives like Vercel and Amplify, and the tradeoffs between convenience VS control. Overall, the toolkit shows promise but is still early-stage. We are excited to see it evolve to support more frameworks and use cases.

AWS Bites is brought to you by fourTheorem an AWS consulting partner with tons of experience with AWS. If you need someone to help you with your ambitious AWS projects, check out fourtheorem.com!

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:

Eoin
00:05:49

The number one thing that you'd think about when you think about Vercel is probably the developer experience. And that involves how easy is it to get up and running and deploy your first application and keep it running. And the zero-config nature of Vercel is probably one of its best selling points because it's just very easy to connect your GitHub repo and have something deployed in a couple of minutes. It doesn't just limit itself to Next.js as well. It supports many other frameworks out of the box. And when you deploy it, you get the automated deployment from your repo, but you also get custom domains and your HTTPS certificates are going to be provisioned for you. And then it'll also scale on demand for you. So it's using other cloud providers under the hood, including AWS, and it's managing all of the scalability. But as well as just scaling within regions on cloud providers, it's also edge optimized. So it's doing edge up deployments as well to various CDNs and running a lot of those server actions and server component rendering on the edge for low latency. One of the really cool things is that if you're working with branches, it will give you preview deployments from your branches. So this means that right away, when you create a feature branch or a fixed branch, and you're working on something, you immediately get a preview URL that you can share with stakeholders. And one of the cool features of that is that your participants can annotate and make comments in line in your preview environment. So you can iterate really quickly with feedback. It also has a whole set of other integrations with other products like databases, you can get a Postgres database up and running pretty quickly. You get logs, they've been putting a lot of investment into AI support recently, CMS integration, and also other things like analytics and important security aspects as well. So there's a whole lot to love about Vercel, but they're not a sponsor of this episode. So while we like the developer experience and the feature set, it's still a software as a service, and you have a build or buy decision, and you have to evaluate the vendor on their own merits. If you want some more control autonomy around the infrastructure, you might want to deploy it to your own AWS account. And Vercel isn't something that will allow you to do that. So if you use Vercel, you don't have a lot of visibility into how your code is deployed and where it's going under the hood. And especially in the context of working with enterprise organizations, there are a lot of good reasons to avoid Vercel and to host front end applications yourself directly on AWS. So where do we start? If we want to host a front end on AWS, this is something that we've talked about a lot. It's a bit of a bugbear of ours, I suppose, just trying to find the right most optimal way to host static websites. We covered it in one of the very first episodes, episode three, how do you deploy a static website on AWS? And then back in episode 80, we were talking specifically about private static websites for enterprises. And there was always, it's always a trade off, no clear winner there. But now AWS has announced a new open source tool to help you tackle this problem. And this is the CloudFront hosting toolkit. So this certainly got a lot of people's attention, a lot of people talking because just you only have to mention Vercel competitor to get people's ears pricked up. And Luciano, you've been good enough to take a good look at this for us. So maybe your best place to give us an introduction, what is this CloudFront hosting toolkit and what can we do with it?

00:12:30

It's a CLI tool, so very easy to install with NPM, very easy to run it in any new project. It takes literally a few seconds just to get started. And also it does security and caching best practices. So all of these things are already incorporated in the generated infrastructure. So one less thing to worry about because if you were doing all of this yourself, maybe you get something working after a few hours of investigation and trainings, but then at the end of the day, you might still be unsure on whether that's something that can be production ready or not. This tool removes a little bit of that concern because it's already following tons of best practices that have been established in the AWS landscape for the last few years. And then it allows you to use custom domains. So if you want to host your own front end in your own custom domain, all of that process is streamlined. It's going to integrate with Route 53 and generate the necessary records, generate the TLS certificates for you, and make sure everything is integrated properly. If you don't have a custom domain, it's going to use a CloudFront domain for you. And finally, there is also a CDK level three construct for the toolkit that can be used as an alternative for the CLI. So that's maybe another option if you prefer to go more with something that is integrated with your own CDK rather than using the CLI. But I mentioned atomic and immutable instant deploys, which is a little bit of a mouthful. So do we want to try to explain what that really is and how it is implemented? Yeah, it's pretty neat.

Eoin
00:13:52

It uses CloudFront functions and CloudFront key value store, which you may not have come across. Let's go through these fundamental pieces of CloudFront first. So CloudFront functions are a little bit like CloudFlare workers, I guess, or other edge functions that you might've come across. They're like a much simpler version of Lambda because you also have Lambda at the edge, but Lambda at the edge is more like a fully fledged Lambda, although not quite. CloudFront functions are just more like lightweight JavaScript isolated contexts that are much more efficient, but also restricted in terms of what packages you can use and that sort of thing. So you could use CloudFront functions for writing lightweight functions for high-scale latency-sensitive CDN customizations like redirects or request response modification, URL rewrites, that sort of thing. And then you have CloudFront key value store. And this is a bit like a simplified version of DynamoDB that runs on the edge. And we've seen this kind of service from CloudFront, sorry, from CloudFlare as well. It's a global low latency key value store, and it allows you to read from CloudFront functions. And by having that state in your key value store, you can do more customizable logic at the edge functions. So when you have CloudFront key value store, you can make updates to function code and updates to the data associated with the function independently of each other. This kind of separation will simplify your functions code and makes it easy to update data without the need to deploy code changes.

Luciano
00:17:34

So I'm going to try to describe the steps that I performed to try to test this tool and the different things that I found out. And hopefully that can give you an idea of how the tool works, the kind of developer experience you get and what gets created for you behind the scenes. So I started with a plain vanilla Next.js project. There is one CLI command that you can find in the Next.js documentation that basically uses a starter kit. And that starter kit is just a simple repo where you have a one Next.js page that points you to the documentation, to the about page and a few other simple things. So effectively it's like one page application, and it's like a static, statically renderable application because it doesn't really do any server side action or anything like that. So you run this first command, this command will do all the scaffolding for you. At that point, you can do npm install to install the dependencies. And if you want to run your frontend locally, just to see it running and play around with it, you can just say npm run dev, and that will give you locally an environment that you can use to tweak things around. Then at some point you are maybe happy with it and you want to try to deploy it using this CloudFront hosting toolkit. The first thing you need to do is install the toolkit itself. We said it's a CLI application, it's built with Node.js. So of course Node.js becomes a requirement at that point. But once you do that, you can easily install with NPX or just by doing an npm install global of the @aws/cloudfront-hosting-toolkit. And that will basically pull the latest version of the toolkit for you in your local development environment. Now the idea is that you need to use it against an existing repository. So this is why I created an Next.js project first. Once I did all of that and I was happy with the outcome, then you can start the tool. And the way you start the tool, you go with your CLI into the specific folder where you have your frontend application and you just run cloudfront-hosting-toolkit init That's literally the one command you need to run. And at that point, you have a guided process that will effectively ask you some questions to try to understand how to deploy this project for you. And the first question that it's going to ask is what is your repository? And of course, it's going to be able to auto detect it from the current working folder. If you have already initialized that as a Git repository and it's hosted on GitHub, it's going to automatically complete that particular question. But you can change it if you have different requirements, or maybe if you haven't set up the repo yet locally, you can still point it to a repo that you have set up already remotely on GitHub. The next step is going to ask you the name of the branch for deployments. It defaults to main, but I think you can use this if you want to change that to something else. Maybe you want to have a branch called deployment or deploy that you specifically, when you want to trigger a deploy, you can decide which branch is actually going to trigger the deployments that way. The next question is what is your framework of choice? And here, you have a few frameworks that are supported. AngularJS, Next.js, React, Vue.js. There is another one which is no build required, which basically means everything that is in the main folder is going to be deployed. And I think this is for static websites that don't require a build process. Maybe you just created a few simple HTML, CSS, and JavaScript files. You have them there and you just want to deploy them. The cool thing here is that it was able to automatically detect that I was using Next.js. So that option was already selected for me. I just needed to hit enter and proceed.

00:21:57

Maybe you want to do additional things that are not the standard way of building an Next.js website. You can easily change all the steps and customize it to your liking without having to change the toolkit or having to change anything else in the infrastructure as code. So it's just this one file allows you to change things. It also generates in this folder the CloudFront function that is used for URL rewriting, the one you described before, Eoin. And you can see all the JavaScript code that is being generated. So also there, you have an opportunity to change things around. Maybe you can, I don't know, add some extra logic to check if there is a query string parameter that says version equals something. Maybe you can actually enable that feature preview that way and by yourself, assuming that it's not supported yet. But basically, this is just to tell you that you have one file with all the logic that are visible. It's easy to change. And if you change it, this is what's going to get deployed for you. And then finally, there is a generic JSON file that contains all the options that were requested to you in the initial init phase. So if you change your mind, maybe you don't want to deploy from main anymore, you want to deploy from another branch, you can easily change that JSON file to get your base configuration updated. At that point, what you can do is that you can effectively deploy. At this point, you haven't deployed anything. You just generated these three configuration files, but those three configuration files are enough for you to start the deployment. So you can run another command called cloudfront-osting-toolkit deploy And what it's going to do is basically going to take all these three generated files as an input, and it's going to create all the necessary cloud formation templates to deploy everything else that we described before. So it's S3 buckets, CloudFront distributions, the CloudFront function, the CloudFront key-value storage, and there is a lot more. You can check out, there is a diagram in the announcement page and in the repository showing exactly all the resources that get created and how they are related to each other. And this is also going to do a few other things. So initially it's going to bootstrap your AWS account, which I think it means it's going to create a dedicated street bucket for the deployments. It's going to create the repository, the CodeStar integration. And actually the CodeStar integration is really interesting because what it does the first time you need to manually confirm that connection between your GitHub account and AWS. And it's something that you have to do in a browser where you are effectively authenticated on both sides. So what it's going to do is going to generate the request for you. Then it's going to redirect you to the AWS console and in the AWS console, you can click to basically connect your own GitHub account. To be honest, this was amazing to see on one side, but on the other side, in my case, it was a little bit annoying because I use, when I access to AWS, I generally use anonymous sessions because I might have sessions for different AWS accounts. So when I was trying to connect to GitHub, it didn't have my own credentials. And it took me a while to connect into that anonymous environment with GitHub, asking you MFA and confirmation by email, just because they see that it's a different session that you haven't used before. But eventually I managed to get it working. And then all the local setup was done. The remote setup was done on AWS. And you just need to click enter to say that you successfully connected GitHub with AWS. And then at that point it's going to proceed with the cloud formation deployment. Now, at this point, I had a problem because once everything is deployed for you, it's also going to try to run the code build for the first time and try to do a first deployment of your front-end application. Like if you were doing your first commit, basically. And that one actually failed because there is an issue with the version of Next.js that I'm using. I'm using the very latest version and that latest version doesn't support Next export, which is what the default build pipeline is using to create the static version of the website. Now, this is something you can easily fix. You just need to create a Next config.js file, do the modern way of saying that you want to produce a static website, which is just like one option, I think it's export static or something like that. Commit that into your own repository. And of course you also need to update the code build pipeline, not to use Next export anymore, because at that point, when you do Next build, it's already creating the final version of your website for deployment. And this is basically where I stopped, but I could see that everything was generated correctly. I could see that if I was doing another commit, the build pipeline would trigger automatically and do all of the deployment we described. So I think even though there are some steps that might be a little bit annoying, overall, that experience was quite pleasant. I don't know what you think so far, and if you have any final consideration on your end. You know, we talked about this kind of stuff when we were talking about CDK in previous episodes.

Eoin
00:26:22

I personally have a bias against code, sorry, against tools that generate code that introduce another level of abstraction, mainly because it's just another, you have to understand kind of what's going on under the hood, and then also what's happening in this abstraction layer. And if, especially with a tool like this that might not be perfectly stable yet, you might wonder, will it suddenly generate a completely different set of infrastructure that might make it difficult to do seamless upgrades? And I'd much prefer to spend the time to provision all of the infrastructure myself and really understand it more often than not. But I can certainly see the value in it, because I have been in a case recently where I've been trying to deploy a static version, like a SPA version of an Next.js app, with only client-side rendering, with static export to CloudFront, and it's definitely not a trivial thing. So I think there's, I definitely commend the effort here to get all of this stuff working. It's not an easy thing to do, and it does provide like a good blueprint for people to get started with it. So while my bias isn't necessarily drawn to a tool like this, I think it is pretty promising. I think there's some really good ideas in here, like the immutable deployments, and it seems to be simple enough to use. There's obviously some rough edges that need to be polished. The fact that it doesn't support server-side rendering may be a bit of a blocker for a lot of people at this point. Let's hope we'll see a bit of evolution there. I mean, I kind of think back to one of the previous releases from AWS we covered, which was the integration testing toolkit, the IITK, and it was also a similar kind of review, like a lot of promising stuff, but needs more work. And when I went back to look at that recently, because I did use it in a side project, it doesn't seem to have actually evolved that much. So I know, we know that AWS likes to release things early and get feedback from the user base, but let's hope that this one sees the love and attention that we need. What's your take?

Luciano
00:28:14

Yeah, I absolutely agree with everything you said. A few things that I can mention in addition to that is that I was able to fix an initial issue that I had with the CLI not installing properly. So I just submitted a PR and I was impressed to see how quickly I got a review. The PR was properly commented and released and very quickly all happened in the span of an hour. And the problem was fixed immediately. The maintainer was extremely welcoming. So that's definitely a good thing to see. And I think that creates a big opportunity for everyone that wants to contribute to this kind of project to be able to do that. It seems really like a very welcoming environment for people to contribute. So maybe if the tool doesn't have all the things you need and you are willing to invest a little bit on it, definitely think about contributing and giving back and working with the maintainer to get to the point where this tool is actually going to be helpful for you and for other people. So that's absolutely a plus one for the tool and the current maintaining team. Another thing that I think is worth mentioning is that there is another walkthrough by our friend Sandro Volpicella. We will have the link in the show notes. He has also done a similar kind of test ride of the tool and wrote a blog post detailing all the things that he found out. I think there is even a few more things that he explains. Like for instance, there is a step function that is used for updating the versions and he also goes through on how that works and why it needs to be there and maybe how it can be improved. So if you want to do an additional deep dive on the technology and get another perspective on the tool, definitely recommend to read that blog post. And then finally, I think it's worth mentioning a few potential alternatives because we are effectively working with a customer that might need this kind of stuff. So we are doing lots of research on what are the possible options there. And it's also a topic we are really passionate about. As you can see, we keep talking about this. So other things that we think are going to be suitable, for instance, you can more or less relatively easy, you should be able to create a container and then run that container with a Next.js application with full server-side rendering either on AppRunner or Fargate. But then of course, you have something that is running 24/7. So maybe not necessarily what you want to do if you want a more serverless approach. And in that front, there is another project that seems very promising that is called OpenNext, which we haven't tried yet, but in the docs, it says that it also supports server-side rendering on Lambda. So that seems maybe a closer re-implementation of what Vercel does for you, but that you can deploy on your own on AWS. It's definitely worth a look. And hopefully we will have time to try it out and maybe give you an overview of what that looks like. Similarly, there is another project called Coolify, which is a little bit more generic. It's probably a modern take on Heroku. So it allows you to host your own PaaS and it does that in a serverless way. But as such, you should be able to host a container that you can run with everything you need to run an Next.js project. So another thing that we really like to try, we haven't had the time yet, but if we do try it, we'll let you know our findings. And finally, there is Amplify, which always gets mentioned every time that people talk about Vercel versus AWS. I don't personally have an opinion on that one because I haven't really used Amplify much. So, oh, and I'll leave it to you to mention something about Amplify if you want. I've looked at, you know, I don't have a huge amount of experience either.

Eoin
00:31:21

I've used the Amplify CLI and the client SDK a lot. The managed side of Amplify is a whole other set of features. Like they allow you to easily provision storage and hosting and all sorts of other stuff. I know that they've introduced a new generation two, and I'd love to do an episode on that in the future. But from what I can see with the new generation two Amplify, it's also solving the same sort of problem in a much more managed service kind of way. So it seems a little bit more like an AWS managed Vercel alternative. And I think from what I've seen so far, a lot of work has gone into that. I haven't tried it, so I don't know exactly what the experience is like. Let us know if you have, because I'd love to know. I have seen some online discussion. Actually Yan Cui was mentioning the release of the CloudFront hosting toolkit. And one of the principal SAs at AWS was answering the question, well, why did you actually create this tool when you already have Amplify? And it seems like they explained pretty clearly that strategically they're basically saying, if you want to manage service, use Amplify. But if you want a little bit more control over the infrastructure and you're really just looking for something to generate that blueprint infrastructure, then that's what the CloudFront hosting toolkit is aimed to solve for you. So at least that gives you the right kind of context for deciding which approach you might like to take. Awesome. And I think that covers everything we wanted to cover for today.

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