Invoke AWS Lambda with Amazon SNS from Different Accounts?

user6438693

I have an AWS account in which i have SNS topics. I have a different AWS account in which i have created a basic lambda function using this

Now i want to trigger this lambda function using the SNS which i have in different account.

What are the permissions i need to provide to my lambda function/execution Role? What is the best practice to achieve that?

Any lead is highly appreciated.

raevilman

Here you go, below link has all you need

https://docs.aws.amazon.com/lambda/latest/dg/with-sns-create-x-account-permissions.html

Steps mentioned in above link:

1) From account A, create the Amazon SNS topic:

aws sns create-topic --name lambda-x-account

Note the topic arn that is returned by the command. You will need it when you add permissions to the Lambda function to subscribe to the topic.

2) From account B, create the Lambda function. For the runtime parameter, select either nodejs6.10, nodejs4.3, python3.6, python2.7 or java8, depending on the code sample you selected when you created your deployment package.

aws lambda create-function \
    --function-name SNS-X-Account \
    --runtime runtime language \
    --role role arn \
    --handler handler-name \
    --description "SNS X Account Test Function" \
    --timeout 60 \
    --memory-size 128 \
    --zip-file fileb://path/LambdaWithSNS.zip  

Note the function arn that is returned by the command. You will need it when you add permissions to allow Amazon SNS to invoke your function.

3) From account A add permission to account B to subscribe to the topic:

aws sns add-permission \
    --region us-east-1 \
    --topic-arn Amazon SNS topic arn \
    --label lambda-access \
    --aws-account-id B \
    --action-name Subscribe ListSubscriptionsByTopic Receive

4) From account B add the Lambda permission to allow invocation from Amazon SNS:

aws lambda add-permission \
    --function-name SNS-X-Account \
    --statement-id sns-x-account \
    --action "lambda:InvokeFunction" \
    --principal sns.amazonaws.com \
    --source-arn Amazon SNS topic arn 

In response, Lambda returns the following JSON code. The Statement value is a JSON string version of the statement added to the Lambda function policy:

{
    "Statement": "{\"Condition\":{\"ArnLike\":{\"AWS:SourceArn\":\"arn:aws:lambda:us-east-1:B:function:SNS-X-Account\"}},\"Action\":[\"lambda:InvokeFunction\"],\"Resource\":\"arn:aws:lambda:us-east-1:A:function:SNS-X-Account\",\"Effect\":\"Allow\",\"Principal\":{\"Service\":\"sns.amazonaws.com\"},\"Sid\":\"sns-x-account1\"}"
}    

Note

Do not use the --source-account parameter to add a source account to the Lambda policy when adding the policy. Source account is not supported for Amazon SNS event sources and will result in access being denied. This has no security impact as the source account is included in the source ARN.

5) From account B subscribe the Lambda function to the topic:

aws sns subscribe \
    --topic-arn Amazon SNS topic arn \
    --protocol lambda \
    --notification-endpoint arn:aws:lambda:us-east-1:B:function:SNS-X-Account

You should see JSON output similar to the following:

{
    "SubscriptionArn": "arn:aws:sns:us-east-1:A:lambda-x-account:5d906xxxx-7c8x-45dx-a9dx-0484e31c98xx"
}

6) From account A you can now test the subscription. Type "Hello World" into a text file and save it as message.txt. Then run the following command:

aws sns publish \
    --topic-arn arn:aws:sns:us-east-1:A:lambda-x-account \
    --message file://message.txt \
    --subject Test

This will return a message id with a unique identifier, indicating the message has been accepted by the Amazon SNS service. Amazon SNS will then attempt to deliver it to the topic's subscribers.

Note

Alternatively, you could supply a JSON string directly to the message parameter, but using a text file allows for line breaks in the message.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

AWS Lambda Publishing to SNS

分類Dev

AWS Lambda to SNS response after invocation

分類Dev

AWS SNS - How to get subscriber lambda triggered by publisher lambda output

