Add these functions to your serverless.yml
This will make the serverless.yml file look like this
Then we need the two handlers for those API functions. Notice how we create src/handler/api a directory which we’ll use to move our handlers over time.
All the code to handle the API is already there (we added it in Day 3) so the handlers are pretty simple:
There is really nothing to it setting up this quick controller that gets an argument (userId or anonymousId), looks for it in the DynamoDB and responds with results (if any)
To test the attribution and anonymous to userId mapping a little better let’s feed the system some events. I have created some sample events (all JSON files). So download those and put them in the /events folder.
Archive of segment events (5 in total).
Now make sure all your dynamoDB sma tables are empty and run the following command to POST each event in one go
You will now be able to query users by anonymousId or userId and get a list of their sessions and referral sources.
Testing by anonymousId
Should give you something like this
And now another test by userId
Will output some more results
So let’s see what we have here? This is the response coming from the /user API
Looks good. Not sure how I am going to digest/use that data and pass it back to segment but let’s consider that a problem for later.
So we’re ready to test drive this in production. Open your serverless.yml and change the stage to prod
Quick sls deploy should create new tables, new event streams and new functions.
When the deployment is done search for the function that is linked to the POST endpoint (URL ending with /events). Copy that URL and log in to your segment account.
Go to your project > destinations > webhooks and enable the webhook to this URL.
After that you can go to the event tester to see if all works well
Now let’s let this integration run for a few days (it’s weekend after all) and check in on Monday if there are any issues/problems we need to solve.