Skip to content

Send emails in AWS with SST

Send emails from your API in AWS with SST.

We are going to build a simple SST app in AWS with a serverless API, and send emails from it.

Before you get started:

  1. Configure your AWS credentials
  2. Install the SST CLI

1. Create a project

Let’s start by creating our app.

Terminal window
mkdir my-email-app && cd my-email-app
npm init -y

Init SST

Now let’s initialize SST in our app. Make sure you have the CLI installed.

Terminal window
sst init

This’ll create a sst.config.ts file in your project root.


2. Add email

Let’s add Email to our app, it uses Amazon SES behind the scenes. Update your sst.config.ts.

sst.config.ts
async run() {
const email = new sst.aws.Email("MyEmail", {
sender: "email@example.com",
});
}

SES can send emails from a verified email address or domain. To keep things simple we’ll be sending from an email. Make sure to use your email address here as we’ll be verifying it in the next step.


3. Add an API

Next let’s create a simple API that’ll send out an email when invoked. Add this to your sst.config.ts.

sst.config.ts
const api = new sst.aws.Function("MyApi", {
handler: "sender.handler",
link: [email],
url: true,
});
return {
api: api.url,
};

We are linking the our email component to our API.

Start dev mode

Start your app in dev mode. This runs your functions Live.

Terminal window
sst dev

This will give you your API URL.

Complete
api: https://wwwrwteda6kbpquppdz5i3lg4a0nvmbf.lambda-url.us-east-1.on.aws/

You should also get an email asking you to verify the sender email address.

Verify your email with SST

Click the link to verify your email address.


4. Send an email

We’ll use the SES client to send an email when the API is invoked. Create a new sender.ts file and add the following to it.

sender.ts
export const handler = async () => {
await client.send(
new SendEmailCommand({
FromEmailAddress: Resource.MyEmail.sender,
Destination: {
ToAddresses: [Resource.MyEmail.sender],
},
Content: {
Simple: {
Subject: {
Data: "Hello World!",
},
Body: {
Text: {
Data: "Sent from my SST app.",
},
},
},
},
})
);
return {
statusCode: 200,
body: "Sent!"
};
};

We are sending an email to the same verified email that we are sending from because your SES account might be in sandbox mode and can only send to verified emails. We’ll look at how to go to production below.

Add the imports.

sender.ts
import { Resource } from "sst";
import { SESv2Client, SendEmailCommand } from "@aws-sdk/client-sesv2";
const client = new SESv2Client();

And install the npm packages.

Terminal window
npm install @aws-sdk/client-sesv2

Test your app

To test our app, hit the API.

Terminal window
curl https://wwwrwteda6kbpquppdz5i3lg4a0nvmbf.lambda-url.us-east-1.on.aws

This should print out Sent! and you should get an email. You might have to check your spam folder since the sender and receiver email address is the same in this case.

Email sent from SST


5. Deploy your app

Now let’s deploy your app.

Terminal window
sst deploy

Next steps

As a next step you can:

  1. Request production access for SES
  2. And use your domain to send emails

This’ll let you send emails from your domain to any email address.