-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcustom_recommender_lambda.py
60 lines (49 loc) · 1.6 KB
/
custom_recommender_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
'''
This is a very simple function that provides a sample of how a custom
recommender can be implemented using a Lambda function. A real custom
recommender would make the appropriate calls to a custom model or
rule-based approach to item recommendations.
To wire up a custom recommender, add the recommender as a "lambda"
type with the Lambda function ARN like the following.
{
"namespaces": {
"my-namespace": {
"recommenders": {
"recommend-items": {
"lambda-recs": {
"variations": {
"lambda-rfy": {
"type": "lambda",
"arn": "arn:aws:lambda:us-east-1:999999999999:function:My-Custom-Function"
}
}
}
}
}
}
}
}
You will also need to modify the IAM role for the PersonalizationHttpApiFunction or
PersonalizationRestApiFunction function (PersonalizationApiExecutionRole) to add a policy
that allows "lambda:InvokeFunction" for the same function ARN in the configuration.
{
"Action": [
"lambda:InvokeFunction"
],
"Effect": "Allow",
"Resource": "arn:aws:lambda:us-east-1:999999999999:function:My-Custom-Function"
}
'''
import json
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, _):
logger.info(json.dumps(event, indent=2, default=str))
recs_to_generate = event.get('numResults', 10)
recs = []
for i in range(recs_to_generate):
recs.append({'itemId': f'item-{i+1}'})
return { 'itemList': recs }