Zum Inhalt springen

Use SNS as both trigger and destination of AWS lambda

Prerequisites

  • AWS console access
  • S3 bucket
  • Two Amazon SNS
  • Lambda configuration

Overview

Image description

SNS

Step 1: Create first SNS

Image description

  • Add SNS policy
{
  "Version": "2012-10-17",
  "Id": "sns-ID",
  "Statement": [
    {
      "Sid": "Example SNS topic policy",
      "Effect": "Allow",
      "Principal": {
        "Service": "s3.amazonaws.com"
      },
      "Action": "SNS:Publish",
      "Resource": "SNS-topic-ARN",  #arn of thie created sns
      "Condition": {
        "StringEquals": {
          "aws:SourceAccount": "426857564226"
        },
        "ArnLike": {
          "aws:SourceArn": "arn:aws:s3:::prashansa-sns-bucket"
        }
      }
    }
  ]
}

*Step 2: * Go to subscription and click on create subscription

  • Create subscription setting protocol as AWS Lambda that means the destination of this SNS is AWS Lambda.

Image description

Step 3: Create another SNS

  • Another SNS for notifying user via email

Image description

  • Set policy for this SNS

*Step 4: * Create subscription

  • Set the protocol to email for this SNS to send direct notification to the user via email

Image description

S3 Bucket

*Step 5: * Create S3 bucket

Image description

Step 6: Go to properties > event notification > create event notification

Image description

Step 7: Go to Destination

  • Scrolling down there is destination

Image description

Lambda

Step 8: Create lambda function

Image description

Step 9: Add trigger

  • Add the first SNS whose protocol was set to lambda as trigger

Image description

Step 10: Set Lambda destination

  • In the lambda destination, set the SNS secondly created that was set the protocol for email as the lambda destination

Image description

Testing

Step 11: Upload the image in S3 bucket

Image description

Step 12: Check the email for notification

Image description

References

https://repost.aws/knowledge-center/lambda-invoke-error-s3-bucket-permission

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert