|
#!/bin/bash |
|
set -euo pipefail |
|
|
|
RESOURCE_NAME="test-$(openssl rand -hex 4)" |
|
export RESOURCE_NAME |
|
|
|
echo "Preparing and uploading function $RESOURCE_NAME from index.js" |
|
zip kinesis.zip kinesis.js |
|
aws --endpoint-url=http://localhost:4566 lambda create-function \ |
|
--function-name "$RESOURCE_NAME" \ |
|
--zip-file fileb://kinesis.zip \ |
|
--handler kinesis.handler \ |
|
--environment "Variables={PRIMARY_ELASTIC_HOST=127.0.0.1:4571,SECONDARY_ELASTIC_HOST=127.0.0.1:4571,ENV=LOCAL}" \ |
|
--runtime 'nodejs12.x' \ |
|
--role arn:aws:iam::000000000000:role/lambda-dotnet-ex |
|
|
|
echo "Invoking function $RESOURCE_NAME" |
|
aws --endpoint-url=http://localhost:4566 lambda invoke --function-name "$RESOURCE_NAME" /dev/stdout |
|
|
|
echo "Creating dynamodb table $RESOURCE_NAME" |
|
aws --endpoint-url=http://localhost:4566 dynamodb create-table \ |
|
--table-name "$RESOURCE_NAME" \ |
|
--attribute-definitions 'AttributeName=a1,AttributeType=S' \ |
|
--key-schema 'AttributeName=a1,KeyType=HASH' \ |
|
--provisioned-throughput 'ReadCapacityUnits=1,WriteCapacityUnits=1' |
|
|
|
echo "Creating kinesis stream $RESOURCE_NAME" |
|
aws --endpoint-url=http://localhost:4566 kinesis create-stream \ |
|
--stream-name "$RESOURCE_NAME" \ |
|
--shard-count 3 |
|
STREAM_ARN=$(aws --endpoint-url=http://localhost:4566 kinesis describe-stream \ |
|
--stream-name "$RESOURCE_NAME" | |
|
jq -r ".StreamDescription.StreamARN") |
|
|
|
echo "Connecting table $RESOURCE_NAME to kinesis stream $STREAM_ARN" |
|
aws --endpoint-url=http://localhost:4566 dynamodb enable-kinesis-streaming-destination \ |
|
--table-name "$RESOURCE_NAME" \ |
|
--stream-arn "$STREAM_ARN" |
|
|
|
echo "Creating event-source mapping for stream $STREAM_ARN" |
|
aws --endpoint-url=http://localhost:4566 lambda create-event-source-mapping \ |
|
--function-name "$RESOURCE_NAME" \ |
|
--event-source "$STREAM_ARN" \ |
|
--batch-size 10 \ |
|
--starting-position TRIM_HORIZON |
|
|
|
echo "Putting new record to table $RESOURCE_NAME" |
|
aws --endpoint-url=http://localhost:4566 dynamodb put-item --table-name "$RESOURCE_NAME" --item '{"a1":{"S":"123"}}' |
|
|
|
echo "Putting existing record to table $RESOURCE_NAME" |
|
aws --endpoint-url=http://localhost:4566 dynamodb put-item --table-name "$RESOURCE_NAME" --item '{"a1":{"S":"123"},"a2":{"S":"456"}}' |
|
|
|
echo "Updating attribute on existing record in table $RESOURCE_NAME" |
|
aws --endpoint-url=http://localhost:4566 dynamodb update-item \ |
|
--table-name "$RESOURCE_NAME" \ |
|
--key '{"a1":{"S":"123"}}' \ |
|
--update-expression 'SET #H = :h' \ |
|
--expression-attribute-names '{"#H":"a2"}' \ |
|
--expression-attribute-values '{":h":{"S":"Hello world"}}' |