-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun_stream_workflow.sh
executable file
·114 lines (89 loc) · 2.93 KB
/
run_stream_workflow.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
set -e # auto exit script if error
############################################################
# Help #
############################################################
Help()
{
# Display Help
echo
echo "Description: script for running twitter stream pipeline with kinesis streams \
and firehose. "
echo
echo "Syntax: scriptTemplate [--image_uri|--role|--create_kinesis|-h]"
echo "options:"
echo "role ARN role for lambda function used in firehose (required)"
echo "image_uri Lambda container image (optional) "
echo "create_kinesis Creates new kinesis stream and firehose and deletes exiting (optional) "
echo "h Print this Help."
echo
}
# Get the options
while getopts ":h" option; do
case $option in
h) # display Help
Help
exit;;
esac
done
IMAGE_URI=""
FIREHOSE_LAMBDA=transform-firehouse-b64-json
LAMBDA_SOURCE_CODE=lambda_packages
while [[ "$#" -gt 0 ]]
do case $1 in
--image_uri) IMAGE_URI="$2"
shift;;
--role) LAMBDA_FIREHOSE_ROLE="$2"
shift;;
--create_kinesis) CREATE_KINESIS_STREAM=true ;;
*) echo "Unknown parameter passed: $1"
exit 1;;
esac
shift
done
if [[ -z $LAMBDA_FIREHOSE_ROLE ]];
then
echo "--role argument for lambda firehose arn role needs to be passed"
exit 1
fi;
printf '\n Running Twitter stream to Kinesis and S3 \n'
update_twitter_stream_lambda_container_image(){
if [[ ! -z $IMAGE_URI ]];
then
echo "Updating container image for ${1}"
aws lambda update-function-code --function-name "${1}" --image-uri "${2}"
else
echo "--image_uri not passed so skipping container image update"
fi;
}
create_update_firehose_lambda_zip() {
(
echo
echo "Zipping lambda package for ${FIREHOSE_LAMBDA}"
echo
cd ${LAMBDA_SOURCE_CODE}/${FIREHOSE_LAMBDA} || exit
# shellcheck disable=SC2035
zip ../${FIREHOSE_LAMBDA}.zip *
if [[ -$? -eq 0 ]];then
if aws lambda list-functions --query 'Functions[*].[FunctionName]'| grep "${1}" > /dev/null; then
printf "\n %s function already exists so updating with zip source code \n" "${1}"
aws lambda update-function-code --function-name "${1}" --zip-file fileb://../"${1}.zip"
else
printf '\n Creating new function %s with zip \n' "${1}"
aws lambda create-function --function-name "${1}" --runtime python3.9 --zip-file fileb://../"${1}.zip" \
--role "$LAMBDA_FIREHOSE_ROLE" --timeout 40 --memory-size 1024 --handler lambda_function.lambda_handler
fi;
fi;
)
}
update_twitter_stream_lambda_container_image "${FIREHOSE_LAMBDA}" "${IMAGE_URI}";
if [[ "${CREATE_KINESIS_STREAM}" = true ]];
then
echo "--create_kinesis set to true so creating kinesis stream and firehose"
python kinesis/create_kinesis_streams.py
sleep 10
fi;
if [[ -$? -eq 0 ]];
then
create_update_firehose_lambda_zip "${FIREHOSE_LAMBDA}" ${LAMBDA_SOURCE_CODE}/${FIREHOSE_LAMBDA}.zip
fi;