forked from evalmee/s3_bucket_notification_lambda
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdate_s3_notification.sh
executable file
·135 lines (119 loc) · 4.96 KB
/
update_s3_notification.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
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
#!/bin/bash
POSITIONAL_ARGS=()
while [[ $# -gt 0 ]]; do
case $1 in
-e | --events)
EVENTS="$2"
shift # past argument
shift # past value
;;
-l | --lambda-arn)
LAMBDA_ARN="$2"
shift # past argument
shift # past value
;;
-b | --bucket)
BUCKET="$2"
shift # past argument
shift # past value
;;
-p | --prefix)
PREFIX="$2"
shift # past argument
shift # past value
;;
-s | --suffix)
SUFFIX="$2"
shift # past argument
shift # past value
;;
-i | --id)
ID="$2"
shift # past argument
shift # past value
;;
-* | --*)
echo "Unknown option $1"
exit 1
;;
*)
POSITIONAL_ARGS+=("$1") # save positional arg
shift # past argument
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters
echo "EVENTS = [${EVENTS}]"
echo "LAMBDA_ARN = ${LAMBDA_ARN}"
echo "BUCKET = ${BUCKET}"
echo "PREFIX = ${PREFIX}"
echo "SUFFIX = ${SUFFIX}"
echo "ID = ${ID}"
if [[ -n $1 && $1 == "get" ]]; then
if [ -z "${BUCKET}" ]; then
echo "missing a required argument (BUCKET)"
exit 1
fi
# get all notification from that bucket
aws s3api get-bucket-notification-configuration --bucket "${BUCKET}"
elif [[ -n $1 && $1 == "delete" ]]; then
if [ -z "${BUCKET}" ] || [ -z "${ID}" ]; then
echo "missing a required argument (BUCKET, ID)"
exit 1
fi
# check id exists
current=$(aws s3api get-bucket-notification-configuration --bucket "${BUCKET}" |
jq -c "select(.LambdaFunctionConfigurations[] | select(.Id == \"${ID}\"))")
if [ -z "${current}" ]; then
echo "id does not exist, and therefore cannot be deleted"
exit 1
fi
# delete notifications with the same ID
aws s3api get-bucket-notification-configuration --bucket "${BUCKET}" |
jq "del(.LambdaFunctionConfigurations[] | select(.Id == \"${ID}\"))" |
xargs -0I{} aws s3api put-bucket-notification-configuration --bucket "${BUCKET}" --notification-configuration {}
elif [[ -n $1 && $1 == "deleteAll" ]]; then
aws s3api put-bucket-notification-configuration --bucket="${BUCKET}" --notification-configuration='{"LambdaFunctionConfigurations": []}'
elif [[ -n $1 && $1 == "update" ]]; then
if [ -z "${BUCKET}" ] || [ -z "${ID}" ] || [ -z "${EVENTS}" ] || [ -z "${LAMBDA_ARN}" ]; then
echo "missing a required argument (BUCKET, ID, EVENTS, LAMBDA_ARN)"
exit 1
fi
# make json string
if [ -z "${PREFIX}" ] && [ -z "${SUFFIX}" ]; then
json_string=".LambdaFunctionConfigurations += [{\"Id\": \"${ID}\",\"LambdaFunctionArn\": \"${LAMBDA_ARN}\",\"Events\": [\"${EVENTS}\"]}]"
elif [ -z "${PREFIX}" ]; then
json_string=".LambdaFunctionConfigurations += [{\"Id\": \"${ID}\",\"LambdaFunctionArn\": \"${LAMBDA_ARN}\",\"Events\": [\"${EVENTS}\"],\"Filter\": {\"Key\": {\"FilterRules\": [{\"Name\": \"Suffix\", \"Value\": \"${SUFFIX}\"}]}}}]"
elif [ -z "${SUFFIX}" ]; then
json_string=".LambdaFunctionConfigurations += [{\"Id\": \"${ID}\",\"LambdaFunctionArn\": \"${LAMBDA_ARN}\",\"Events\": [\"${EVENTS}\"],\"Filter\": {\"Key\": {\"FilterRules\": [{\"Name\": \"Prefix\", \"Value\": \"${PREFIX}\"}]}}}]"
else
json_string=".LambdaFunctionConfigurations += [{\"Id\": \"${ID}\",\"LambdaFunctionArn\": \"${LAMBDA_ARN}\",\"Events\": [\"${EVENTS}\"],\"Filter\": {\"Key\": {\"FilterRules\": [{\"Name\": \"Prefix\", \"Value\": \"${PREFIX}\"},{\"Name\": \"Suffix\", \"Value\": \"${SUFFIX}\"}]}}}]"
fi
# check id exists
current0=$(aws s3api get-bucket-notification-configuration --bucket "${BUCKET}" | jq -c)
current=$(echo ${current0} | jq -c "select(.LambdaFunctionConfigurations[] | select(.Id == \"${ID}\"))")
if [ -z "${current}" ]; then
# create
echo "id does not exist, and therefore it will be created"
if [ -z "${current0}" ]; then
# echo new
echo "{${json_string}}" | sed 's/.LambdaFunctionConfigurations +=/"LambdaFunctionConfigurations":/' | jq -c |
xargs -0I{} aws s3api put-bucket-notification-configuration --bucket "${BUCKET}" --notification-configuration {}
else
# echo existing
echo "${current0}" | jq -c "${json_string}" |
xargs -0I{} aws s3api put-bucket-notification-configuration --bucket "${BUCKET}" --notification-configuration {}
fi
else
# update
# echo updating
aws s3api get-bucket-notification-configuration --bucket "${BUCKET}" |
jq "del(.LambdaFunctionConfigurations[] | select(.Id == \"${ID}\"))" |
jq -c "${json_string}" |
xargs -0I{} aws s3api put-bucket-notification-configuration --bucket "${BUCKET}" --notification-configuration {}
fi
else
echo "argument needed (get, update, delete)"
exit 1
fi
exit 0