How to create live stream using AWS IVS
Tags: aws, livestream
Published: 2022-06-01 08:34:21

Nowadays, everyone uses the live stream to interact with a large audience. It is used in live classes, virtual events etc. You can use twitch, Facebook live or youtube live to interact with your audience. But if you want to include the live stream directly on your product, there are multiple tools available for live streaming, like mux. You can visit the mux website to check the integration document. In this blog, I am going to talk about amazon IVS. It can help you to add the low latency live streaming feature to your product.

Amazon interactive video service is very simple to integrate with your product. There are two-part to live streaming. First is the source that provides the input, and another is the destination that consumes the live stream. Some terminology of amazon IVS is channel, stream configuration, playback configuration, and recording configuration. Recording configuration has the information of where recordings get saved. Playback configuration contains the playback URL that use to consume the stream and stream configuration has the server URL and stream key that use on the source. Stream, playback and recording are part of the channel.

First, create an s3 bucket that we can use in the create recording configuration API. Open the AWS console and select s3 from the storage section. Click on create bucket available on the right side. Provide the bucket name (ivs-test) and select ap-south-1 in the AWS region dropdown. We will use this name in the API request body to create the recording configuration. 
Open postman and click on send request. Set the type post in the request tab, and add the URL https://ivs.ap-south-1.amazonaws.com/CreateRecordingConfiguration. This API required authorization. So we have to add the authorization, and here we need AWS Signature. Use the access key and secret access key that we got after creating an IAM user(you can check at the end of this blog to check how to create IAM user). Set the AWS region ap-south-1, becasue we are using the Mumbai region and service name ivs. The request body of this post requirest will be like
`{
    "destinationConfiguration": {
        "s3":{
            "bucketName": "ivs-test"
        }
    }
}`
The bucket name is the name of the s3 bucket that we created. In response, we will get the arn that is required in the next API call. If there is an error make sure that you are using the correct bucket name, assigned the correct policy to the user and region in the API baseurl, and authentication is the same and correct.

After creating arn we need to create the channel. API URL to create a channel is https://ivs.ap-south-1.amazonaws.com/CreateChannel. This is a post request and requires authorization same as creating a recording configuration. The request body of this API is 
`{
    "authorized": false,
    "latencyMode": "LOW",
    "name": "mytest-channel-2",
    "recordingConfigurationArn": "arn:aws:ivs:ap-south-1:675448338706:recording-configuration/wmNiQ4Bax4DH"
}`
name is the name of the channel that you want, recordingConfigurationArn is the arn that we received in the response from the create recording configuration api. If everything is correct, we get the response that includes channel and streamKey. 
`{
   "channel": {
       "arn": "arn:aws:ivs:ap-south-1:675448338706:channel/SEWXauAmanyO",
       "authorized": false,
       "ingestEndpoint": "2d161ce0df68.global-contribute.live-video.net",
       "latencyMode": "LOW",
       "name": "mytest-channel-2",
       "playbackUrl": "https://2d161ce0df68.ap-south-1.playback.live-video.net/api/video/v1/ap-south-1.675448338706.channel.HeWXauAnoTyO.m3u8",
       "recordingConfigurationArn": "arn:aws:ivs:ap-south-1:675448338706:recording-configuration/wmN8b3BwhLDH",
       "recordingS3BucketName": "",
       "tags": {},
       "type": "STANDARD"
   },
   "streamKey": {
       "arn": "arn:aws:ivs:ap-south-1:675448338706:stream-key/q07N0M98o0bZ",
       "channelArn": "arn:aws:ivs:ap-south-1:675448338706:channel/HeWXauAloTyO",
       "tags": {},
       "value": "sk_ap-south-1_q07N0MT9k9bZ_d7YdhtgtxpANaaghBahuxZlvbgFhqc"
   }
}
`
'playbackurl' is the  URL that is used to watch live stream i.e on the destination side. 'ingestEndpoint' and the value in the streamKey used to create the RTMP URL that used on the source side. RTMP url will be rtmps://2d161ce0df68.global-contribute.live-video.net:443/app/sk_ap-south-1_q07N0MT9k9bZ_d7YdhtgtxpASaaPhPaUUxZlvbgFhqc.

**Create IAM user**
We need an IAM user that has the access to s3 bucket and ivs channel. Open the AWS console and click on IAM in the security, Identity, and compliance section. From the left-hand side select policies and click on create policy. Select JSON and copy-paste this policy. Again in the IAM section select users from the left-hand side and click on Add user. Provide a user name and also select access key - programmatic access. Click on the next permission button at the bottom. On the next screen select, attach the existing policy directly and select the policy that we just created. After creating a user you will get the access key id and secret access key. Save it in a safe place, and don't share it with others.