AWS: How to trigger email notification with attachment on S3 events via Lambda function
HTML-код
- Опубликовано: 12 сен 2024
- Welcome to the AWS Lambda tutorial. In this tutorial, I'm gonna show you how to trigger email notification with attachment on S3 events via Lambda function.
---
Support my work:
---
Patreon: / srcecde
PayPal: paypal.me/srcecde
Paytm | Gpay: 9023197426
Note:
1. I have shared the email screenshot that I have received via SES at the end of the video.
2. The size of the file should be less than 10 MB
Events: PUT
Previous tutorial: • AWS: How to trigger em...
Read file from S3: • AWS: Reading File cont...
Playlist: • EC2 instance state and...
GitHub: github.com/src...
---
Connect with me
---
Twitter: / srcecde
GitHub: github.com/src...
Facebook: / srcecde
Instagram: / srcecde
LinkedIn: / srcecde
Reddit: / srcecde
Thanks great tutorial. My scenario is, If there is events done in aws S3 bucket for a whole day, we need a single SNS notification alert at the end of the day or at anytime using python code in lambda
Hi great tutorial works perfectly for single file. can you please tell what modification to be done for multiple files attachment in single email from a s3 bucket
This works like a charm !! Thanks a ton!
Glad it helped! Please like, share & subscribe. :)
Hi, Nice tutorial.. Is it possible to password protect the attachment & then email ?
Your tutorials are great! Easy to follow and cover the complex stuff I need help with. However what code would I need to add in order to display a URL link instead of the attachment in the email notification? Sorry, I'm not familiar with this coding language.
Thanks for stopping by Nate Sheridan. For sending link you need to add within body in the code. Replace URL with the actual URL. If you don't want to send attachment then you can use this code: github.com/srcecde/aws-tutorial-code/blob/master/lambda/lambda_s3_event_ses_email.py
I hope this helps. Please like, share & subscribe. :)
Awesome tutorial. Can it be used to send attachment of any file type? Excel, ppt, pdf, zip....
Thanks for stopping by Amit Gupta. I have not tested with those files but think it should work.
Hi great video dude. I want to schedule a a cloudwatch task which will send the latest file uploaded to an S3 bucket as an attachment instead of when the file is uploaded. How could your code be changed to allow for this?
this code is not working
The tutorial has been awesome and of great use !!
I have used it previously, now want to use it in a scenario where the file reaching the s3 bucket has changing name every time. I wish to modify this code in a way that, it should pick up the name of the file from the bucket as it is and add it as attachment. Please help me in achieving this.
Hello, please let me know if you could share light on the above question.
Thanks for stopping by khushboo khurana. The current implementation considers the same filename which is uploaded in a bucket. If the file is uploaded in sub-folder in S3 then you need to modify the code. If that is the case then replace the "object" on line 46 with "object.split('/')[-1]" and it should work. Let me know if you have any further questions. Please like, share & subscribe. :)
what if there is a zip file which is there and i want to access it send to different mail
Thanks for the video. I did the same but not receiving any email. and Email delivery failed! in lambda execution result
Hi,
Can you upload a video below scenario...
There will be an application that will upload a file on the Cloud. This file will be processed by a server-less computing platform and will send an email with all the details to the user. When the user receives the email, he/she is notified about it. In case the user wishes to delete the email, he/she has to reply to the email saying Delete. On receiving this response, the file will be deleted and moved to another folder on the Cloud.
Hello @Srce Cde, Can you help me out with this code? I am getting errors on the final code that you have uploaded into your github repo.
Hi srce Cde
In my scenario, the file reaches the s3 bucket and will change the name every time and the file name contains special symbols such as [ : , space], etc every time it is replacing those symbols with %, +, etc How to avoid changing special characters?
Thanks, for the tutorial. How can we able to rename the attachment with the current "date format" before sending it.
Yes, you can! All you need to do is to modify line no 11 with the name you want. I hope this helps. Please like, share & subscribe :)
this code can be used for the .wav file format from S3 bucket .
Thanks for stopping by saravana kumar. Yes, it will also work with .wav file. But make sure that the overall message does not exceed 10 MB (after base64 encoding). I hope this helps. Please like, share & subscribe. :)
Srce Cde yes worked fine
Awesome !!!
Thanks for stopping by Nitish. I'm glad that you found the tutorial useful. Please like share and subscribe. :)
Thanks for sharing the tutorial. Its awesome! I am using code for one of my function which generate report and will use this code to send email. I am new programming and I am getting error when I am trying to send email to multiple recipients. Please tell me what changes I need to do in this code so that I can send it to multiple recipients.
Thanks for stopping by khushboo khurana. Can you please share the error/ trace back?
Please tell me how to modify your code to add multiple email address, I am using the same code with just basic changes of name of file etc. changed according to requirement. My main goal is add multiple email address, please help on that.
With reference to the GitHub link in the video description. Replace line 29 with "to = ['a','b', 'c']". Replace line 51 with "response = ses.send_raw_email(Source = sender, Destinations = to, RawMessage = {"Data": msg.as_string()})". I hope this helps. Please like, share & subscribe. :)
Finally replace line 41 with "msg["To"] = ','.join(to)" and it should work.
@@SrceCde Yes, it worked!! Thank you so much.
Another part, how about modification to add "Cc" address as well? Is that possible?
Hi brother ,
your videos are great. Really great work.
I have one problem if you can help me out with this please see,
So i want to attach files from s3 bucket and send it to the respected Id,Like once the files are placed in s3 bucket/folder xyz.csv file then it supposed to pick that up and send it over the mail.
if you have something like that please let me know.
Thank you
Thanks for stopping by Vicky! You can set the trigger using prefix if you want to send an email if there is any upload in that sub-folder. Based on that trigger, you can read/download the file content via boto3. Or you can also generate then pre-signed URL and send it as a part of the email. I hope this helps.
Please like, share & subscribe :)
How to trigger email notification on s3 events via lambda function using Terraform
@Srce Cde I tried to run the same ,but the code is working fine,but unable to see any mails (verified).Do you have any suggestion ?
Thanks for stopping by Teena Kochu. Can you please make sure that the emails are verified?
@@SrceCde my mistake...I wasn't add event notification in s3 bucket..but now it got success..Thank you
@@teenakochu6450 i have everything same but its not working.
i am getting mails of delete or put operation as taught in previos video but this code when i try to copy paste and put my email. it does not work
I'm having issues with the code, I'm very new to programming & I am getting error "START RequestId: 430ad004-9bb2-4ee8-ad54-
[ERROR] KeyError: 'Records'
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 14, in lambda_handler
for i in event["Records"]:
I see in your video you did get the same error. Anything I need to add/correct here. Thanks much. Cheers, Sam
Thanks for stopping by Sam. I believe the reason you are are getting this error is you are trying to manually "Test"/"Run" the lambda function. To invoke and run the function successfully, you have to drop a file in the S3 bucket and it should trigger the lambda function successfully (Since, we have set the trigger). I hope this helps. Let me know in case of any further queries. Please like, share & subscribe. :)
Hello Srce Cde,
That's an awesome video, I was able to follow well till the end, but I encounter this error. Could you please help.
An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.: NoSuchKey
Traceback (most recent call last):
File "/var/task/lambda_function.py", line 17, in lambda_handler
fileObj = s3.get_object(Bucket = bucket_name, Key = objects)
File "/var/runtime/botocore/client.py", line 316, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/var/runtime/botocore/client.py", line 626, in _make_api_call
raise error_class(parsed_response, operation_name)
botocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the GetObject operation: The specified key does not exist.
Thanks
Thanks for stopping by Bharath Chandra. Replace this line "fileObj = s3.get_object(Bucket = bucket_name, Key = objects)" with "fileObj = s3.get_object(Bucket = bucket_name, Key = object)". You have placed extra 's' in objects. You can refer the GitHub link in the video description. I hope this helps. Please like, share & subscribe. :)
@@SrceCde Thanks for the quick reply
how can we send email with attachment to multiple email Id's
Thanks for stopping by Jayesh Khairnar. You can add the email addresses as a part of ToAddresses as a list of string. I hope this helps. Please like, share & subscribe. :)
why the 10 mb restriction..and what can I do for larger files?
thanks in advance
Thanks for stopping by Mohit. The 10MB (after base64 encoding) limit/restriction is the hard limit set by AWS withing the SES service for attachment per email. Unfortunately, it cannot be increased. The other way around to your problem is to upload the file to S3 Bucket (from same lambda function) and include the downloadable link of that file in an email. I hope this helps.
@@SrceCde thanks bro
I was trying the same but I got an error An error occurred (AccessDenied) when calling the GetObject operation: Access Denied: ClientError. Please suggest me something
Thanks for stopping by PRATIBHA RANA. Your lambda function does not have permission to read from S3. For that you need to add permission within IAM role that you have attached with the lambda function. You can go ahead and add AWSLambdaExecute policy within IAM role. I hope this helps. Please like, share & subscribe. :)
@@SrceCde Solved!! Thank you so much....You are doing a great job!
@@SrceCde bhai, my email is verified and also permission of lambdaexecutive is allowed. still i do not recieve any email niether any logs. please help
and where is the screenshot?
Thanks for stopping by Mohit. Actually, the screenshot is there at the end of the video. But the duration is less than 1 second. So it just pop-up and the video ends. Sorry for the inconvenience caused.
Hi...
I really liked the tutorial. You made it very easy to understand. So i have created this function but i am getting the below error. Could you plz help:
{
"errorMessage": "'Records'",
"errorType": "KeyError",
"stackTrace": [
" File \"/var/task/lambda_function.py\", line 11, in lambda_handler
for i in event[\"Records\"]:
"
]
}
Thanks for stopping by Sarah Khan. I believe you are trying to invoke/execute the lambda function by clicking on the Test button, and as a result it will not receive "Records" in an event. Instead, you need to drop a file in S3 bucket and that should trigger the lambda function and this issue will be resolved.
@@SrceCde yes correct. I am able to trigger email notification. Thanks a lottt:))
Please like, share & subscribe. :)
@@SrceCde bhai can you make it more simple?
I believe you are trying to invoke/execute the lambda function by clicking on the Test button, and as a result it will not receive "Records" in an event. Instead, you need to drop a file in S3 bucket and that should trigger the lambda function and this issue will be resolved.
yes correct. I am able to trigger email notification????
Just saying: Written tutorials are more efficient for the learner.
Thanks for the feedback. I will try to write the text version. :)
@@SrceCde Great! And it would help if the final code is presented as text (not screenshots) so that we can copy it into a code editor.
You can refer to the video description for code. I have mentioned the GitHub code link. I hope this helps. Please like, share & subscribe. :)
@@SrceCde Yes, I missed clicking on the "Show More" link in the description. I see your GitHub link now. Thank you!