Tutorial for implementing Twitch EventSub using serverless infrastructure.
Serverless solutions such as those offered by AWS, Google Cloud, Microsoft Azure, and others, are a cost effective method of integrating with Twitch EventSub. Unlike traditional hosted, or virtualized, servers which often have a monthly or yearly rental which is applied regardless of if the server is utilized or not, Serverless solutions such as API Gateway, Lambda, and DynamoDB charge based on usage and can scale up and down as needed so you will not be paying for unused capacity.
This tutorial is intended as a basic introduction to how these services can be used with EventSub, and depending on your needs they may or may not be suitable in a production environment.
The goal is to have an internet accessible callback URL for Twitch to send notifications to, 2 Lambda functions (1 for subscribing, unsubscribing, and returning status data, and 1 function for handling incoming notifications), and a DyanmoDB database to store notifications. While the creation of this setup could be entirely automated, this hands-on tutorial should lead to a greater understanding of the underlying services involved.
- Registered an app on the Twitch Developer Console, and made note of the Client ID and Client Secret.
- Registered an account with Amazon Web Services (AWS).
- API Gateway - Provides a publicly accessible callback URL, over HTTPS, for Twitch to send notifications to.
- Lambda - Stateless functions that will handle EventSub subscription requests, and handling incoming verification/notification messages from Twitch.
- DynamoDB - Document database for storing notifications, with the option for new/updated items to trigger other services/functions.
- Go to the DynamoDB console, click
Create Table
. - Enter a name for the table, and enter
id
as the Primary key. - Disable
Use default settings
, Disable Read/Write capacity auto scaling, set the provisioned capacity to 1 Read and 1 Write capacity unit. - Click
Create
. Once the table is created scroll down and make note of theAmazon Resource Name (ARN)
.
- Go to the IAM console, select
Roles
under theAccess management
section, thenCreate role
. - Select
Lambda
as the use case, clickNext: Permissions
, search for and select theAWSLambdaBasicExecutionRole
policy. - Continue through the next steps, give the role a name and make note of it and finish the role creation.
- Select the newly created role and click
Add inline policy
. - Select DynamoDB, add the Write permission
PutItem
, in the resources section add the ARN of the previously created DynamoDB table. - Review, name, and save the policy.
- Go to the Lambda console, click
Create function
. - Name the Subscriptions handler function, such as
TwitchDev-Tutorial-EventSub-Subscriptions
, selectNode.js 12.x
runtime, change default execution role and using an existing role select the IAM role created in the previous step, then clickCreate function
. - Under the
Actions
dropdown menu, select upload .zip and select theSubscriptions.zip
file included in this repo. - In the
Environment variables
section, create keys forclientID
andclientSecret
which will be the client id and secret from your app on Twitch,eventSubSecret
variable which you create yourself (10 to 100 characters), and we'll also come back later to add acallback
variable once we've created our API Gateway. ClickDeploy
. - Follow the previous steps to create a function for
Notifications
, this time with theeventSubSecret
variable again, and the variablesregion
which is your AWS region your DynamoDB table is in, such aseu-west-1
, and atableName
variable which will be the name of your DynamoDB table.
- Go to API Gateway Console, click
Create API
. - Select
REST API
(make sure NOT to select the private REST API option), choose a name, optionally a description, and clickCreate API
. - From the
Actions
dropdown, selectCreate Resource
and name itnotifications
. Then selectCreate Method
and set it toPOST
. - Select
Use Lambda Proxy integration
and type in the name of your notifications Lambda function create earlier and click save. - Select
Deploy API
from theActions
menu, name the deployment stage DEV, and copy theInvoke URL
.
- Go to the Lambda console and select the
Subscriptions
function that was created. - Add an Environment variable named
callback
with the value being yourInvoke URL
with/notifications
on the end of it, eghttps://1234.execute-api.eu-west-1.amazonaws.com/DEV/notifications
. - Deploy the changes.
- In the
Select a test event
dropdown in the top right, selectConfigure test events
, create the 3 following events:
Event Name: Subscribe
{
"type": "subscribe"
}
Event Name: Status
{
"type": "status"
}
Event Name: Unsubscribe
{
"type": "unsubscribe",
"id": ""
}
- To test run the
Subscribe
test event which should show successfully executing the Lambda function. To ensure the EventSub subscription is established run theStatus
test and the log should return that there is 1 subscription that is enabled (if it is showing as pending, wait a moment and check the status again). To test that an enabled subscription is saving notifications to DynamoDB you can trigger the topic, which in this example would be to follow my Twitch Channel and check your DynamoDB table to see if there is a new item. Once finished with testing, copy the ID of the EventSub subscription given by theStatus
test, paste that into theUnsubscribe
test and run that to unsubscribe.