https://serverless-stack.com/ logo
#help
Title
# help
t

Tharshan

11/15/2021, 11:49 AM
Hey folks - how can I use SST to setup email sending via AWS in the stack?
m

manitej

11/15/2021, 11:50 AM
Use AWS SES
t

Tharshan

11/15/2021, 11:51 AM
@manitej ok gotcha. Do we have to configure that manually within AWS console? I was asking if there is a way to do it with IaC within SST stacks
m

manitej

11/15/2021, 11:53 AM
To grant all ur API endpoints permission to SES, change const api = new sst.Api(this, "Api", { routes: { "POST /": "src/lambda.handler", }, }); to const api = new sst.Api(this, "Api", { permissions: ["ses"], routes: { "POST /": "src/lambda.handler", }, });
Use
aws-cdk
to send emails
t

Tharshan

11/15/2021, 1:35 PM
When I apply that permissions change
it says no change detected
Is that normal?
f

Frank

11/15/2021, 5:20 PM
Thanks for chiming in @manitej
@Tharshan give this a try:
Copy code
const api = new sst.Api(this, "Api", {
  defaultFunctionProps: {
    permissions: ["ses"],
  }
  routes: {
    "POST /": "src/lambda.handler",
  },
});
I also had to add the sender addresses in SES, and validate them.
I did it manually a long while back. Not sure if you can do that automatically in CloudFormation these days.
t

Tharshan

11/15/2021, 5:29 PM
Hey @Frank - yep I got it to work well. Since I am sending notifications via different services like webhooks, email (ses) and slack - I am trying to switch to SNS To get a fanout pattern - so it sends to each consumer
The consumer gets a message like so
Receipt sent! {
Records: [
{
EventSource: 'aws:sns',
EventVersion: '1.0',
EventSubscriptionArn: 'arn:aws:sns:us-west-2:132730161483:dev-slack-feedback-widget-NotifyTopic:3e80d4df-78f4-40e8-ba5b-3b415ee0cf8d',
Sns: [Object]
}
]
}
But i don’t see the message I published to the topic
Copy code
const topicPayload = {
      default: JSON.stringify(payload),
    };
await sns
        .publish({
          TopicArn: process.env.topicArn,
          Message: JSON.stringify(topicPayload),
          MessageStructure: "json",
        })
        .promise();
Unless this is possible with SQS too?
f

Frank

11/15/2021, 5:33 PM
The consumer gets a message like so
What consumer is this, Queue or Kinesis Stream?
t

Tharshan

11/15/2021, 5:35 PM
It’s a Topic construct
Copy code
const topic = new sst.Topic(this, "NotifyTopic", {
      subscribers: [
        "src/lambda/consumers/email.main",
        "src/lambda/consumers/webhook.main",
        "src/lambda/consumers/zapier.main",
        "src/lambda/consumers/slack.main",
      ],
    });
Ah I see the message is actually inside the Sns key
f

Frank

11/15/2021, 5:35 PM
ah yeah
t

Tharshan

11/15/2021, 5:35 PM
Strange that it always give me an array of records
Is there a way to only get it to give me one object?
f

Frank

11/15/2021, 5:40 PM
yeah it’s always an array
t

Tharshan

11/15/2021, 5:45 PM
Ah Ok - so I should expect to always process them as an array cause there maybe more than one?
f

Frank

11/15/2021, 6:03 PM
yeah, you will always receive an array, I just loop through them