Hey folks - how can I use SST to setup email sendi...
# help
t
Hey folks - how can I use SST to setup email sending via AWS in the stack?
m
Use AWS SES
t
@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
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
When I apply that permissions change
it says no change detected
Is that normal?
f
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
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
The consumer gets a message like so
What consumer is this, Queue or Kinesis Stream?
t
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
ah yeah
t
Strange that it always give me an array of records
Is there a way to only get it to give me one object?
f
yeah it’s always an array
t
Ah Ok - so I should expect to always process them as an array cause there maybe more than one?
f
yeah, you will always receive an array, I just loop through them