What's the cheapest way to deploy Node JS APIs on AWS?

I want to deploy a nodejs express application which also works with an rds instance. I’ve already set it up using beanstalk, but load balancing cost me half of the overall bill, which is absurd since I couldn’t configure SSL on beanstalk’s EC2 instance without the loadbal.

I’m using GitHub actions for cicd, I’m willing to use codepipeline as well. I’m even willing to containerize the entire thing.

It’s a cost efficient project, i could really use some input here.


Related discussions:

Is it gonna be high enough volume to actually require an LB?

Containerise it and put it on ECS or a free-tier EC2 host.

Its a simple setup you really don’t need to use Beanstalk for it and you can configure the SSL on the EC2 through NGINX/Lets Encrypt.

This is all the super basic DevOps stuff, you can DM me and I will walk you through it if you want.

API gateway and Lambda probably.

you asked what is the cheapest. the cheapest is to put it in aws lambda, and figure out how to transform lambda events into express calls. google tells me there are tried ways to do this.

cheap yes, but with downsides:

  • any kind of local storage is a goner. do you want sessions? need to rework.
  • highly parallel. if a dozen requests hit at once, you’ll have multiple instances starting up. which is good, but your database backend might cough up blood.
  • cold starts. if you have a megabyte of static files, the first hit after a few minutes inactivity will take a few hundred ms to be served. the rest will be quick.
  • deployment requires updating a lambda function. well, it is still easier than updating ec2 installations, but something to keep in mind.
  • no websockets.
  • no background tasks.
  • limits on payload sizes

there are other things to consider:

  • the static files might be routed to an s3 bucket, via cloudfront. thus you only have to deal with the dynamic content in lambda.
  • do you really need an rds? in some cases, dynamodb does the job. or a combination of dynamodb, s3, s3 select and athena. if you can, consider redesigning.

I do this all the time. Appsync + Lambda or Api Gateway + Lambda. Server less products are cheaper and generally easier than VMs which can be very difficult to administer.

Use and learn AWS CDK. It’s a game changer.

How would you get traffic into ECS without a LB? I mean, while still being able to redeploy and not having to manually change DNS

For anyone who needs to setup ssl with elastic beanstalk without LB,

Remove LB and serve the single instance as http 80 traffic.
In your dns, transfer the specific subdomain to cloudflare. Add a cname to the elastic beanstalk server domain in Cloudflare with proxy enabled. Cloudflare allows proxy ssl traffic. Which means connection is secure upto cloudflare. Cloudflare setup does not cost you anything.

This method is suitable only for development/staging environments because ssl termination does not go upto the server.

Without knowing what it’s used for and what’s the expected traffic level, it’s hard to make a good recommendation.

Elastic Beanstalk have single instance so you don’t have to pay for load balancer. Of course, this is for low traffic / low load workloads

You can also configure CloudFront to point to your Elastic Beanstalk environment url, which would then take care of SSL if you use ACM (this is for custom domain purpose). I haven’t tested it yet, but I don’t know why it wouldn’t work. Pointing to CloudFlare is also a free option

Now the cost should mostly be ec2 instance and rds

Now… For NodeJS API, AWS has a lot of serverless options, I would suggest looking at API Gateway + Lambda + DynamoDB. For low traffic, this is very low cost, even free in some cases

AWS SAM

It’s without a doubt the cheapest and AWS recommended best practice. Severless, scalable, cheap.

Cheapest in terms of your credit card and short-term time is probably an EC2 instance.

Best option nowadays is usually Lambda.

Lambda

AWS has really expensive egress. I pay about 5x more for traffic than for 2xEC2+ELB. Consider this before deciding on hosting on AWS.

Cheapest only looking at infra costs and ignoring development costs, or cheapest to take what exists without changes and deploy it?

You can even do it just with Lambda with an HTTPS endpoint if you don’t mind having a random domain for the API. Then put a CloudFront and S3 static HTML for the frontend.

I guess CloudFront could also help with a custom domain for the Lambda API.

Lambda will happily use container images, just needs a little different way to handle the incoming events compared to if you’d do it with plain container services like ECS or EKS

Not cheap if your API usage grows significantly

iirc the benchmarks, at 0.5s/req, 56x requests a second is when it becomes more cost efficient, purely from a compute cost, to switch over to ECS

I’ve been trying to look into it, I’m new to devops and AWS so it’s a little difficult for me to learn atm. i mean, it’s a payment gateway integration app, it doesn’t really need static files. the RDS is used for user records who are using the gateway.

I’m not sure how to implement cicd along with lambda, and how to migrate the current project to lambda in the first place. i have dockerized the current project, and am using docker-compose to launch the new build package on ebs automatically. how will this dynamic work with lambda? could you please point me towards any resources i could look into?

I’ll definitely look into it, thank you!

I’ll consider all of this, yes. it’s a pretty basic project, not in need of an LB. payment gateway integration. front end is on amplify, backend is in a docket runtime env on ebs.
I’ll look into all of this tomorrow at work, thanks a lot for the response!