AWS lambdba Boto3 Python – Reading from Kinesis stream throws Invalid type for parameter ShardIterator, value: {'ShardIterator':

Question:

I have a lambda that actually puts some data into the kinesis stream. That works perfectly fine. I can see the data written into the kinesis in the cloud watch logs.
Now I am writing another Lambda that reads that kinesis stream when there some data is available on the stream. I am able to get the shard iterator. But when I pass it to the get_records, it says invalid parameter

  import boto3
  import json  
  import time      
  from pprint import pprint
  import base64

  kinesis_client = boto3.client('kinesis')

  def lambda_handler(event, context): 
        response = kinesis_client.describe_stream(StreamName='teststream')

        my_shard_id = response['StreamDescription']['Shards'][0]['ShardId']
        #print(my_shard_id)
        shard_iterator = kinesis_client.get_shard_iterator(StreamName='teststream', 
        ShardId=my_shard_id, ShardIteratorType='LATEST')
        #pprint(shard_iterator)
        #I am able to print the shard iterator but when I do get_records It says Invalid type 
        #for parameter ShardIterator, value: {'ShardIterator': 

        record_response = kinesis_client.get_records(ShardIterator=shard_iterator,Limit=10)
         #pprint(record_response)

This is the error I get when get_records is called.

enter image description here

Asked By: Jason

||

Answers:

The boto3 get_shard_iterator() call returns a JSON object:

{
    'ShardIterator': 'string'
}

Therefore, use:

shard_iterator_response = kinesis_client.get_shard_iterator(
    StreamName='teststream', 
    ShardId=my_shard_id,
    ShardIteratorType='LATEST'
)

shard_iterator = shard_iterator_response['ShardIterator']
Answered By: John Rotenstein