Advice for unattended vending machine startup with basic api, crud, and database needs

Hi all,

I’m debating between using Lambda or ECS Fargate for our restful API’s.

• Since we’re a startup we’re not currently experiencing many API calls, however in 6 months that could change to maybe ~1000-1500 per day

• Our API calls aren’t required to be very fast (Lambda cold start wouldn’t be an issue)

• We have a basic set of restful API’s and will be modifying some rows in our DB.

• We want the best experience for devs for development as well as testing & CI.

• We want to be as close to infrastructure-as-code as we can.

My thoughts:

My thinking is that since that we want to make a great experience for the devs and testing, a containerized python api (flask) would allow for easier development and testing. Compared to Lambda which is a little bit of a paradigm shift.

That being said, the cost savings of lambda could be great in the first year, and since our API’s are simple CRUD, I don’t think it would be that complicated to set up. My main concern is ease of testing and CI. Since I’ve never written stuff on Lambda I’m not sure what that experience is like.

We’ll be using most likely RDB Aurora for our database so we’ll want easy integration with that too.

Any advice is appreciated!

Also curious on if people are using SAM or CDK for lambda these days?

Simple is good. Lambda is harder to do everything on from deployment to monitoring to observability to testing. And you think cold starts won’t be an issue until they are. You can keep them warm though. The scale to nothing is great, but has tradeoffs.

If it’s a startup, your job is to make code build testing and deployment as simple as possible so you can iterate, fix and ship features as fast as possible. My personal opinion is ECS Fargate allows you to do that simpler than lambda. It’s pretty cheap if you start with small containers too.

Whilst developing on Lambda may not be as easy as developing an ECS/ Fargate container, although you can run a docker image within Lambda now. I would say to not underestimate the overhead of maintaining a running service where you’re responsible for the full image. Keeping a Docker image patched and monitoring your containers for anomalous activity, then reacting to that can introduce some additional challenges as the shared security model puts more responsibility upon you when using ECS over Lambda.

For 1500 hits a day, you could avoid lot of complexity and upskilling by using a simple low cost EC2 instance hosting your app.

Container via Lightsail Containers or App Runner or Fargate.

IMO, LS Containers is the easiest, cheapest, HA containers; best for MVP with moderate traffic. It’s AWS best kept secret because it’s Fargate under the hood.

Regular RDS works fine unless you really need Aurora where I/O might give you bill shock.

hard disagree. i find people that say this are often using lambda poorly.

see my response to op

CDK all the way.

If you do choose lambda then you must remove flask and do routing with aws.

otherwise use ecs. lots of good comments in this thread already about it.

I prefer lambda but play within the rules. avoid waiting and looping and the flask thing i mentioned. batch where you can and decouple producers from consumers.

it’s not just, “oh, I’m going to use lambda.” It has to be done right if you decide that’s the path you’re going to take.

Having done both Fargate web app and Lambda web apps, I would say you’ll probably be successful with either one. IMO Lambdas can certainly satisfy your needs - there’s a few hoops to jump through for setting up local testing, or you can just drop them into the web and test them there, merging them into your pipeline when they’re ready. Monitoring (imo) is easy - everything goes to cloudwatch either way.

On the fargate side, image management is a different challenge, but perfectly manageable. You can easily add functionality and version your images and move fast. Networking and capacity planning become more of a concern but again, not insurmountable.

CDK and SAM both handle both things fine, with Fargate there’s an additional question of “is this even running?” in addition to pushing out image updates whereas in lamdba you’re just kinda deployed and ready.

I prefer CDK to SAM, but I’ve got deployments for Lambda using both. Hope it helps.

We use fastapi with mangum. This let’s us deploy a containerized lambda that we can also run locally and should in the future be able to move to ECS with minimal effort.

Aws chalice framework is a great way to do flask like development within lambda.

After reading your comment, I totally agree with you. No reason you can’t use lambda if you set it up properly, but you have to play by lambda rules.

would prefer any comments to be on my post to op.

maybe you’ll see some rules you can use the next time you want to use lambda

At 1500 hits a day I think you could have a solution that is almost free to run by going all in on serverless.

A very flexible and performant stack might look like this

  • API Gateway - HTTP (cheaper and faster than REST but less features)
  • Lambda
  • SQS for anything that can be deferred to keep endpoints as fast possible and/or tasks that can fan out well
  • DynamoDB instead of RDS, it’s fast, cheap and a whole lot easier to manage. Look into single table design.

At work we define everything in terraform so it’s pretty easy to spin up new lambdas with a cicd pipeline setup and ready to go. We are writing our services in Golang and have had great performance using a similar stack to what I outlined above.

Also there is a bunch of integrations between these service and others. E.g. you can setup dynamodb streams to write changes to a parquet formatted file in s3 then later query it with Athena.

I recommend reading up on the lambda instance lifecycle.

It’s very important to setup all your dependencies that are not request specific during the first initialisation, also try to limit the number of requests for things like credentials, appconfig can be a good solution or environment variables for things that are fairly static and not secret. And depending on how easy it is to do requests in parallel in your language of choice, make use of it to keep execution time low.

I just want to point out that - especially at 1,500 requests PER DAY, you could EASILY have one single Lambda function as your whole API. I‘ve done that several times with Fastify (I think it’s a Flask equivalent in JavaScript) and there’s absolutely no problem with that.

https://www.serverless.com/blog/flask-serverless-api-in-aws-lambda-the-easy-way

No one forces you to have one Lambda per HTTP endpoint. In fact, Lambda and ECS Fargate basically share the same foundation - Firecracker MicroVMs.

You could also bundle your whole API as a container and use that to deploy your code to Lambda. You could use API Gateway with proxy route to point to your Lambda. You can also use the Lambda function URL if you don’t need to have a specific domain. Routing will happen inside your Lambda.

Take a look at DynamoDB if you already have a solid understanding of what you want to build and how you want to access your data. You could even consider S3 as a DB replacement, depending on your usecase.

Your bill is very like some cents per months.

We use a similar setup but I would advocate for packaging your code into a zip rather than using a container as there will be better performance that way.

„If you do choose Lambda then you must remove flask…“

No you don’t. There is not a single technical constraint that’s preventing you from using an HTTP routing framework (Flask, Express, …) in a Lambda function when dealing with low to moderate traffic. 1,500 requests per day is a joke. Assuming a slow request would take 200ms all in, a single Lambdalith could already handle 5 requests per second. That‘s almost half a million requests per day from a single Lambda function.

People tend to be so dogmatic about single purpose Lambda functions that it’s not even funny anymore..

yeah, do that if you want but it is one of the anti patterns defined by aws

Yeah, when reading AWS documentation you should also think about who they want to address. Start with a monolith if you already know you’re building something small with low traffic, adapt / enhance / split when needed. Don’t pre-optimize, be efficient.

„Just because you can doesn’t mean you should“ goes both ways.