The new Cosmic dashboard is here!
Test drive it now →

Build and Deploy a Serverless Node.js App powered by Cosmic

Community Articles
Community Articles Build and Deploy a Serverless Node.js App powered by Cosmic

Serverless application development is growing in popularity in no small part due to the appealing proposition to "build apps, not infrastructure". This way of building apps provides many benefits including not having to manage server infrastructure, infinite scalability and not having to pay for idle server time, paying only when your service is in use.

The Serverless platform further abstracts building serverless apps into a delightful developer experience that helps you get a cloud function-powered app up and running fast.

In this tutorial I'm going to show you how to get started with the Cosmic Serverless Starter app in a matter of minutes. And as a bonus I'll show you the steps to transform any Node.js app into an infinitely scalable Node.js Serverless App. Let's get started.


Check out the Cosmic Serverless Starter on GitHub

Install the Serverless Starter

To install the Cosmic Serverless Starter, run the following commands:

npm i -g cosmic-cli
cosmic login
cosmic init serverless-starter

These commands do the following:
1. Install the Cosmic CLI
2. Login to your Cosmic Account (you may need to create a new Bucket, if so, it'll walk you through this 😀)
3. Download the Serverless Starter and import the demo content to your selected Bucket.

Once it's downloaded, you'll be prompted to start the app with the following commands:

cd serverless-starter
cosmic develop

Your application should now be running at http://localhost:3000

Get Ready for Lift Off

Now that we have our application running locally, let's get ready to launch it to the AWS cloud. First we'll need to connect our AWS credentials. To find these on your AWS account, Serverless has a great walk through on their website.

After you've retrieved your AWS keys run the following commands:

export AWS_ACCESS_KEY_ID=your-aws-key-id
export AWS_SECRET_ACCESS_KEY=your-aws-secret-key

And now we're all set to launch our Serverless Starter to AWS.  Run the following command to login to the Serverless service and deploy our app:

npm i -g serverless
serverless login
COSMIC_BUCKET=your-bucket-slug serverless deploy

The serverless deploy command does all the heavy-lifting for us. It performs some magic behind the scenes with our AWS account, then once our app is available, it will provide the endpoints to find the deployed app. Adding the environment variable COSMIC_BUCKET, connects our deployed app to our Cosmic Bucket via the Cosmic API.  This enables us to manage content for our newly deployed serverless app from the Cosmic Admin Dashboard.

From the output above, you can see our available endpoints where our Serverless app is now deployed.

Make any Node.js App Serverless-Ready

So we ran a few commands and pushed the Cosmic Serverless Starter app to AWS... cool. But let's go a bit deeper and take a look at how we transformed the Node Starter app and how we can convert most any Node.js app into a serverless app.

Edit serverless.yml
To transform the Node Starter to a Serverless app, we had to create a serverless.yml file and set the config to handle dynamic routes.  This is accomplished in the function > app > events area to indicate the dynamic http events.  It looks like this:

Edit app.js
Next we needed to edit the entry point for the app, app.js file to export the main handler function.  We also included the serverless-http node module to wrap our Express app for Serverless use.  Here's the app.js file converted to serverless:

In Conclusion

With a few edits, you can quickly transform a Node.js app into a serverless app.  The Serverless platform abstracts the complexity of AWS account configuration to give you a delightful development experience.  Add Cosmic-powered content and you have an infinitely scalable serverless app with content managed via the Cosmic API.  No servers to manage, no CMS to maintain. Building apps, not infrastructure is the future!

If you have any comments or questions about building serverless apps with Cosmic, reach out to us on Twitter and join the conversation on Slack.

You may also like

An easy way to add analytics dashboard in your cosmic bucket.
Here we build a progressive web app in React.js with the power of Cosmic. It respond with status 200 and carries data when you disconnected to internet and in Chrome browser (both mobile and desktop) it gives an option to install the app on home screen.
In this tutorial, I'm going to show you how to create a minimalistic but awesome single page app blog using Vue, Vuex, Vuetify, Cosmic and deploy it to Netlify.
Add the power of Algolia search to your Bucket. This Extension enables you to sync your Cosmic Objects with Algolia, making your data searchable. Set up automatic syncing to automatically add / edit / remove content from Algolia. Follow the simple steps below to install and setup Algolia.
In this tutorial, Cosmic co-founder Tony Spiro takes you on a tour of Cosmic Webhooks. Learn how webhooks work, see how to easily install integrations with Slack, auto-deploy websites to Netlify, and get an intro to Cosmic Functions.
To demonstrate how easy it is to get started with Cosmic, we'll utilize the Cosmic CLI to quickstart an Angular Ecommerce App. This Angular ecommerce app allows you to process orders from customers and accept Stripe payments for goods and services. Manage inventory in the Cosmic Dashboard.

Get Started with Cosmic

Build personal projects for free. Add your team at unbeatable prices.
Start Building Talk to Sales