|
| 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" |
0 commit comments