4 # Set your preferred profile name.
5 #AWS_DEFAULT_PROFILE=default
6 # Set S3 bucket names in which quota should be enabled.
7 # Leaving empty will enable quota on all buckets.
8 #Buckets="your_bucket_name1 your_bucket_name2 ..."
10 Rolename
=lambda-s3quota
17 echo "\e[33m★ $*\e[m" >&2
23 if [ "$1" -gt 0 ] 2>/dev
/null ||
[ -z "$1" ]
28 [ "$1" -ge 0 ] 2>/dev
/null
&& shift
29 echo "\e[3$c;1m$m $lastlog\e[m \e[3${c}m$*\e[m" >&2
35 logstart Create Role
$Rolename
36 aws iam create-role
--role-name "$Rolename" --assume-role-policy-document file://assumerole-lambda.json
37 rolearn
=`aws iam get-role --role-name "$Rolename" --query Role.Arn --output text`
40 ## Create IAM Policies
42 logstart Create Policy IAMListUsers
43 aws iam create-policy
--policy-name IAMListUsers
--policy-document file://policy-listusers.json
44 logstart Create Policy S3PutObjectTagging
45 aws iam create-policy
--policy-name S3PutObjectTagging
--policy-document file://policy-s3puttag.json
48 ## Attach Policies to Role
53 aws iam list-policies
--query "Policies[?PolicyName==\`$policy\`].Arn" --output text
56 for policy
in IAMListUsers S3PutObjectTagging AWSLambdaBasicExecutionRole
58 logstart Attach Policy
$policy to Role
$Rolename
59 arn
=`get_policy_arn "$policy"`
60 aws iam attach-role-policy
--role-name "$Rolename" --policy-arn "$arn"
64 aws iam list-attached-role-policies
--role-name "$Rolename" --output text
67 ## Create Cloudwatch Log Group
68 logstart Create Cloudwatch Log Group
69 aws logs create-log-group
--log-group-name /aws
/lambda
/"$Funcname"
70 aws logs put-retention-policy
--log-group-name /aws
/lambda
/"$Funcname" --retention-in-days 7
73 ## Create Lambda function
75 logstart Create Lambda
$Funcname
76 tmpfile
=`mktemp --suffix=.zip -u`
77 trap 'rm "$tmpfile"' EXIT INT
78 zip "$tmpfile" lambda_function.py
80 aws lambda create-function
--function-name "$Funcname" \
81 --description "Listens to S3 write events and put 'Creator' tag on objects." \
84 --handler lambda_function.lambda_handler \
85 --zip-file "fileb://$tmpfile"
87 lambdaarn
=`aws lambda get-function --function-name "$Funcname" --query Configuration.FunctionArn --output text`
91 ## Grant S3 to invoke Lambda
93 logstart Permit S3 service to Lambda
94 aws lambda add-permission
--function-name "$Funcname" --statement-id quota-s3
--action lambda
:InvokeFunction
--principal s3.amazonaws.com
97 ## Create Lambda Trigger within S3
99 merge_notification_config
()
103 export eventId
="create-all-lambda"
104 aws s3api get-bucket-notification-configuration
--bucket "$bucket" |\
106 from __future__ import print_function
110 eventId = os.environ["eventId"]
111 cnfstr = "".join(sys.stdin.readlines())
112 sys.stderr.write("Current Notification Config: %s\n" % (cnfstr))
114 config = json.loads(cnfstr)
116 config = {"LambdaFunctionConfigurations":[]}
117 Lambda = dict(map(lambda x: (x["Id"], x), config["LambdaFunctionConfigurations"]))
118 newLambda = {eventId: {
120 "LambdaFunctionArn": os.environ["lambdaarn"],
121 "Events": ["s3:ObjectCreated:*"]
123 Lambda.update(newLambda)
124 config.update({"LambdaFunctionConfigurations": Lambda.values()})
125 print(json.dumps(config))
129 for bucket
in `[ -n "$Bucket" ] && echo "$Buckets" || aws s3api list-buckets --query 'Buckets[*].Name' --output text`
131 logstart Set Notification Config on bucket
$bucket
132 aws s3api put-bucket-notification-configuration
--bucket "$bucket" --notification-configuration "$(merge_notification_config "$bucket" "$lambdaarn")"