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

Комментарии • 70

  • @user-yz9jy5yn6u
    @user-yz9jy5yn6u Год назад

    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

  • @nrithyam925
    @nrithyam925 2 года назад

    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

  • @akhila.g.krishnan3970
    @akhila.g.krishnan3970 3 года назад

    This works like a charm !! Thanks a ton!

    • @SrceCde
      @SrceCde  3 года назад

      Glad it helped! Please like, share & subscribe. :)

  • @ashwiniramu5521
    @ashwiniramu5521 2 года назад

    Hi, Nice tutorial.. Is it possible to password protect the attachment & then email ?

  • @natesheridan6584
    @natesheridan6584 4 года назад

    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.

    • @SrceCde
      @SrceCde  4 года назад +1

      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. :)

  • @amitinva
    @amitinva 3 года назад

    Awesome tutorial. Can it be used to send attachment of any file type? Excel, ppt, pdf, zip....

    • @SrceCde
      @SrceCde  3 года назад

      Thanks for stopping by Amit Gupta. I have not tested with those files but think it should work.

  • @RizwanKler
    @RizwanKler 2 года назад

    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?

  • @cricinfoprime
    @cricinfoprime Год назад +1

    this code is not working

  • @khushbookhurana327
    @khushbookhurana327 4 года назад

    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.

    • @khushbookhurana327
      @khushbookhurana327 4 года назад

      Hello, please let me know if you could share light on the above question.

    • @SrceCde
      @SrceCde  4 года назад

      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. :)

  • @adityapratapsingh4776
    @adityapratapsingh4776 Год назад

    what if there is a zip file which is there and i want to access it send to different mail

  • @abhagera4302
    @abhagera4302 2 года назад

    Thanks for the video. I did the same but not receiving any email. and Email delivery failed! in lambda execution result

  • @vinayakmahadev488
    @vinayakmahadev488 3 года назад

    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.

  • @souravpatel4802
    @souravpatel4802 2 года назад

    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.

  • @venkatkomminenii
    @venkatkomminenii 2 года назад

    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?

  • @udaykumar-ld4ky
    @udaykumar-ld4ky 2 года назад

    Thanks, for the tutorial. How can we able to rename the attachment with the current "date format" before sending it.

    • @SrceCde
      @SrceCde  2 года назад

      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 :)

  • @saravanakumar-iw2si
    @saravanakumar-iw2si 4 года назад

    this code can be used for the .wav file format from S3 bucket .

    • @SrceCde
      @SrceCde  4 года назад +1

      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. :)

    • @saravanakumar-iw2si
      @saravanakumar-iw2si 4 года назад +1

      Srce Cde yes worked fine

  • @niteshaarne2208
    @niteshaarne2208 4 года назад

    Awesome !!!

    • @SrceCde
      @SrceCde  4 года назад

      Thanks for stopping by Nitish. I'm glad that you found the tutorial useful. Please like share and subscribe. :)

  • @khushbookhurana327
    @khushbookhurana327 4 года назад

    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.

    • @SrceCde
      @SrceCde  4 года назад

      Thanks for stopping by khushboo khurana. Can you please share the error/ trace back?

    • @khushbookhurana327
      @khushbookhurana327 4 года назад

      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.

    • @SrceCde
      @SrceCde  4 года назад

      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. :)

    • @SrceCde
      @SrceCde  4 года назад

      Finally replace line 41 with "msg["To"] = ','.join(to)" and it should work.

    • @khushbookhurana327
      @khushbookhurana327 4 года назад

      @@SrceCde Yes, it worked!! Thank you so much.
      Another part, how about modification to add "Cc" address as well? Is that possible?

  • @vickybagul6072
    @vickybagul6072 2 года назад

    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

    • @SrceCde
      @SrceCde  2 года назад

      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 :)

  • @DevOpsDIQ
    @DevOpsDIQ 7 месяцев назад

    How to trigger email notification on s3 events via lambda function using Terraform

  • @teenakochu6450
    @teenakochu6450 3 года назад

    @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 ?

    • @SrceCde
      @SrceCde  3 года назад

      Thanks for stopping by Teena Kochu. Can you please make sure that the emails are verified?

    • @teenakochu6450
      @teenakochu6450 3 года назад

      @@SrceCde my mistake...I wasn't add event notification in s3 bucket..but now it got success..Thank you

    • @muhammadarsalan4183
      @muhammadarsalan4183 3 года назад

      @@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

  • @sk6724
    @sk6724 4 года назад

    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

    • @SrceCde
      @SrceCde  4 года назад

      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. :)

  • @bharathchandra2339
    @bharathchandra2339 4 года назад

    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

    • @SrceCde
      @SrceCde  4 года назад

      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. :)

    • @bharathchandra2339
      @bharathchandra2339 4 года назад +1

      @@SrceCde Thanks for the quick reply

  • @jayeshkhairnar1
    @jayeshkhairnar1 3 года назад

    how can we send email with attachment to multiple email Id's

    • @SrceCde
      @SrceCde  3 года назад

      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. :)

  • @mohitvadehra142
    @mohitvadehra142 4 года назад

    why the 10 mb restriction..and what can I do for larger files?
    thanks in advance

    • @SrceCde
      @SrceCde  4 года назад

      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.

    • @mohitvadehra142
      @mohitvadehra142 4 года назад +1

      @@SrceCde thanks bro

  • @pratibharana7915
    @pratibharana7915 4 года назад

    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

    • @SrceCde
      @SrceCde  4 года назад +1

      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. :)

    • @pratibharana7915
      @pratibharana7915 4 года назад

      @@SrceCde Solved!! Thank you so much....You are doing a great job!

    • @muhammadarsalan4183
      @muhammadarsalan4183 3 года назад

      @@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

  • @mohitvadehra142
    @mohitvadehra142 4 года назад

    and where is the screenshot?

    • @SrceCde
      @SrceCde  4 года назад

      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.

  • @sarahkhan717
    @sarahkhan717 3 года назад

    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\"]:
    "
    ]
    }

    • @SrceCde
      @SrceCde  3 года назад +1

      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.

    • @sarahkhan717
      @sarahkhan717 3 года назад +1

      @@SrceCde yes correct. I am able to trigger email notification. Thanks a lottt:))

    • @SrceCde
      @SrceCde  3 года назад

      Please like, share & subscribe. :)

    • @muhammadarsalan4183
      @muhammadarsalan4183 3 года назад

      @@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????

  • @annwebdevelopment1064
    @annwebdevelopment1064 4 года назад

    Just saying: Written tutorials are more efficient for the learner.

    • @SrceCde
      @SrceCde  4 года назад

      Thanks for the feedback. I will try to write the text version. :)

    • @annwebdevelopment1064
      @annwebdevelopment1064 4 года назад

      @@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.

    • @SrceCde
      @SrceCde  4 года назад

      You can refer to the video description for code. I have mentioned the GitHub code link. I hope this helps. Please like, share & subscribe. :)

    • @annwebdevelopment1064
      @annwebdevelopment1064 4 года назад

      @@SrceCde Yes, I missed clicking on the "Show More" link in the description. I see your GitHub link now. Thank you!