Here is a Bash script you can use to retrieve a test message from a Kinesis Stream. The script can be modified to read from either the oldest message (TRIM_HORIZON
), the latest message (LATEST
), or go back over the last hour (AT_TIMESTAMP
). The default is LATEST
.
1#!/bin/bash23JQ_CHECK=$(which jq)4if [ -z "$JQ_CHECK" ]; then5echo6echo "This script requires the jq JSON processor. Please install for your OS from https://stedolan.github.io/jq/download/"7echo8exit 19fi1011if [ $# -ne 1 ]; then12echo13echo "usage: $0 <stream_name>"14echo15exit 116fi1718# Set the stream name19STREAM_NAME=$12021# Choose the iterator type:22# TRIM HORIZON is for starting at the begining of the Kinesis Stream.23# This can take a while if you have a lot of records.24# To use TRIM HORIZON, uncomment the following line:25# TYPE=TRIM_HORIZON2627# AT_TIMESTAMP allows you to go back to a point in time. This is set for going back one hour28# To use AT_TIMESTAMP, uncomment the following two lines:29# TIMESTAMP=$(($(date +%s) - 3600)).00030# TYPE="AT_TIMESTAMP --timestamp $TIMESTAMP"3132# LATEST means start at the most current point in the stream and read forward33TYPE=LATEST3435# Get a list of shards36SHARDS=$(aws kinesis list-shards --stream-name $STREAM_NAME | jq -r .Shards[].ShardId)3738# Get all the starting points39SHARD_ITERATOR=()40i=041for shard in $SHARDS ; do42SHARD_ITERATOR[$i]=$(aws kinesis get-shard-iterator --shard-id $shard --shard-iterator-type $TYPE --stream-name $STREAM_NAME --query 'ShardIterator')43i=$((i+1))44done4546# Start getting events from all shards and display them47while [ 1 ] ; do48len=${#SHARD_ITERATOR[@]}49for (( i=0; i < $len; i++ )); do50DATA=$(aws kinesis get-records --limit 50 --shard-iterator ${SHARD_ITERATOR[$i]})51SHARD_ITERATOR[$i]=$(echo $DATA | jq -r .NextShardIterator)52ROWS=$(echo $DATA | jq -r .Records[].Data?)53for row in $ROWS; do54echo $row | base64 -d | jq .55done56done57done