0

I am using Segment to record user behavior in my application, and am sending these events to an AWS Lambda function, which then writes these Segment events to an event tracker in a recommender on Amazon Personalize in order to support real-time updates to the recommender. To implement this I followed the example here.

Following the example linked above, in the AWS Lambda function I am trying to write Segment events to Amazon Personalize as follows:

response = personalize_events.put_events(
                trackingId = os.environ['personalize_tracking_id'],
                userId = userId,
                sessionId = event['anonymousId'],
                eventList = [
                    {
                        "eventId": event['messageId'],
                        "sentAt": int(dp.parse(event['timestamp']).strftime('%s')),
                        "eventType": event['event'],
                        "properties": json.dumps(properties)
                    }
                ]
            )

However in the logs on CloudWatch I am seeing the following error:

An error occurred (InvalidInputException) when calling the PutEvents operation: Invalid trackingId <trackingId redacted> in account <redacted>

I have double-checked the trackingId associated with the event tracker on the recommender and confirmed that the trackingId matches.

I'm not exactly sure where to go from here. Any help would be greatly appreciated.

abfine
  • 1
  • 1
  • Your code looks fine. For the trackingId, make sure you're not using the event tracker's ARN. The trackingId should be a UUID. You can also test PutEvents and a sample payload in the AWS console under Datasets -> your interactions dataset -> Modify dataset -> Create record. This will allow to test a working PutEvents payload and then compare that to what you're sending in your Lambda function. – James J Jun 30 '23 at 21:54
  • Thank you James! It turned out to be an issue with the region configuration, as correctly surmised in the comment below. Thank you for the tip about Create Record. – abfine Jul 01 '23 at 20:45

1 Answers1

0

This error is a consequence of the boto3 region configuration. If your Lambda is in a different region than your Personalize resources (e.g. the Segment configuration requires your Lambda to be in us-west-2, and your Personalize resources are in us-east-1) you will need to configure it like so:

...

from botocore.config import Config

boto_config = Config(
    region_name="us-east-1",
)


def lambda_handler(event, context):
    personalize_events = boto3.client("personalize-events", config=boto_config)
    ...

This will allow the boto3 client to find the specific event tracking ID in your account.