DHW DHW - 10 months ago 170
Node.js Question

Deploy Angular 2 App to EC2

I hope this is the correct place to be asking this question, please move or delete this post if it's not.

I am trying to create a simple website that can handle basic POST and GET requests using Angular 2 and Amazon EC2. I have a fair amount of experience with working on the front end of a web app in Angular 2, but little experience with connecting that to a back end and doing so on an Amazon EC2 instance.

I was hoping to find an example or some resources that would explain how to use the HTTP service with a backend framework of some kind. I know that Angular provides examples of how to use the HTTP service, but its hard to picture what the backend setup needs to look like to handle these requests and also how to correctly configure this setup on EC2. Any help or resources would be greatly appreciated!

Answer Source


As @glavan said, SPA like angular 2 apps can be deployed in AWS S3. This is the most cost efficient approach for SPAs. Here is a video deploying SPA on S3. This video will guide you through step by step instructions for deploying your angular app.


AWS EC2 is a good option. But there are many more alternatives available. As you said, you were new to backend, setting up EC2, VPC's and Elastic-ip is a little difficult process.

Nowadays, SPA's cover a lot of business logic, routing, etc., We need our backend only as API's for performing CRUD operations with database. I would like to suggest a bleeding edge technology called serverless. Here is the tutorial for launching your backend within 5 minutes. AWS lambda is a service that is called as function as service. You can build your backend using AWS lambda + API gateway + DynamoDB.

For eg: say you want to register some details in backend, you will POST all the data from client to your backend with url and proper path. In AWS lambda, you write your logic for POST as a function, which contains the logic to parse the data from request and send to dynamoDB. Now, this function can be exposed to world by connecting this function with API gateway( an another service in AWS). At the end we get an API, which can be used in your angular 2 APP. SO, on invoking the POST, angular 2 -> API gateway -> Lambda(extract request and send to DB) -> dynamoDB.

Benefits of using serverless compared to EC2.

  1. You don't need to manage your server(EC2) from updating the new security patch to auto-scaling, everything is taken care by lambda. Serverless is a fully managed service.
  2. You only pay when your lambda functions are invoked. On the contrast, even though your web app doesn't receive traffic for a given day, you have to pay the day-tariff for the given day.

Having said, try serverless when compared to traditional backend approach. Any questions on this would be welcomed.