Can we subscribe an Amazon SNS topic using an AWS Lambda function?
Yes. With help of sns.subscribe()
Please keep in mind: dont forget to confirm it sns.confirmSubscription()
If you mean can you use a message from a SNS topic as the trigger for a lambda function, then the answer is yes.
If you mean can you write a lambda function that adds a subscription to an SNS topic then the answer is yes. Towards the bottom are links for the language-specific AWS SDKs. Your Lambda function would need to have the appropriate permissions to be able to add a subscription as well.
Related
I have a question regarding the relationship between Lambda functions and Amazon SNS as a service.
In SNS you create subscriptions to a topic, one being a Lambda function. In my head I'd imagine this is to invoke and trigger the function when a message is sent out from that topic. But on the Lambda side you can create triggers to do that same thing, one of them being SNS.
What exactly is the difference, it kinda seems like they do the same thing. But if I create a subscription for a topic using a Lambda, it does not show up in the the triggers of the function in the Lambda services, which makes me wonder what exactly is the difference between these two?
What exactly is the difference?
There is no difference from the functional point of view. What you are observing are just console imperfections.
To make sure, I manually verified using my SNS and my lambda function.
When you use Create subscription in the SNS console, and add a lambda function, it does show up as a trigger in lambda console.
Similarly, if you use lambda console and add SNS trigger, you will find the function in the Subscriptions section of SNS console.
However, AWS console is not perfect. For example, if you remove the lambda function from Subscriptions, it will not remove it from triggers on lambda console. Instead you will see the following error:
For some this may be a bug, for others a feature. I learned not to rely heavily on what AWS console shows. Just double check if the subscription works or not.
I have created lambda and SNS using cloudformation. Here first lambda is invoking a SNS which has a subscription of another lambda. Here SNS is supposed to invoke alias of lambda. In SNS topic lambda ARN is showing as subscriber but in lambda it is not added as trigger. There is not a single invocation log of the lambda invoked from SNS. So is this problem about some kind of permission or else ? need help...
Yes, this is a permissions issue. You need to add a permission to the lambda function to allow SNS to invoke it.
Use the AWS::Lambda::Permission resource to add permissions to allow SNS to invoke the lambda function.
I had a heck of a time with one. I had originally set my lambda function to look for an event source of "aws:s3". In order to get the event source from an s3 event, I used "event.Records[0].eventSource". When I tested event.Records[0].eventSource == "aws:sns" to check if my SNS trigger had fired, it wouldn't work. It wasn't until I found sample SNS event JSON that I noticed that SNS events have an event source node of "EventSource". It's capital case. I changed my test to
event.Records[0].EventSource == "aws:sns"
and it worked. So much for consistency in event message formats.
I have a CloudFormation template which invokes a Lambda function to create a SNS topic. The create-stack works fine and the stack is created and then the topic is created. However, when I try to use update-stack on the same template to update the topic, I see some errors.
I have no idea what this "Internal Failure" is. And the CloudWatch log does not show any error, the Lambda function is also returning response for the "update" request.
I found a guy asking the similar question and the answer is
Important
After you create an Amazon SNS topic, you cannot update its properties
by using AWS CloudFormation. You can modify an Amazon SNS topic by
using the AWS Management Console.
As you know, my CloudFormation template is invoking a Lambda function to create the SNS topic. So, is it possible to call the CloudFormation template to invoke the Lambda function to update the SNS topic? Any help is appreciated.
One more thing: deleting and then creating the SNS topic cannot be considered as a good solution because there are queues subscribing to this topic.
Is that possible if the user create a ticket in freshdesk that needs to be trigger the AWS lambda function.
That shouldn't be that hard. I would like to recommend using the following architecture
FreshDesk Ticket Trigger
FreshDesk Ticket Trigger Handler Published Message to SNS Topic
AWS Lambda Configured to SNS Topic as Event Source
AWS Lambda Code Accepts the SNS topic message (as Input) and performs the necessary processing
The advantages of using SNS rather directly calling Lambda are
Reducing the exposure of AWS API to only SNS topic and completely sealing rest of the API (IAM Privileges)
Possibility of Fan-Out Architecture [Multiple Lambda Functions can listen to the same SNS topic - near zero configuration]
For anyone landing on this topic.
It's possible with Freshdesk Marketplace app. With onTicketCreate product event, any actions can be written to execute with a Serverless function. It's completely run in Freshworks platform cloud.
If required, it can call your AWS Lambda.
StackOverFlow Old Question
i have been searching around and what i got on SOF is this question which is very old. Now question is any changes have been made by AWS for this?
I am getting sns notifications but email is quite complicated and Header is too long.
is there any way to customized these things?
There isn't a built-in solution within AWS SNS service to modify the notification, however as an alternative you can consider the workaround of using :
SNS → Lambda → SES
Configure a Lambda function to be triggered by your SNS topic. The function retrieves the SNS notification and within it modify the content as desired and then send an email via SES.
Kindly keep in mind that there will be additional costs (Lambda & SES charges) introduced due to this workaround.