AWS SES with Lambda
Tags: aws, serverless, awscommunitybuilders, ses
Published: 2023-03-15 10:55:27

Amazon SES (Simple Email Service) is a simple service that can be used by small and large industries to send marketing, transaction and notification emails as the cost is less and it is reliable. SES can be easily integrated to an existing application with the help of SMTP or AWS SDK.

## Why SES ?

• Building an in house email solutions is a complex and expensive challenge for a business due to the infrastructure demands, network assembly and security involved. Also some proper third-
party email solutions require huge up-front prices. SES eliminates these complexities.
• It have the benefit of a refined email infrastructure that amazon has used for its large consumer base.
• With SES there are not any direct prices and no minimum commitments. If we use SES in an Amazon EC2 instance, the first 62,000 emails sent are free and a very low price for emails sent
thereafter.
• Also SES can be integrated with other AWS services to provide seamless email solutions to the end users. You pay as you go, and you pay just for what you utilize.

## SES Configuration

**SES can be configured to be used in two ways.**
• Use SES as outbound email server
• Using existing server and configure it to send the mails through Amazon SES.

## SES with AWS Lambda

To send Emails from a Lambda function using SES we need to configure the following.

**• Required IAM Lambda permissions for the API call.**
  1. **ses:SendEmail** – _Email is immediately queued for sending after composing_
  2. **ses:SendRawEmail** – _This is suitable in cases where we need to add attachments or HTML content._

These policies can be attached to an IAM role that would be assigned to our Lambda function.

**• Verified SES identity.**
1. _To use SES it is required to verify sender email addresses to confirm that the sender owns them and to prevent unauthorized use._
2. _We must verify each identity that we use. From, Source, Sender and Return address_
3. _Email addresses are case sensitive_
4. _If both email address and the domain the mail ID belongs is verified, the email address settings override the domain settings._
5. _Verification status is different for each region. If we want to send mails from the same
identity from different regions it has to be verified in all regions._

## Following is a simple Lambda example function using SES.

```
import boto3
client = boto3.client(
    'ses',
    region_name=region,
    aws_access_key_id='aws_access_key_string',
    aws_secret_access_key='aws_secret_key_string'
)
response = client.send_email(
    Destination={
        'ToAddresses': ['recipient1@domain.com', 'recipient2@domain.com'],
    },
    Message={
        'Body': {
            'Text': {
                'Charset': 'UTF-8',
                'Data': 'email body string',
            },
        },
        'Subject': {
            'Charset': 'UTF-8',
            'Data': 'email subject string',
        },
    },
    Source='sender.email@domain.com',
)
```

## Benefits of SES

**High Deliverability**:
Features like content filtering and reputation dashboard ensure high number of emails are delivered. Higher the sender’s reputation more the number of emails sent from that address.

**Cost-Effective**:
No upfront or fixed costs and the cost depends on our monthly usage of this service.

**Configurable**:
SES can be configured with other AWS services like EC2, ECS and Cloudwatch to get detailed analysis of the mail metrics and also get notifications using SNS.

## Use Cases

**Transactional**:
_SES can be used to send automated mails for order confirmation, delivery, and subscription related information._

**Notifications**:
_Notifications on application health and metrics and send user notifications when an event is triggered._

**Incoming Mails**:
_SES can be used to receive mails and programmatically route the same to a S3 bucket._

**Marketing**:
_Promotions, offers and new product details can be sent in high quality emails using SES._