ECR and Lambdas Step One
When AWS
released their tool SAM
(Serverless Application Model
) it conceptually felt like a step forward. Before this many people had probably used Serverless
to simplify their process of pushing Lambdas
, and now finally AWS
was taking steps of standardizing this. I mean, it makes sense. A Lambda
is at its core something very simple. A small piece of code that you want to run under some conditions. So what’s the work you need to do to get it published?
Well, when you started to look under the hood of what SAM was doing you’d quickly get the feeling that this is a bit more complex than you’d expected. It basically transformed its yaml
into cloud formation, deployed the cloud formation, created an S3
bucket, and uploaded your program as a zip file to this bucket.
WAIT.
Didn’t we invent docker
and all of this other stuff so that it would be super easy to take one piece of code and run it anywhere? Why are we now creating buckets and running circles around the whole world to accomplish this stuff?
Well, I was very happily surprised when starting a recent lambda project and finding that the cloudformation template for lambdas now support ImageUri
. Wow, Docker
(or OCI
or whatever you want to say, soon maybe the container landscape will be like the web library landscape, one million different projects whos meaning you’ll never penetrate).
Ok, so what do you need to do?
Basically this:
- Setup an
ECR
repository - Build your image using AWS base image
- Push your image to
ECR
- Reference the image using its
URI
Devil is in the details though.
Code of the Day
Here’s a first step. This script lets you build your local docker image. (no AWS involved)
#!/bin/bash
SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}")
IMAGE_NAME="dev-blog/persist-metrics-lambda"
IMAGE_VERSION=`cat package.json | jq -r '.version'`
cd $SCRIPT_DIR/..
docker build -t $IMAGE_NAME:$IMAGE_VERSION -t $IMAGE_NAME:latest .