Send SES Email With S3 Bucket Object Presigned Url

In this blog, we will create a script in AWS Lambda function which will generate the presigned url of s3 object and then will send the email using AWS SES.

Step 1: Open you AWS account.

Step 2: Open IAM service console.

Step 3: Create IAM policy for an IAM Role.

  • Copy the below policy and paste it in your IAM policy.
 1{
 2    "Version": "2012-10-17",
 3    "Statement": [
 4        {
 5            "Sid": "VisualEditor0",
 6            "Effect": "Allow",
 7            "Action": "logs:*",
 8            "Resource": "arn:aws:logs:*:*:log-group:*:log-stream:*"
 9        },
10        {
11            "Sid": "VisualEditor1",
12            "Effect": "Allow",
13            "Action": [
14                "s3:*",
15				"ses:*",
16                "logs:*"
17            ],
18            "Resource": [
19                "arn:aws:s3:::*",
20                "arn:aws:logs:*:*:log-group:*"
21            ]
22        }
23    ]
24}

Note: To know how to create a custom IAM policy , Please follow this link

Step 4: Create IAM Role and Attach the above IAM policy.

Note: To know how to create IAM Role, Please follow the this link

Step 5: Open Lambda console.

Step 6: Create Lambda Function.

To create an Lambda Function, Go to Lambda service from AWS console and create a new Function.

Lambda Console

  • Add Lambda Function name. (You can write any name).
  • In Runtime info, Choose "Python 3.8".
  • In permission, Choose "Use an existing Role" in Execution Role
  • In Existing Role, choose the IAM role which you have create above for this Lambda Function.
  • Click on "Create Function".
  • Go to "Function Code" and Paste the below Python code in it.
 1import json
 2import logging
 3import boto3
 4from botocore.exceptions import ClientError
 5
 6logger = logging.getLogger()
 7logger.setLevel(logging.INFO)
 8
 9s3 = boto3.client('s3')
10SESclient = boto3.client('ses',region_name='ap-south-1')
11
12def lambda_handler(event, context):
13    bucket_name = event['Records'][0]['s3']['bucket']['name']
14    object_key = event['Records'][0]['s3']['object']['key']
15    
16    try:
17     UrlResponse = s3.generate_presigned_url('get_object',
18                                                    Params={'Bucket': bucket_name,
19                                                            'Key': object_name},
20                                                    ExpiresIn=86400)
21                                                    
22     print (UrlResponse);
23     
24    except ClientError as e:
25        logging.error(e)
26        return None
27        
28    response = SESclient.send_email(
29        Destination={
30            'ToAddresses': ['test@gmail.com','test2@gmail.com'],
31        },
32        Message={
33            'Body': {
34                'Text': {
35                    'Charset': 'UTF-8',
36                    'Data': 'New File has been uplaoded in the '+ bucket_name +' bucket. You can download that file using this link: '+ UrlResponse ,
37                },
38            },
39            'Subject': {
40                'Charset': 'UTF-8',
41                'Data': 'Important: New File uploaded',
42            },
43        },
44        Source='sender@gmail.com',
45     )

Note: Don't forget to change the service region, recipient and sender email ID.

Step 5: Save the Lambda Function. To the Test the same, upload the file in your bucket.

Step 6: Please don't forget to share the post with your friend.

I :heart: AWS! :smile: Enjoy