Kinesis scripts
Use the following Bash scripts to automate the creation of a Kinesis Stream and retrieve a test message. Copy the code below and save it to your computer, for example as create_kinesis_stream.sh or retrieve_test_message.sh.
Run chmod +x create_kinesis_stream.sh to make it executable.
You also need jq, a command line JSON processor the script depends on. For installation instructions for your OS, see the jq download page.
The script also depends upon the AWS CLI, which you will need to install and configure before executing the script.
The script takes two arguments: your chosen AWS Kinesis Stream name and a shard count.
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://jqlang.org/download/"7echo8exit 19fi1011if [ $# -ne 2 ]; then12echo13echo "usage: $0 <stream_name> <shard_count>"14echo15exit 116fi1718# Set the stream name19STREAM_NAME=${1:-twilio-events}20SHARD_COUNT=${2:-1}2122# Create the initial stream23aws kinesis create-stream --stream-name $STREAM_NAME --shard-count $SHARD_COUNT24if [ $? -ne 0 ]; then25echo "Kinesis create failed"26exit 127fi2829# Get the ARN for the Kinesis Stream30KINESIS_ARN=$(aws kinesis describe-stream --stream-name $STREAM_NAME | jq -r .StreamDescription.StreamARN)3132# Create the policy for the Kinesis Stream33POLICY_ARN=$(aws iam create-policy --policy-name twilio-events-kinesis-write --policy-document '{34"Version": "2012-10-17",35"Statement": [36{37"Sid": "Quickstart0",38"Effect": "Allow",39"Action": [40"kinesis:PutRecord",41"kinesis:PutRecords"42],43"Resource": "'$KINESIS_ARN'"44},45{46"Sid": "Quickstart1",47"Effect": "Allow",48"Action": [49"kinesis:ListShards",50"kinesis:DescribeLimits"51],52"Resource": "*"53}54]55}' | jq -r .Policy.Arn)5657if [ -z "$POLICY_ARN" ]; then58echo "Failed to create IAM policy"59exit 160fi6162# Generate a random external ID63EXTERNAL_ID=$(openssl rand -hex 40)64if [ -z "$EXTERNAL_ID" ]; then65echo "Failed to generate external ID"66exit 167fi6869# This is Twilio's intermediary role, which needs permission to assume the70# role created below (Twilio assumes your role via a two-hop chain, so the71# trust policy must name this specific role, not just Twilio's account)72TWILIO_ASSUME_ROLE_ACCOUNT=${TWILIO_ASSUME_ROLE_ACCOUNT:-arn:aws:iam::177261743968:role/egress-domain-prod-isolated}7374# Add the random external ID to the role ARN75# More information can be found here: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html76ROLE_ARN=$(aws iam create-role --role-name twilio-events-kinesis-write --assume-role-policy-document '{77"Version": "2012-10-17",78"Statement": [79{80"Effect": "Allow",81"Principal": {82"AWS": "'$TWILIO_ASSUME_ROLE_ACCOUNT'"83},84"Action": "sts:AssumeRole",85"Condition": {86"StringEquals": {87"sts:ExternalId": "'$EXTERNAL_ID'"88}89}90}91]92}' | jq -r .Role.Arn)9394if [ -z "$ROLE_ARN" ]; then95echo "Failed to create IAM role"96exit 197fi9899# Finally attach the policy and the role100aws iam attach-role-policy --role-name twilio-events-kinesis-write --policy-arn $POLICY_ARN101102if [ $? -ne 0 ]; then103echo "Attaching policy to role failed"104exit 1105fi106107# Print out the values needed for creating the sink in nice JSON108echo "{"109echo '"arn":"'$KINESIS_ARN'",'110echo '"role_arn":"'$ROLE_ARN'",'111echo '"external_id":"'$EXTERNAL_ID'"'112echo "}"
You can use the following Bash script to automate the retreival of a test message. Copy the code below and save it to your computer, for example as retrieve_test_message.sh.
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://jqlang.org/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 beginning 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