Getting started with Lambda in a Virtual Environment
This tutorial walks you through the steps that you will need to take to integrate AWS Lambda functions with the Clarifai API. To begin, we need to set up some initial helper functions for users to get easy access to our clarifai_grpc package. Please note that these instructions only work on Linus machines.
python3 -m venv snowflake_lambda_venv
source snowflake_lambda_venv/bin/activate
pip install clarifai-grpc
cp -r snowflake_lambda_venv/lib/python3.7/site-packages/ python
zip -r lambda_function.zip ./python -x '*.pyc'
rm -rf python
Upload your helper functions to the AWS console
Now you will need to create a new Layer in AWS. You can learn more about Layers on AWS here.
Then in AWS console, upload that .zip file as a new Layer here https://console.aws.amazon.com/lambda/home?region=us-east-1#/layers
The new Layer should look like this:
Name: clarifaiGRPCWithPythonFolder
Arch: x86_64
runtimes: Python 3.7, Python 3.8, Python 3.9
Create an AWS lambda function (we will call it MyFunction
in the below examples) using the blank template. Reference this document to learn more about creating a lambda function. Go to the page in AWS with your lambda function, click add a layer, and then select your layer from the custom layers tab.
Give it a name MyFunction
and select python >= 3.7.
Next, insert the "ARN" (Amazon Resource Name) of the layer to the lambda. You can copy the ARN directly from the Amazon UI.
Test out your function
Use this as a template to do something in your function:
import json from google.protobuf import json_format from clarifai_grpc.grpc.api import resources_pb2, service_pb2 from clarifai_grpc.grpc.api.status import status_pb2, status_code_pb2 def lambda_handler(event, context): # We pass the request in "request" field so that the event can store other information in future if needed. request = json_format.ParseDict(event['request'], service_pb2.PostModelOutputsRequest(), ignore_unknown_fields=True) # do some work here. outputs = [] for inp in request.inputs: output = resources_pb2.Output(data=inp.data) output.data.text.raw = "some text from lambda function" output.data.concepts.extend([resources_pb2.Concept(id="lamborghini23", value=0.75)]) outputs.append(output) # just echo the incoming data for now into the response. response = service_pb2.MultiOutputResponse( #status=status_pb2.Status(code=status_code_pb2.SUCCESS), outputs=outputs, ) return { # Leave room for other fields above if needed in future. 'response': json_format.MessageToDict(response), }
Test out your function with some data:
{ "request": { "inputs": [ { "data": { "image": { "url": "https://images.pexels.com/photos/1108099/pexels-photo-1108099.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500" } } } ] } }