Using API Gateway to publish SNS topics / multiple lambda function with API Gateway

 

Questions


Right now my requirement is, whenever I get data through API, I have to save it into 2-3 different places (for example, into my own DB, into some BI service and also sometimes into a logging DB).

I don’t know if it’s possible to bind a single resource and single method into multiple lambda functions or so. So, my alternate approach was, as I already know how to trigger multiple lambda functions by subscribing to SNS topic, I thought maybe if I can somehow publish to SNS topic from the API Gateway, the rest will be easy. My current thinking is something below:

current implementation thinking

But the problem is, I am not able to publish to SNS topic from the API Gateway. I am getting errors like TopicArn or TargetArn Reason: no value for required parameter.

My approach is, create a normal SNS topic. Then, create a special role policy like below:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "StmtXXXXXXXXXXX",
            "Effect": "Allow",
            "Action": [
                "sns:Publish",
                "sns:Subscribe",
                "sns:Unsubscribe"
            ],
            "Resource": [
                "SNS-TOPIC-ARN"
            ]
        }
    ]
}

Then creating a API with POST/GET method (I tried both) and added SNS topic as AWS Service Proxy and the Role as Execution role.

 

 

————————————————-

Answer

You have to pass in TopicArn or TargetArn to SNS via AWS API Gateway. There are the different ways can archive that.

1.
You can create a method request parameter, then create an integration request query string parameter is named TopicArn/TargetArn and map the method request parameter to it.

2.
You can create an integration request query string parameter is named TopicArn/TargetArn and put your Arn as a static value.

Here is the step by step instruction provided by AWS API Gateway.

amazon-sns,amazon-web-services,aws-api-gateway,aws-lambda

Leave a Reply

Your email address will not be published. Required fields are marked *