分類Dev

Invoke a AWS Lambda function by a http request

分類Dev

AWS Lambda 関数を使用して Amazon SNS トピックをサブスクライブしますか?

分類Dev

Invoke AWS Lambda function when multiple Lambda function is done

分類Dev

How to let AWS lambda in a VPC to publish SNS notification?

分類Dev

send push notification using lambda with aws sns for single device

分類Dev

AWS Lambda different IP address for each Lambda

分類Dev

Can I use two Amazon MWS accounts from the same server?

分類Dev

Amazon SNS For Apple - Error loading apple credentials from file

分類Dev

Invoke a AWS Lambda function via Google App Script

分類Dev

Invoke AWS Lambda function only once, at a single specified future time

分類Dev

How do I invoke a step function using AWS Lambda ( Python)

分類Dev

AWS Lambda - editing functions outside of amazon.com

分類Dev

呼び出し後のAWS LambdaからSNSへの応答

分類Dev

AWS: Publish SNS message for Lambda function via boto3 (Python2)

分類Dev

AWS Lambda Alexa Intent、Node.js内からSNSへの公開

分類Dev

AWS:Lambda関数からSNSへの公開

分類Dev

AWS Lambda SNSトリガーイベントタイプ?

分類Dev

AWS CloudFormation for Lambda および SNS トピック

分類Dev

Amazon SNSの確認

分類Dev

How aws accounts are differentiated?

分類Dev

FCM with AWS SNS

分類Dev

FCM with AWS SNS

分類Dev

AWS Lambda&SNS:Lambdaクロスリージョンを呼び出す

分類Dev

I cant invoke javascript from functions on a different class

分類Dev

AWS Lambda(node12.x) with KnexJs only invoke on first run and fails on the subsequent times

分類Dev

Setting http response header from AWS lambda

Related 関連記事

  1. 1

    AWS Lambda Publishing to SNS

  2. 2

    AWS Lambda to SNS response after invocation

  3. 3

    AWS SNS - How to get subscriber lambda triggered by publisher lambda output

  4. 4

    Invoke a AWS Lambda function by a http request

  5. 5

    AWS Lambda 関数を使用して Amazon SNS トピックをサブスクライブしますか?

  6. 6

    Invoke AWS Lambda function when multiple Lambda function is done

  7. 7

    How to let AWS lambda in a VPC to publish SNS notification?

  8. 8

    send push notification using lambda with aws sns for single device

  9. 9

    AWS Lambda different IP address for each Lambda

  10. 10

    Can I use two Amazon MWS accounts from the same server?

  11. 11

    Amazon SNS For Apple - Error loading apple credentials from file

  12. 12

    Invoke a AWS Lambda function via Google App Script

  13. 13

    Invoke AWS Lambda function only once, at a single specified future time

  14. 14

    How do I invoke a step function using AWS Lambda ( Python)

  15. 15

    AWS Lambda - editing functions outside of amazon.com

  16. 16

    呼び出し後のAWS LambdaからSNSへの応答

  17. 17

    AWS: Publish SNS message for Lambda function via boto3 (Python2)

  18. 18

    AWS Lambda Alexa Intent、Node.js内からSNSへの公開

  19. 19

    AWS:Lambda関数からSNSへの公開

  20. 20

    AWS Lambda SNSトリガーイベントタイプ?

  21. 21

    AWS CloudFormation for Lambda および SNS トピック

  22. 22

    Amazon SNSの確認

  23. 23

    How aws accounts are differentiated?

  24. 24

    FCM with AWS SNS

  25. 25

    FCM with AWS SNS

  26. 26

    AWS Lambda&SNS:Lambdaクロスリージョンを呼び出す

  27. 27

    I cant invoke javascript from functions on a different class

  28. 28

    AWS Lambda(node12.x) with KnexJs only invoke on first run and fails on the subsequent times

  29. 29

    Setting http response header from AWS lambda

ホットタグ

アーカイブ