Tuesday 15 February 2022

AWS Lambda vs Fargate

My manager, 7 finger Lucy
The following story is a re-enactment of what actually happened.  Names, dates, species and quite a lot more have been changed to protect those involved in the crimes against code...

Smoke emerged from management ears.  It had been months.  Six, perhaps, since our principal dev-ops ninja had started converting our twenty-eight Windows web services to run in containers.  Not a single container had been demoed yet.  "Stephen, we'd like you to take over this project, and... err... get it done quickly."

"Challenge accepted," I replied, retrieving an extra keyboard, pulling up my sleeves, and tightening my head band.  Browser open, my fingers sprang into action, "Google, what is...?"

I know what a container is, but err... yeah, my experience lacked... experience.

"So, principal dev..."  (I'll call her Bob)

"...Bob, what have we done so far?"

"600 hundred gazillion lines of pure code, she winked... YAML, Kubernetes, Terraform, nginX, Helm Charts, Docker, WSL 2, Artifactory and Windows Terminal.  She licked her lips.  Here, let me show you..."

Pete
The challenge, it appeared, seemed a bit more challenging that I had anticipated.  "Pete," I called our AWS consultant, because his name was Pete.

"Fargate," Pete replied.  A gong resounded.  I don't know why.  "Less problems, with Fargate, you will have."

I liked the idea.  I liked AWS.  Fargate was AWS.

I googled, "YAML, Kubernetes, Terraform, nginX, Helm Charts, Docker, WSL 2, Artifactory, Windows Terminal, Fargate."

Then I YouTubed, "YAML, Kubernetes, Terraform, nginX, Helm Charts, Docker, WSL 2, Artifactory, Windows Terminal, Fargate."

"I no longer have any more to teach you, my son," said the voice in my head.  It sounded like Pete.  "A container, in docker, you must create.  To Fargate, it must go."

"Kubernetes vs. Fargate," I typed into YouTube.  "EKS vs. ECS," it suggested.  "Fine, EKS vs ECS," I replied.  "ECS is simpler," it replied.

"Simple, I like simple," I told YouTube.

I ignored Bob's Git branch.  "600 hundred gazillion lines of pure code..." ignored.

I powered up PowerShell, installed WSL 2, formed my CloudFormation, setup a GitLab runner, bashed a new Bash script, docked a container in Docker, shipped it to ECR and Artifactory, and sailed it on to Fargate.

To be fair, it took me two months to release our first container.  There were hurdles, every step of the way, from setting up the GitLab runner to getting the containers to scale fast enough.  What I had achieved, however, was a custom solution that allowed us to 

  • continue to deploy to our individual, development EC2 environments
  • continue to be able to debug and step through any part or all of our application locally
  • build and run auto-scaling Linux containers in multiple environments
The cherry on the cake, however, was that I could show a colleague what to do for the rest of the application, and by simply copying and pasting a Dockerfile (renaming the entry point), and configuring a few values in a JSON file (CPU, memory, storage, scaling profile) he was able to convert a web service.  Setting up the remaining 27 services took a couple of days.

I patted myself on the back.


"Lambda, an alternative to containers, is.  To Lambda, your code could go."

"But you said,..."

"Mmmm...  Simpler.  Simpler your solution could be."


I remembered Lambdas.  Python scripts, that timed out and ran out of memory... very quickly.  And we were charged... for every call!

"Que?"  I asked the voice in my head.

"Not just Python... all languages.  Compiled code and NuGet packages run, it can!"

"Quicker, your API calls must be."

"Less memory, your APIs use, they must."

"For every million calls, charged, you will be."


And so it was, that I found myself questioning whether or not I had made the right choice.  I think the gist is, that there are always going to be alternatives.  Often there isn't a right choice.  While prepping and doing research to find a better solution before starting is optimal, when deadlines crop up, there  may not be enough time to do enough researching and prototyping.  Sometimes it's necessary to ask the experts what they think, and go with that.  


In the case of Lambdas vs ECS Fargate, I think the most important considerations are firstly, can it actually be done as a Lambda?  Lambdas are, at the time of writing this, restricted to 15 minutes and 10GB of RAM.  Secondly, it's price.  If the code base is small, loads quickly and only needs to run for a few seconds at a time, Lambda could be cheaper.  For long running code, with large binaries, that doesn't need to scale up and down as quickly, Fargate is cheaper.  If you want to keep data cached in memory for a long time, Lambda is not going to work.  If you want to prototype something for free, and don't expect much traffic, Lambda might be the way to go.  As with many services in AWS, one has to do the price math, to make good decisions.


Here's a useful breakdown for anyone who would like to know more about the costs.

No comments:

Post a Comment

How to reduce complexity and know everything.

Quality We have a great QA. His code is like a Rolex. You look at his gherkin, and smile.  It says what it's testing, in plain English, ...