Creating the lambda function
Publish the image to AWS ECR
Last time we used “Author from scratch” to create a lambda function on AWS. This time we use the option “Container image” for this. Here we need to provide the “Amazon ECR image URI”, which is a place where we can publish images. ECR means Elastic Container Registry. If you don’t have any ECR repository you need to create one.
You can use the awscli which can be installed by “pip install awscli" and "aws configure“.
You can use this cli to do everything you can do with AWS web interface. Let’s create a container registry using the cli.
aws ecr create-repository --repository-name clothing-tflite-images
The response looks like:
38*******013.dkr.ecr.eu-west-1.amazonaws.com/clothing-tflite-images with:
- 38*******013 as account id
- eu-west-1 is the region
- clothing-tflite-images is the name of the registry
Before you can push anything to this registry you need to login into the registry.
aws ecr get-login --no-include-email
This outputs everything with clear text password.
aws ecr get-login --no-include-email | sed 's/[0-9a-zA-Z=]/{20,\}/PASSWORD/g'
Output:
docker login -u AWS -p PASSWORD https://387546586013.dkr.ecr.eu-west-1.amazonaws.com
You need this to be able to login to this registry with docker. And then you’re able to push to this registry. To login you need the following command which takes everything what the AWS command returns as own command and immediately execute it.
$(aws ecr get-login --no-include-email)
This command should return the message “Login Succeeded”. Now you can do some configuration, to get a variable with the full URI of the image you’re going to push to ECR.
ACCOUNT=387546586013
REGION=eu-west-1
REGISTRY=clothing-tflite-images
PREFIX=${ACCOUNT}.dkr.ecr.${REGION}.amazonaws.com/${REGISTRY}
TAG=clothing-model-v4-001
REMOTE_URI=${PREFIX}:${TAG}
Now you can start the docker image again with
docker run -it --rm clothing-model:latest
Now you can tag this image
docker tag clothing-model:latest ${REMOTE_URI}
Next thing to do is pushing the image to ECR:
docker push ${REMOTE_URI}
Now you should find your docker image on AWS website.
Creating the function
Now let’s create the lambda function while using the option “Container image” as mentioned before. Now you have everything you need to configure the function.
Let’s give it a name “clothing-classification” and provide the “Container image URI” (387546586013.dkr.ecr.eu-west-1.amazonaws.com/clothing-tflite-images:clothing-model-xception-v4-001).
Instead of putting the name there you can also click “Browse images” and select the right one. There are no more changes necessary. Click “Create function”.
To test it you can invoke a test event. Go to “Test” and give it a name “pants” for example, change the code an click “Test”.
{
"url": "http://bit.ly/mlbookcamp-pants"
}
Configuring and testing it
This leads to an error “Task timed out after 3.00 seconds”. 3 seconds is not sufficient for our case. Therefore we need to change this. Go to “Configuration” –> “General configuration” –> “Edit”. There you can increase timeout to 30 seconds. That should be enough for the first time when we run the function. (It needs to get the image and needs to initialize everything)
You should also increase the Memory to 1.024 MB.
Having changed the configuration you can run the test again. The output now should be the same as you got locally, but now it comes from AWS.
Pricing
To get an imagination what are the costs for AWS lambda you can search for “aws lambda pricing” –> AWS Lambda Pricing – Amazon Web Services (aws.amazon.com/lambda/pricing)
The price depends on the region. We’re using Ireland because it’s cheaper, there you see a price for the used 1.024MB which is $0.0000000167 per 1ms (December 2023).
Calculating the price for 2.000ms based on that pricing: $0.0000334
To classify 10.000 images you have to pay $0.334