forked from aws-samples/aws-lambda-fanout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanout
executable file
·400 lines (373 loc) · 15.7 KB
/
fanout
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#!/bin/bash
# AWS Lambda Fan-Out Utility
#
# Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# A copy of the License is located at
#
# http://aws.amazon.com/apache2.0
#
# or in the "license" file accompanying this file. This file is distributed
# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# This script is the main script for the command line utility
#
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
source "$DIR/cli/awscli.sh"
source "$DIR/cli/functions.sh"
source "$DIR/cli/targets.sh"
source "$DIR/cli/help.sh"
## Locates a destination AWS Lambda Function
function checkLambdaTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | sed -E -n 's#^arn:aws:lambda:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:function:([a-zA-Z0-9_-]{1,64})$#\1#p' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid ARN '${DESTINATION_NAME}', must be a fully qualified AWS Lambda Function ARN" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on functions within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws lambda "get-function" --function-name ${DESTINATION_NAME} --no-paginate --query 'Configuration.FunctionArn' --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null | sed -E -n 's#^arn:aws:lambda:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:function:([a-zA-Z0-9_-]{1,64})$#\1#p' )
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find function with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon Kinesis Stream
function checkKinesisTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | sed -E -n 's#^arn:aws:kinesis:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:stream/([a-zA-Z0-9_-]{1,128})$#\1#p' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid ARN '${DESTINATION_NAME}', must be a fully qualified Amazon Kinesis Stream ARN" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on streams within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws kinesis describe-stream --stream-name ${DESTINATION_NAME} --no-paginate --query 'StreamDescription.StreamARN' --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null | sed -E -n 's#^arn:aws:kinesis:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:stream/([a-zA-Z0-9_-]{1,128})$#\1#p')
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find stream with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon Kinesis Firehose Delivery Stream
function checkFirehoseTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | sed -E -n 's#^arn:aws:firehose:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:deliverystream/([a-zA-Z0-9_-]{1,64})$#\1#p' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid ARN '${DESTINATION_NAME}', must be a fully qualified Amazon Kinesis Firehose Delivery Stream ARN" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on streams within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws firehose describe-delivery-stream --delivery-stream-name ${DESTINATION_NAME} --query 'DeliveryStreamDescription.DeliveryStreamARN' --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null | sed -E -n 's#^arn:aws:firehose:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:deliverystream/([a-zA-Z0-9_-]{1,64})$#\1#p' )
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find stream with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon SQS Queue
function checkSqsTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | grep -E -e '^https://((queue)|(sqs\.[a-z]+-[a-z]+-[0-9]))\.amazonaws\.com/[0-9]{12}/[a-zA-Z0-9_-]{1,80}$' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid url '${DESTINATION_NAME}', must be a fully qualified Amazon SQS Queue Url" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on queues within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws sqs get-queue-url --queue-name ${DESTINATION_NAME} --query 'QueueUrl' --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null)
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find queue with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon IoT MQTT Topic
function checkIotTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | grep -E '^[a-zA-Z0-9-]+\.iot\.[a-z]+-[a-z]+-[0-9]\.amazonaws\.com#.*$' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid configuration '${DESTINATION_NAME}', must be a fully qualified Amazon IoT Endpoint followed by an MQTT topic name (separated by #)" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on topics within your local user" 1>&2
exit -1
else
DESTINATION_ID="$(aws iot describe-endpoint --output text ${DESTINATION_SEARCH_ARGS[@]})#${DESTINATION_NAME}"
fi
fi
fi
}
## Locates a destination Amazon SNS Topic
function checkSnsTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | grep -E -e '^arn:aws:sns:[a-z]+-[a-z]+-[0-9]:[0-9]{12}:[a-zA-Z0-9_-][a-zA-Z0-9_-]{0,255}$' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid ARN '${DESTINATION_NAME}', must be a fully qualified Amazon SNS Topic ARN" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on topics within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws sns list-topics --query "Topics[?ends_with(TopicArn,\`:${DESTINATION_NAME}\`)].TopicArn" --no-paginate --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null)
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find topic with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon ElastiCache Memcached Cluster
function checkMemcachedTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | grep -E -e '^[a-zA-Z][a-zA-Z0-9-]{0,19}\.[a-z0-9]+\.cfg\.[a-z]+[0-9]\.cache\.amazonaws\.com:[0-9]+$' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid endpoint '${DESTINATION_NAME}', must be a valid Amazon ElastiCache cluster endpoint in the format <configuration-endpoint>:<port>" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on topics within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws elasticache describe-cache-clusters --cache-cluster-id ${DESTINATION_NAME} --query 'CacheClusters[0].ConfigurationEndpoint.[Address,to_string(Port)] | join(`:`, @)' --no-paginate --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null)
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find topic with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon ElastiCache Redis Replication Group
function checkRedisTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | grep -E -e '^[a-zA-Z][a-zA-Z0-9-]{0,19}\.[a-z0-9]+\.ng\.[0-9]+\.[a-z]+[0-9]\.cache\.amazonaws\.com:[0-9]+$' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid endpoint '${DESTINATION_NAME}', must be a valid Amazon ElastiCache replication group primary endpoint in the format <primary-endpoint>:<port>" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on clusters within your local user" 1>&2
exit -1
else
DESTINATION_ID=$(aws elasticache describe-replication-groups --replication-group-id ${DESTINATION_NAME} --query 'ReplicationGroups[0].NodeGroups[0].PrimaryEndpoint.[Address,to_string(Port)] | join(`:`, @)' --no-paginate --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null)
if [ -z "${DESTINATION_ID}" ]; then
echo "Can not find cluster with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
fi
fi
fi
}
## Locates a destination Amazon ElasticSearch Domain and Index
function checkEsTarget {
if [ ! -z "$DESTINATION_NAME" ]; then
DESTINATION_ID=$( echo "$DESTINATION_NAME" | grep -E -e '^search-[a-z][a-z0-9-]{2,27}-[a-z0-9]+\.[a-z]+-[a-z]+-[0-9]\.es\.amazonaws\.com#.*$' )
if [ -z "${DESTINATION_ID}" ]; then
if [ ! -z "${DESTINATION_ROLE}" ]; then
echo "Invalid endpoint '${DESTINATION_NAME}', must be a fully qualified Amazon Elasticsearch Service Domain endpoint followed by '#' then the index name" 1>&2
echo " - You specified a Role ARN, and name expansion can only be made on domains within your local user" 1>&2
exit -1
else
if [ -z "${DESTINATION_INDEX}" ]; then
echo "Invalid parameters, you must specify --index" 1>&2
exit -1
fi
DESTINATION_ENDPOINT=$(aws es describe-elasticsearch-domain --domain-name ${DESTINATION_NAME} --query 'DomainStatus.Endpoint' --output text ${DESTINATION_SEARCH_ARGS[@]} 2> /dev/null)
if [ -z "${DESTINATION_ENDPOINT}" ]; then
echo "Can not find domain with name '${DESTINATION_NAME}' in the local account" 1>&2
exit -1
fi
DESTINATION_ID="${DESTINATION_ENDPOINT}#${DESTINATION_INDEX}"
fi
fi
fi
}
ACTION=
if [ $# -ne 0 ]; then
ACTION=$1
shift
fi
if [ "$ACTION" == "register" ]; then
WORKER_TYPE=
if [ $# -ne 0 ]; then
WORKER_TYPE=$1
shift
fi
readCliParams $@
readTargetParams ${PASSTHROUGH[@]}
if [ "$WORKER_TYPE" == "lambda" ]; then
checkLambdaTarget
elif [ "$WORKER_TYPE" == "kinesis" ]; then
checkKinesisTarget
elif [ "$WORKER_TYPE" == "firehose" ]; then
checkFirehoseTarget
elif [ "$WORKER_TYPE" == "sns" ]; then
checkSnsTarget
elif [ "$WORKER_TYPE" == "sqs" ]; then
checkSqsTarget
elif [ "$WORKER_TYPE" == "iot" ]; then
checkIotTarget
elif [ "$WORKER_TYPE" == "memcached" ]; then
checkMemcachedTarget
elif [ "$WORKER_TYPE" == "redis" ]; then
checkRedisTarget
elif [ "$WORKER_TYPE" == "es" ]; then
checkEsTarget
else
echo "Invalid target type '$WORKER_TYPE', you must specify a target type, one of" 1>&2
echo " - sns: for specifying an Amazon Simple Notification Service (SNS) Topic" 1>&2
echo " - sqs: for specifying an Amazon Simple Queue Service (SQS) Queue" 1>&2
echo " - es: for specifying an Amazon Elasticsearch Domain" 1>&2
echo " - kinesis: for specifying an Amazon Kinesis Stream" 1>&2
echo " - firehose: for specifying an Amazon Kinesis Firehose Delivery Stream" 1>&2
echo " - iot: for specifying an AWS IoT MQTT topic" 1>&2
echo " - lambda: for specifying an AWS Lambda Function" 1>&2
echo " - memcached: for specifying an Amazon ElastiCache Memcached Cluster" 1>&2
echo " - redis: for specifying an Amazon ElastiCache Redis Replication Group" 1>&2
doHelp
exit -1
fi
readObjectProperties ${PASSTHROUGH[@]}
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "register: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
registerFanoutTarget ${PASSTHROUGH[@]}
elif [ "$ACTION" == "update" ]; then
readCliParams $@
readObjectProperties ${PASSTHROUGH[@]}
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "update: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
updateFanoutTarget
elif [ "$ACTION" == "activate" ]; then
ACTIVE=true
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "activate: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
updateFanoutTarget
elif [ "$ACTION" == "deactivate" ]; then
ACTIVE=false
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "deactivate: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
updateFanoutTarget
elif [ "$ACTION" == "unregister" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "unregister: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
unregisterFanoutTarget
elif [ "$ACTION" == "list" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "list: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
listFanoutTargets $@
elif [ "$ACTION" == "deploy" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readFunctionConfigParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "deploy: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
deployFanout
elif [ "$ACTION" == "destroy" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "destroy: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
destroyFanout
elif [ "$ACTION" == "hook" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
readSourceConfigParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "list: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
hookFanoutSource $@
elif [ "$ACTION" == "unhook" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "list: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
unhookFanoutSource $@
elif [ "$ACTION" == "pause" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "list: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
setHookFanoutSourceState inactive $@
elif [ "$ACTION" == "resume" ]; then
readCliParams $@
readFunctionParams ${PASSTHROUGH[@]}
readWorkerParams ${PASSTHROUGH[@]}
if [ ${#PASSTHROUGH[@]} -ne 0 ]; then
echo "list: unexpected parameter ${PASSTHROUGH[@]}" 1>&2
doHelp
exit -1
fi
setHookFanoutSourceState active $@
else
echo "Invalid action, you must specify an action" 1>&2
doHelp
exit -1
fi