So you have your beautiful and robust Angular application. It’s time to deploy it and spread it to the world. Now the question comes. Where should I deploy? There is a bunch of options:

Traditional hosting

Dedicated server (like AWS EC2 or VPS)

Serverless environment

Today we will focus on the third one: Serverless environment. In my opinion, this is the best solution for startups and freelancers. You are paying only for what you are using and when you are using it. FaaS (Function as a Service) is running on the shared machines, launched only when customer request comes to the server and running as long as it is necessary to fulfill it. Most Serverless providers are charging you for number of performed requests, and execution time. Also, most of them are offering some free tier, ie. Amazon Web Services gives you 1 000 000 free requests monthly!

What @ng-toolkit/serverless will do for you, is review your application configuration to apply best solution for it. It will take a look inside angular.json file and based on it, setup necessary config, files and changes in the code.

Prerequisite

To make use of the library which we are going to apply for deploying, you need to have installed and configured CLI for the provider which you are going to use. Take a look at these guides to accomplish this:

I believe that you have NodeJS and Angular CLI installed. So let’s create a new project:

ng new serverlessApp

cd serverlessApp

You can also add server-side rendering (Angular Universal) support to it:

ng add @ng-toolkit/universal

@ng-toolkit/serverless

We won’t go forward with the whole set up manually. I believe that everyone more like the situation when setup is done for them, and they can focus on development.

To achieve that we are going to use newest CLI feature: ng add command combined with the external library @ng-toolkit/serverless .

Now you can choose your favorite provider.

AWS Lambda & Google Cloud Functions:

ng add @ng-toolkit/serverless —-provider aws

or

ng add @ng-toolkit —-provider gcloud

After successfully updating your project you can check if it is working correctly on lambda, by typing:

npm run build:serverless:deploy

An URL to your app is attached to the command output:

Serverless: Packaging service... Serverless: Excluding development dependencies... Serverless: Compiling function "helloWorld"... Serverless: Uploading artifacts... Serverless: Artifacts successfully uploaded... Serverless: Updating deployment... Serverless: Checking deployment update progress... ................................. Serverless: Done... Service Information service: myapp project: another-project-197513 stage: dev region: us-central1 Deployed functions helloWorld https://us-central1-another-project-197513.cloudfunctions.net/http Serverless: Removing old artifacts...

If you want, you can use a custom domain. Check out this step-by-step guide on how to set it up in AWS API Gateway. When your domain is successfully provisioned, you can deploy your app to production: