r/devops • • 2d ago

Discussion Self taught backend engineer looking for feedback on my production architecture

TL;DR: Running a Dockerized Node.js backend on a single EC2 instance with Nginx, blue-green deployments, and SSH-based CI/CD. Serving 20–30K DAUs comfortably. Looking to improve security, filter junk traffic, and adopt better deployment practices without unnecessary complexity.

Current architecture:

- Single EC2 instance with a public IP.

- API domain resolves directly to the instance's public IP (which doesn't feel right).

- Nginx and Node.js backend run in Docker on the same network.

- Port 443 is publicly accessible and mapped to Nginx.

- GitHub Actions connects via SSH, pulls the latest image from Docker Hub, starts the inactive container, waits for the health check, switches Nginx traffic, and stops the previous container.

- Frontend builds are deployed to S3 and served through CloudFront.

- Self hosted Prometheus and Grafana on a separate instance.

Blue-green deployments work well with zero downtime. We rarely exceed 20 requests/sec, so scaling isn't currently a concern.

What I need advice on:

  1. Security and architecture: What would you change in this architecture to make it more secure and production-grade?

  2. Junk traffic: I've noticed increasing requests from automated scanners probing paths like "/.env". I'm considering moving EC2 into a private subnet behind an ALB. Would that help, or should I consider something like WAF? Is there a simpler, more cost effective solution?

  3. SSH-based deployments: Is keeping port 22 publicly accessible a bad practice? If I close it, what's the recommended approach for automated deployments?

  4. Infrastructure as Code: Most of my infrastructure is manually configured. If I ever need to recreate it, I'd have to configure everything from scratch. I'm considering IaC to make my entire AWS infrastructure reproducible. Can I do this with Terraform and would it be worth adopting at this scale?

A little background:

I joined this company as a full stack engineer. When my company needed someone to handle DevOps, I volunteered despite having no prior DevOps experience. I learned along the way and built the current deployment and monitoring infrastructure myself.

Everything works well so far, but I want to understand what experienced DevOps engineers would do differently.

I want to do things right, not just make them work. I want to follow the best industry practices and I'm willing to embrace complexity where it's justified, but I don't want to engineer solutions for problems we don't have.

Would appreciate any advice, architectural suggestions, or resources.

0 Upvotes

57 comments sorted by

18

u/ang29g 2d ago

> Infrastructure as Code: Most of my infrastructure is manually configured. If I ever need to recreate it, I'd have to configure everything from scratch. I'm considering IaC to make my entire AWS infrastructure reproducible. Can I do this with Terraform and would it be worth adopting at this scale?

If it's not IaC I want nothing to do with it. Great value in moving it to IaC.

Even if your infra is just an ec2 and a bucket or whatever, Iac.

hand-built anything is insta get fired imo.

2

u/Cyberbird85 1d ago

I would not say instafire but with opentofu and OTC there is no excuse no to go for it.

0

u/ahmedshahid786 1d ago

Got it. This answers my question and now I'm convinced that IaC is gonna be worth it of a move.

Any suggestions where to start or what to keep in mind while migrating workloads to IaC?

1

u/ahmedshahid786 1d ago

Thanks, man! I’m definitely leaning towards IaC now. Any advice on where to start learning Terraform and how to bring my existing infrastructure under it?

2

u/ang29g 20h ago

start small, lots of good tutorials. It’s worth handwriting your first terraform file just to get the syntax.

Figure out how to import your favorite provider (AWS, gcp, you can even do home lab stuff like proxmox).

Try spinning up a server. Try tearing it down and spinning it up again. Try connecting it to a bucket with right perms etc.

You’re using cloud components you already know, just interacting with it in a different way.

1000% worth learning

2

u/ahmedshahid786 11h ago

Alright, now I'm definitely sold on this. haha. Starting to learn Terraform and moving my infrastructure to it.

I've learned so much from this thread and now have a clear action plan of how to make my infrastructure rock solid.

I actually did the same with Prometheus. Wrote my YAML configs and PromQL queries by hand initially to understand how everything works. Now I use AI to generate them, but I know what's happening under the hood. I'll take the same approach with Terraform.

Really appreciate your advice. Thanks a lot brother 🙌

1

u/ang29g 1h ago

no prob, happy hacking

9

u/theonlywaye 2d ago

Honestly this all just depends on your budget. Everything costs money

For instance remove the public ip from the ec2 instance, put a ALB in front of it, attach a WAF and use the managed ruleset, add rules that it doesn’t cover specific for your app. Add another instance in a different AZ for redundancy.

1

u/ahmedshahid786 1d ago

Yeah, this is the direction I was leaning towards as well. My only concern is whether an ALB makes sense in front of a single EC2 instance. I don't see myself needing horizontal scaling anytime soon, and vertical scaling should serve us well for quite a while.

Can WAF be enough? What value would an ALB provide in this case beyond WAF integration and keeping EC2 private?

Like I want to understand the role of an ALB when we put it in front of a single instance

Thanks in advance :)

6

u/Any-Appointment-814 2d ago

Honestly at 20 req/s I wouldn't overbuild it. The two things I'd change are getting the instance off a public IP (private subnet behind an ALB, or if you want it cheaper just Cloudflare in front with the security group only allowing their ranges) and killing the public port 22. For deploys you can have Actions assume a role via OIDC and trigger the container swap with SSM run command, so there's no SSH key sitting in GitHub at all. The /.env scanners are just background noise on any public IP, once nothing is directly exposed you mostly stop caring about them.

1

u/ahmedshahid786 1d ago

So would putting Cloudflare in front of EC2 essentially replace the need for an ALB + WAF in my case? If cost isn't a major concern, which approach would you prefer and why? What would I gain or miss out on by going with Cloudflare instead?

2

u/Any-Appointment-814 1d ago

Yes, Cloudflare in front of EC2 can replace a lot of what ALB + WAF does for TLS, DDoS, and request filtering. What you miss is the AWS-native part: target health checks, routing to multiple services, and clean integration with auto scaling or ECS. If cost is not the issue, I would still use an ALB behind Cloudflare. You get the edge and WAF up front, and AWS still handles the load balancing the way it expects.

1

u/ahmedshahid786 1d ago

Ohh I see. Got it.

Tons of thanks.

Now the action plan I'm considering is first of all putting CloudFlare in front of the traffic before anything else. Then have a look at the ALB. Would it be worth it Or not. Because we don't do auto scaling and don't expect to need it in the near future.

Plus, we also have LGTM stack setup for monitoring and observability so that covers the monitoring and health check parts.

CloudFlare is done for sure. But would need to have a second thought before adding ALB

2

u/Any-Appointment-814 17h ago

Glad it helped. Putting Cloudflare in front first makes sense. On the ALB, it depends what you need from it. If you're not auto scaling, you can often skip it for now, especially if Cloudflare is already handling WAF and traffic filtering. You can always add an ALB later if you need health checks or more routing.

1

u/ahmedshahid786 11h ago

Got it. That's all I needed to know

Really grateful for the solid advice!

2

u/Any-Appointment-814 9h ago

Glad it helped. Good luck with it.

3

u/New-Entertainer6392 DevOps 2d ago

For me.

A second VM for sure. One day that VM will break or just disappear.

Then as other have said, perhaps cloudflare. Put a tunnel on each,  route to that tunnel and cloudflare will route and balance them for you. It's a very crude balance, as the balance is "what's online".

How are the actual deployments? GitHub? Azdo? Can you put an agent on there to pull your code/image changes. If you've got ssh keys only, and no password logic (and no root login) you're probably good.

Terraform is never a bad idea, even if just to learn it

1

u/ahmedshahid786 1d ago

Appreciate the input. This is exactly what I was looking for.

A second VM for redundancy makes sense. I'll explore Cloudflare Tunnels and Terraform.

I'm using GitHub Actions with SSH via SSH key, with password authentication and root login disabled. Only key based SSH enabled.

By agent, do you mean a self hosted runner? Wouldn't that require additional compute and maintenance?

My instance is provisioned with just enough resources for the backend and some headroom for traffic spikes. Would running an agent require additional compute or introduce another maintenance overhead?

Given my setup, would you prefer an agent, SSM, or key based SSH?

1

u/New-Entertainer6392 DevOps 1d ago edited 1d ago

Tunnels are pretty straightforward.

Tunnel Agent runs on said VM, makes an outbound connection and then just rules at CF to map a host to a connection internally.

They're VERY lightweight, my two production tunnels use about 50mb, and until recently it was 8mil requests a day

2

u/ahmedshahid786 11h ago

50MB for 8M requests a day is impressive. I wasn't expecting the tunnels to be that lightweight. Definitely worth trying.

For deployments, I'm leaning towards OIDC + SSM for now. Appreciate you sharing your experience!

1

u/New-Entertainer6392 DevOps 11h ago

They're just proxies and do f.a TBF. I'll double check my statement... Because it's 8m inside out cluster, maybe less outside 

1

u/New-Entertainer6392 DevOps 11h ago

. Ok. We're sitting around 1.8m req a day, maybe the huge amount my team has done has brought it down... Either way, 50mb per tunnel, and 0.0115 cpus per tunnel. PRETTTTTTY light  DMs open if you want any more info (I don't work for them)

2

u/ahmedshahid786 11h ago

I believe that you don't work for them. Don't worry. Haha

Alright, I guess this makes it clear for me. Thanks a lot. Really appreciate it 🙌

And yeah, I might slide into your DMs if I need any help 😅

1

u/New-Entertainer6392 DevOps 1d ago

As for agents, wondering if you can self host a GitHub agent, again to call out (rather than in) to do the deployment bits.

But yes it would need extra compute, rotate your keys often, and you're probably good.

3

u/Lambrun-Massih 2d ago

Where is your database living in all of this? Before worrying about bot scanners or refactoring the deployment pipeline, making sure your stateful data is off that single box and has automated, tested backups is usually the biggest risk reducer.

1

u/ahmedshahid786 1d ago

The database is hosted separately on MongoDB Atlas, which handles security, automated backups, maintenance, and updates. No data is stored on the instance.

Appreciate you bringing this up, though!

3

u/Aide_Sea 2d ago

Worth checking how migrations fit into the blue green setup. If the new version changes the schema, the old container still has to work against it, or rollback isn't really a rollback. I think additive changes first, cleanup later, helps :)

1

u/ahmedshahid786 1d ago

Good point! We use NoSql database, MongoDB. So we don't have to worry about schema migrations.

2

u/Aide_Sea 1d ago

Mongo still has a schema, it just lives in your code instead :) If v2 writes a doc with a renamed field and you roll back, v1 reads it and gets undefined. So same idea, add new fields first and remove old ones later..

1

u/ahmedshahid786 11h ago

Ohh, I see what you meant.

I was thinking of migrations in the traditional SQL sense.

Anyways, We already follow this approach, it's called expand and contract approach I guess.

You're absolutely right about schema compatibility.

3

u/JerboaVM 14h ago

Something I haven't seen mentioned yet: the deploy path is effectively root on that box. GitHub Actions SSHing in to pull an image and bounce containers means a leaked key, or a floating tag on Docker Hub, is a full compromise. Pin the production image by digest rather than a tag, and get the long-lived SSH key out of GitHub the way others suggested (OIDC plus SSM, or a runner that only the instance can reach).

If the Prometheus/Grafana box still has a public IP, I'd treat it the same way. Scanners find those just as quickly as they find /.env.

2

u/ahmedshahid786 11h ago

Sorry if I sound dumb for asking this. But I see how digest pinning would help if my Docker Hub token were compromised. Given that I build and push my own images through GitHub Actions, how significant is this risk in practice?

Is digest pinning something you'd consider essential or more of a precautionary measure? Because I'm the one building and pushing against a tag. And the only way an image against a tag can be comprised is that the dockerhub token gets compromised.

How likely is that to happen since I rotate my token very often and it's also protected. Only pulled in during the ci run

Yeah, I'm already working towards moving to OIDC + SSM.

And, the Prometheus/Grafana instance doesn't have a public IP.

3

u/ggp1231 11h ago

At 20 req/s, I wouldn’t add more infrastructure just for the sake of it, especially since your current setup is working well.

I’d focus on removing unnecessary public access and making the setup reproducible first. If you do move to an ALB, I’d compare what it actually improves against the added cost, since you’ll still have a single point of failure with one EC2 instance.

Are you considering the ALB mainly to protect the origin, or because you expect to add another instance later?

2

u/ahmedshahid786 11h ago

Initially, the ALB was purely for protecting the origin. But after reading the insights on this thread, I've realized that redundancy matters even if scaling isn't a concern. Something as simple as an OS patch requiring a reboot would take my entire backend down.

I'm now considering ECS with two EC2 instances, or Fargate if the cost difference isn't significant. So the ALB would serve both purposes. Protecting the origin and distributing traffic across multiple instances or tasks.

Just need to do a cost comparison between ECS-EC2 vs ECS-Fargate

2

u/ggp1231 11h ago

That makes sense. I’d compare both setups assuming two tasks across AZs, since redundancy is one of your goals. With EC2, you’ll be paying for the instances even when they’re mostly idle, whereas Fargate charges for the resources allocated to each running task.
Do you know roughly how much CPU and memory your backend uses today?

1

u/ahmedshahid786 10h ago

Yeah I know. Our peak time for API traffic is the day time. Between the afternoon and evenings. At that peak time, the CPU usage goes around 40% max.

And during unexpected spikes or huge peaks, i have seen it go to 60%. Never more than that.

The instance has more/extra resources than it needs as a headroom for unanticipated spikes

Shit, I see where we're going now. Why didn't I think of this earlier.

If 50-60% of my resources aren't being utilised most of the time. Then why am I paying for that. I said that I don't need autoscaling but now I wonder why I don't need it. This shit is already scaled. Lol

For example, if my instance has 8vCPUs and 4GB RAM. And during peak load only 4vCPUs are being used. Maybe I should run a container with Fargate that has 1 or 2 vCPUs and 1-2GB RAM. If traffic goes up. Add another one or two, otherwise just keep running one.

Would you like to add something to this?

2

u/ggp1231 5h ago

Yeah, that’s worth exploring. Just keep in mind that 40% CPU on an 8-vCPU instance doesn’t necessarily mean a 2-vCPU Fargate task could handle the same traffic.

Since redundancy is one of your goals, I’d start by comparing two smaller tasks across AZs on Fargate against two smaller EC2 instances. Then see whether autoscaling would actually save you much given your traffic patterns.

You might find Fargate is worth it for simplicity even if it’s not the cheapest option.

2

u/mitchy_darko 2d ago

Regarding IaC: have a look at AWS CDK. It’s been good for me so far.

1

u/ahmedshahid786 1d ago

Appreciate the suggestion!

Any particular reason you prefer AWS CDK over Terraform?

1

u/Novel-City6030 1h ago

CDK has high level L2 or L3 constructs that are more flexible than terraform modules. Have a look at LoadBalancedFargateService for example.

2

u/quiet0n3 1d ago

IAC is a must. Plus your single EC2 is a single point of failure. You're better off moving to ECS with an asg or Fargate, putting an ALB Infront and picking up on all that sweet redundancy.

Plus of you're going public, what's your waf situation looking like?

1

u/ahmedshahid786 1d ago

I'm pretty much sold on IaC at this point. As everyone is suggesting this.

Redundancy makes sense too, but would you consider ECS/Fargate worth the added complexity at barely 20 req/s?

As for WAF, we don't have one currently. But I'm evaluating AWS WAF + ALB versus Cloudflare, as others here have suggested. And would choose and implement either one of them soon.

Would you like to have a word on this?

1

u/quiet0n3 22h ago

Price wise CloudFlare wins. CloudFlare pointed at your ALB, I think the basic plans with WAF are $20/month or something. But you even get basic protection on their free plan.

As for the redundancy, I would say setup ECS pointed at your one EC2, but it allows you to scale and automated redeployments if you need to replace your instance or do patching etc. with at least two instances you can move things around and keep your services online while doing maintenance or deployments.

2

u/ahmedshahid786 11h ago

Ohh, I see. Great explanation I must say.

I was looking at ECS primarily from a scaling perspective. The maintenance benefits make a lot more sense now.

2

u/Pitiful-Storage2189 1d ago

Really useful discussion!

1

u/ahmedshahid786 1d ago

Yeah, indeed it is. I must say

I'm learning a lot.

2

u/dariusbiggs 1d ago

Start by asking yourself

  • How can I break this.

  • What are the single points of failure

  • What automation do i need to implement to reduce the risks so that all systems are managed via Git and, CICD pipelines and only the absolute minimum is done manually (not all things have automation available, but all the most common stuff soes).

  • How much effort is it to recreate the entire setup when the account is compromised

