Search code examples
pythonamazon-web-servicesaws-lambdaaws-sdkrecommendation-engine

How to use AWS Personalize real time campaign invocations from AWS Lambda?


AWS Personalize is a new service by Amazon for personalized recommendations. Since this service is not yet integrated into boto3, how can I use it from Lambda function. There is a way to invoke this from local using python with some additional steps mentioned here in step 3-6 AWS Python SDK

However this involves downloading some JSON's and adding models to AWS service.

Some help would be appreciated!


Solution

  • I got it working like this:

    import boto3
    import os
    import json
    import importlib
    
    path = '/tmp/models/personalize-runtime/2018-05-22/'
    os.makedirs(path)
    
    BUCKET_NAME = 'personalize-dataset' # replace with your bucket name
    KEY = 'personalize-runtime.json' # replace with your object key
    FILENAME = '/tmp/models/personalize-runtime/2018-05-22/service-2.json'
    
    s3 = boto3.resource('s3')
    s3.Bucket(BUCKET_NAME).download_file(KEY, '/tmp/models/personalize-runtime/2018-05-22/service-2.json')
    
    
    importlib.reload(boto3)
    

    And you need to add an environmental variable to lambda: AWS_DATA_PATH == /tmp/models