amazon-web-servicesaws-lambdaamazon-personalize

Trouble writing real-time Segment events to Amazon Personalize using PutEvents


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.


Solution

  • 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.