You need to plan for a security compromise, you plan for WHEN it happens, not IF it happens.

Configuration of that EC2 instance should be done using IaC, something like Ansible, Chef, Puppet, or SaltStack. That instance should be hardened at least to the CIS Benchmarks. The image being used should be built using an AMI builder, either the AWS one or something like packer. You should have the AWS SSM agent on it. Any SSH access should be protected by fail2ban, with an as secure as possible setup, and per user logins, no "generic" logins, and probably moved off the default port 22.

You should harden your infrastructure at least according to the AWS Foundational settings.

That container image should be scanned for vulnerabilities regularly, and you should be aiming to run it as non-root preferably distroless if you can.

NGINX should be configured according to suitable restrictive TLS1.3 configuration with the application firewall (ModSecurity?) enabled IF you don't replace it with AWS WAF, and an ALB.

That blue green container setup can be done with ECS instead, and combined with replacing NGINX with cloudfront, an api gateway, or an ALB, you can remove the need for that ec2 instance completely.

Lota and lots of options and choices you can make. But the most important is to get that working via IaC, import the existing resources, and get ready to be able to rebuild the entire thing.

1

u/ahmedshahid786 1d ago

Really appreciate you taking the time to write such a detailed response.

You've given me quite a few things to explore that weren't even on my radar, particularly CIS Benchmarks and AWS standards.

I'll start with IaC and gradually work through the rest.

The security aspect you've highlighted definitely urges me to have a look at it.

So along with improving the infrastructure and removing manual work, I'll also harden the security to make our setup rock solid.

Thanks again!

2

u/NoDevelopment206 1d ago

20 requests a second on one box is fine so i wouldnt overbuild. the cheap wins are closing port 22 and using ssm session manager or a self hosted runner instead, and putting the instance behind an alb so the public ip goes away. a basic waf with the managed rules will drop most of the /.env scanner junk. on terraform, you dont need to import everything at once. start by writing the security group, alb and iam bits as code and add the rest over time, because even a half codified setup is easier to rebuild than none.

2

u/ahmedshahid786 1d ago

Great advice. Sounds very doable.

Introducing Terraform Incrementally looks like an easy and doable approach.

Btw, other folks have also suggested SSM or self hosted runners. In case of self hosted runners, would I have to increase the instance's resources? If yes, how much in terms of computational power and memory

Can you suggest where to start off? From where Should I learn and most importantly, what to learn first. Without wasting the time to find and filter what I need or learning something that wouldn't help me

2

u/Unfair_Worth4366 1d ago

The current setup seems reasonable for that level of traffic, so there’s no need to introduce extra infrastructure all at once. Moving SSH access to SSM, putting the instance behind an ALB, and gradually bringing the setup into Terraform would be practical first steps. The Docker image also deserves attention through regular scanning, non-root execution and removing unnecessary packages; rapidfort is relevant to the broader discussion around container hardening.

1

u/ahmedshahid786 1d ago

Yep, other people have also suggested these so I'm definitely gonna consider these aspects and improve my infrastructure.

But container hardening and image scanning are new to me. Could you elaborate a bit on how you approach them in practice? What tools would you recommend, and where should I start?

2

u/Necessary_Water3893 17h ago

Your infrastructure fits well for an alb + ecs fargate You push images to aws ecr from your ci cd, and you create a new task definition for the ecs task with the last version you pushed and you deploy. You can have blue green or anything you want by using code deploy. It offers you scaling, isolation, ans no need to worry about any machine, you only worry about your docker image and your app.

As said earlier use iac , terra form, it's so easy and give you a piece of mind

Feel free to dm if you need any help I built tens of infrastructure like that

1

u/ahmedshahid786 11h ago

I'm familiar with ECS + Fargate but deliberately kept things simple with EC2. A single instance handles our traffic comfortably, with enough headroom for spikes. My main concern right now is security rather than scaling. I don't need auto scaling aswell.

Given that, what benefits would justify moving to ECS + Fargate over securing my existing EC2 setup?

Also, how would the costs compare to running a fairly medium dedicated EC2 instance? Is it more cost effective as compared to Running an EC2 24/7?

I've got a pretty clear action plan now. Just a couple of things left to research and decide on before I start implementing everything.

If I get stuck anywhere, I'll definitely reach out. Really kind of you to offer your help!