Running Containers on AWS Lambda

Running Containers on AWS Lambda

Author here.

I had an interesting service I needed to build and I didn’t realize that you could run full containers on AWS Lambda. I’m not an AWS expert at all, but it worked out really great.

I wrote up my process in the article.

Side note: the service I built ended up on /r/programming and hacker news at the same time, and even though the service shells out to the command line and is a bit slow, it didn’t matter because of the scaling lambdas gave me. Service is here: https://earthly-tools.com/text-mode

great article on the overview.

What size were the containers when you shipped it ?

Also, I think you should run the npm install as:

npm install --production

This will remove any devDependencies you have like typescript etc.

Thanks and great npm tip!

The image ended up at 776 MB which seems huge. I have a follow-up article I’m working on where I use go. I don’t really have a good understanding of node.js and its ecosystem so going to go made things a lot easier for me (and I got a smaller image).

Also use multi stage builds. This lets you end with a lean final image that only has the runtime, the JS artifacts and prod deps. This can be further optimized using a bundler that will tree shake the deps. So you will end with a single bundled file and the node runtime.

I had no idea you could run Docker containers via Lambda. That is pretty compelling.

Now, if only they’d add GPU support …

Yeah, my go solution works like that. I build the go binary and copy it across. I’ll write a follow-up because it’s surprising how much faster the go version is.

I was wishing for the same thing!

There are some deep learning things I’d love to be able to hook up to lambda.

Specifically some of the image super-resolution upsizers.

Even a Fargate option would be really nice. The additional work required to use EC2 instances in that workflow is (comparatively) burdensome. (Can you guess what I’ve been doing all week?)

Not surprising if you think about it. In the final stage your artifact is a static binary. When paired with a base image like distroless it’s about as lean as any image can get

Interesting. Do you have any links to resources which describe how you’d do this?

Do you have any high level feeling about cost differences?

i.e. how much would this cost, for some arbitrary amount of traffic, compared to just directly using AWS node 12 runtime?

IMO, Adding containers was a huge step forward for making tooling/deployment less messy during development/testing, and then going to production.

Without getting in to much detail, I built out a few terraform modules that can be referenced to build and deploy containers to lambda (only assumes terraform/docker on the build/deploy machine):

https://github.com/atheken/terraform-building-blocks

I think you make your own containers “from scratch”, but if you derive from their base images, you have a lot less work to do to get your lambda running.

Neat. I’ll have a look.

And, yes, I agree that containers makes Lambda much less messy. I always found the process of building and deploying a zip archive to be unnecessarily complicated. Especially so, compared to GCP Cloud Functions, which do the obvious thing and use requirements.txt, package.json, etc. to bootstrap the execution environment. Granted, using containers doesn’t relieve you of that burden, but it provides a (relatively) uniform workflow.

Yeah, I guess I replied to the wrong comment.

I vastly prefer containers to conventions based on what “magic files” are in the repo.

A process is a useful abstraction for a unit of computing, combining it with a convention for passing args and setting env vars. How the process _does the stuff_ shouldn’t matter to the scheduler.

Lambda does something interesting when you deploy a new function. I believe it pulls from ECR right then and keeps a copy.. somewhere. As far as I know, you will pay a small amount for storage in ECR, and maybe some internal b/w to lambda, but I wouldn’t expect any other major changes in cost.

yeah, agreed. The cost besides ECR is the same as other lambda’s