Subscribing AWS Lambda Function To SNS Topic With aws-cli

The aws-cli documentation and command line help text have not been updated yet to include the syntax for subscribing an AWS Lambda function to an SNS topic, but it does work!

Here’s the format:

aws sns subscribe \
  --topic-arn arn:aws:sns:REGION:ACCOUNT:SNSTOPIC \
  --protocol lambda \
  --notification-endpoint arn:aws:lambda:REGION:ACCOUNT:function:LAMBDAFUNCTION

where REGION, ACCOUNT, SNSTOPIC, and LAMBDAFUNCTION are substituted with appropriate values for your account.

For example:

aws sns subscribe –topic-arn arn:aws:sns:us-east-1:012345678901:mytopic
–protocol lambda
–notification-endpoint arn:aws:lambda:us-east-1:012345678901:function:myfunction

This returns an SNS subscription ARN like so:

{
    "SubscriptionArn": "arn:aws:sns:us-east-1:012345678901:mytopic:2ced0134-e247-11e4-9da9-22000b5b84fe"
}

You can unsubscribe with a command like:

aws sns unsubscribe \
  --subscription-arn arn:aws:sns:us-east-1:012345678901:mytopic:2ced0134-e247-11e4-9da9-22000b5b84fe

where the subscription ARN is the one returned from the subscribe command.

I’m using the latest version of aws-cli as of 2015-04-15 on the GitHub “develop” branch, which is version 1.7.22.