Skip to content

Commit e58a9cc

Browse files
authored
chore: added aws s3 event triggering script (bregman-arie#10255)
1 parent df28b9b commit e58a9cc

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[](./sample.png)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#!/bin/bash
2+
3+
# always put up the detail of scripts . version, author, what it does, what event triggers and all ..
4+
5+
###
6+
# Author: Adarsh Rawat
7+
# Version: 1.0.0
8+
# Objective: Automate Notification for a object uploaded or created in s3 bucket.
9+
###
10+
11+
# debug what is happening
12+
set -x
13+
14+
# all these cmds are aws cli commands | abhishek veermalla day 4-5 devops
15+
16+
# store aws account id in a variable
17+
aws_account_id=$(aws sts get-caller-identity --query 'Account' --output text)
18+
19+
# print the account id from the variable
20+
echo "aws account id: $aws_account_id"
21+
22+
# set aws region, bucket name and other variables
23+
aws_region="us-east-1"
24+
aws_bucket="s3-lambda-event-trigger-bucket"
25+
aws_lambda="s3-lambda-function-1"
26+
aws_role="s3-lambda-sns"
27+
email_address="adarshrawat8304@gmail.com"
28+
29+
# create iam role for the project
30+
role_response=$(aws iam create-role --role-name s3-lambda-sns --assume-role-policy-document '{
31+
"Version": "2012-10-17",
32+
"Statement": [{
33+
"Action": "sts:AssumeRole",
34+
"Effect": "Allow",
35+
"Principal": {
36+
"Service": [
37+
"lambda.amazonaws.com",
38+
"s3.amazonaws.com",
39+
"sns.amazonaws.com"
40+
]
41+
}
42+
}]
43+
}')
44+
45+
# jq is json parser here parse the role we created
46+
47+
# extract the role arn from json resposne and store in variable
48+
role_arn=$(echo "$role_response" | jq -r '.Role.Arn')
49+
50+
# print the role arn
51+
echo "Role ARN: $role_arn"
52+
53+
# attach permissions to the role
54+
aws iam attach-role-policy --role-name $aws_role --policy-arn arn:aws:iam::aws:policy/AWSLambda_FullAccess
55+
aws iam attach-role-policy --role-name $aws_role --policy-arn arn:aws:iam::aws:policy/AmazonSNSFullAccess
56+
57+
# create s3 bucket and get the output in a variable
58+
bucket_output=$(aws s3api create-bucket --bucket "$aws_bucket" --region "$aws_region")
59+
60+
# print the output from the variable
61+
echo "bucket output: $bucket_output"
62+
63+
# upload a file to the bucket
64+
aws s3 cp ./sample.png s3://"$aws_bucket"/sample.png
65+
66+
# create a zip file to upload lambda function
67+
zip -r s3-lambda.zip ./s3-lambda
68+
69+
sleep 5
70+
71+
# create a lambda function
72+
aws lambda create-function \
73+
--region $aws_region \
74+
--function $aws_lambda \
75+
--runtime "python3.8" \
76+
--handler "s3-lambda/s3-lambda.lambda_handler" \
77+
--memory-size 128 \
78+
--timeout 30 \
79+
--role "arn:aws:iam::$aws_account_id:role/$aws_role" \
80+
--zip-file "fileb://./s3-lambda.zip"
81+
82+
# add permissions to s3 bucket to invoke lambda
83+
LambdaFunctionArn="arn:aws:lambda:us-east-1:$aws_account_id:function:s3-lambda"
84+
aws s3api put-bucket-notification-configuration \
85+
--region "$aws_region" \
86+
--bucket "$aws_bucket" \
87+
--notification-configuration '{
88+
"LambdaFunctionConfigurations": [{
89+
"LambdaFunctionArn": "'"$LambdaFunctionArn"'",
90+
"Events": ["s3:ObjectCreated:*"]
91+
}]
92+
}'
93+
94+
aws s3api put-bucket-notification-configuration \
95+
--region "$aws_region" \
96+
--bucket "$aws_bucket" \
97+
--notification-configuration '{
98+
"LambdaFunctionConfigurations": [{
99+
"LambdaFunctionArn": "'"$LambdaFunctionArn"'",
100+
"Events": ["s3:ObjectCreated:*"]
101+
}]
102+
}'
103+
104+
# create an sns topic and save the topic arn to a variable
105+
topic_arn=$(aws sns create-topic --name s3-lambda-sns --output json | jq -r '.TopicArn')
106+
107+
# print the topic arn
108+
echo "SNS Topic ARN: $topic_arn"
109+
110+
# Trigger SNS topic using lambda function
111+
112+
# Add sns topic using lambda function
113+
aws sns subscribe \
114+
--topic-arn "$topic_arn" \
115+
--protocol email \
116+
--notification-endpoint "$email_address"
117+
118+
# publish sns
119+
aws sns publish \
120+
--topic-arn "$topic_arn" \
121+
--subject "A new object created in s3 bucket" \
122+
--message "Hey, a new data object just got delievered into the s3 bucket $aws_bucket"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
boto3==1.17.95
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import boto3
2+
import json
3+
4+
def lambda_handler(event, context):
5+
6+
# i want to know that event thing
7+
print(event)
8+
9+
# extract relevant information from the s3 event trigger
10+
bucket_name=event['Records'][0]['s3']['bucket']['name']
11+
object_key=event['Records'][0]['s3']['object']['key']
12+
13+
# perform desired operations with the upload file
14+
print(f"File '{object_key}' was uploaded to bucket '{bucket_name}'")
15+
16+
# example: send a notification via sns
17+
sns_client=boto3.client('sns')
18+
topic_arn='arn:aws:sns:us-east-1:<account-id>:s3-lambda-sns'
19+
sns_client.publish(
20+
TopicArn=topic_arn,
21+
Subject='s3 object created !!',
22+
Message=f"File '{object_key}' was uploaded to bucket '{bucket_name}"
23+
)
24+
25+
# Example: Trigger another Lambda function
26+
# lambda_client = boto3.client('lambda')
27+
# target_function_name = 'my-another-lambda-function'
28+
# lambda_client.invoke(
29+
# FunctionName=target_function_name,
30+
# InvocationType='Event',
31+
# Payload=json.dumps({'bucket_name': bucket_name, 'object_key': object_key})
32+
# )
33+
# in case of queuing and other objective similar to the netflix flow of triggering
34+
35+
return {
36+
'statusCode': 200,
37+
'body': json.dumps("Lambda function executed successfully !!")
38+
}
78.8 KB
Loading

0 commit comments

Comments
 (0)