The purpose of this project is to demostrate a silly way of reducing cost by switching off an EC2 instance (The EC2 instance is host to an API server) when not in use (Not recieving any inbound HTTP requests). I am an amateur computer programmer, assume that I don't know what I am talking about or doing... This project is experimental... I shall have neither liability nor responsibility to any person or entity with respect to any loss or damage caused, or alleged to be caused, directly or indirectly by the use of information contained here.
Bootstraped tech startups have access to limited amounts of funds; so it is very essential to cut cost. Keeping the EC2 instance (which is host to the Web Application's API server) off when not in use while building and testing a full stack Web Application using Client Side Rendering is essential to cost cutting.
A conscious developer might write a shell script to stop the EC2 instance and run it before logging off for the day or taking a snack break. But this method is not fool-proof or fully automatic.
- What if the Developer forgets to switch off the EC2 instance before logging off for the day?
- When you stop / start your EC2 instance, the IP address of that instance will change.
- What if your team consists of developers (Like Frontend Developers or UI / UX Developers) who are not savvy with Amazon Web Services? These developers might be working late into the night when the Devops engineers are away...
- What if your team consists of non-developers like testers, product managers, scrum masters, sales staff, etc. who are not savvy with AWS. They might be testing or demoing the Web Application (which is in testing / development stage) to a potential client when the Devops engineers are away from keyboard.
What if we are able to switch on the EC2 instance (host to the application's API server) as soon as a user starts using our Web Application and switch off the EC2 instance when there is no inbound HTTP traffic (that is, nobody is using the Web Application) automatically?
- Angular2+ Application (You can use any Frontend technology, I have used Angular2+ because I am comfortable with it).
- S3 bucket (We copy the compiled Angular2+ Application inside an S3 bucket and serve it to the user's browswer through AWS Cloudfront).
- Angular HTTP interceptor (Every XMLHttpRequest made from within the Angular2+ Web Application propogates through this HTTP interceptor).
- EC2 instance (This EC2 instance is host to an API server built using Expressjs).
- Expressjs server (Serves API requests made from the frontend Angular2+ Application).
- Lambda function (Can switch on and switch off the EC2 instance).
- Expressjs middleware (Every XMLHttpRequest made to the express server passes through this middleware).
- EventBridge (The expressjs server's middleware updates the cron job before processing each HTTP request, the cron job triggers the Lambda function (To switch off the EC2 instance) after 15 minutes (The time peroid is arbitary).
- Amazon API gateway (HTTP requests made by the users from the browser to the Lambda function is routed through this API gateway).
XMLHttpRequest(s) are sent to the Expressjs API server (which is running inside an EC2 instance) as soon as a user starts using the Angular2+ Web Application.
- If the EC2 server is down, the XMLHttpRequest(s) to the API server fail. If XMLHttpRequest(s) to the API server fail, then we make an XMLHttpRequest to the Lambda function (through the API Gateway) which switches on the EC2 server.
- If the EC2 instance is up and running, and the API server is serving XMLHttpRequest(s), we update a cron job (The cron job triggers a Lamba function which switches off the EC2 server after an arbitrary time period) before processing each XMLHttpRequest. This way the EC2 server will be switched off as soon as (15 minutes in our case) the API server stops receiving XMLHttpRequest(s).
The API(s) might be a little bit slow since they are editing the eventBridge on each API call...
This front end Web Application was generated with Angular CLI version 13.3.1.
Run ng serve
for a dev server. Navigate to http://localhost:4200/
. The application will automatically reload if you change any of the source files.
Run ng build
to build the project. The build artifacts will be stored in the dist/
directory.
- SSH into the EC2 server.
- Clone this repository into the EC2 server by running
git clone https://github.com/shibisuriya/Amazon-EC2-Toggle.git
- Change directory to API/ by running
cd Amazon-EC2-Toggle/API
- Run
npm i
to install node modules. - Run
npm run serve
to start the expressjs server. - Expose the API server to the internet from the AWS EC2 console.
- Zip the files given in Lambda/ folder and upload it to AWS Lambda.
- Expose the Lambda function to the internet using the API Gateway console.
- continous deployment of angular application in using github action.