npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serverless-sns-filter-tmp

v2.0.0

Published

Plugin to add SNS Subscription Fiters to a serverless SNS event

Downloads

2

Readme

serverless-sns-filter

A serverless plugin to add SNS Subscription filters to lambda events

For an example of a working application, see the integration-test

This plugin adds the following resources to your Serverless Cloudformation stack:

  • A lambda function (invoked using CloudFormation Custom Resources) that adds an SNS Subscription Filter to a SNS Subscription
  • An IAM Role, with a policy, to allow the above Lambda to create the SNS subscription filters including the following:
- Effect: Allow
           Action:
           - SNS:ListSubscriptions
           Resource:
           - Fn::Sub: arn:aws:sns:${AWS::Region}:${AWS::AccountId}:*
         - Effect: Allow
           Action:
           - SNS:setSubscriptionAttributes
           Resource:
             Fn::Sub: arn:aws:sns:${AWS::Region}:${AWS::AccountId}:*

Installation

Install the plugin:

npm install serverless-sns-filter

Add the plugin to your serverless.yml

plugins:
  - serverless-sns-filter

Configuration

Add a filter policy to your function under the events section:

functions:
  hello:
    handler: handler.hello
    events:
      - sns: ${self:custom.greeterTopic}
        # filter policy to accept messages with attrib_one values including "foo" OR "bar"
        filter:
          attrib_one:
            - foo
            - bar

Usage

SNS Subscription filters require SNS notifications to include MessageAttributes that the filters are matched against.

Example, NodeJS lambda that shall post a message that shall be received by the hello function above:

serverless.yml:

  sendMessage:
    handler: send.with_attribute
    environment:
      TOPIC_ARN: "${{self:custom.greeterTopicArn}}"

send.js:

const AWS = require('aws-sdk')
let sns = new AWS.SNS()

const publish_sns = (params, callback) => {
  sns.publish(params).promise().then(done => {
    console.log(JSON.stringify(done))
    callback(null,done)
  }).catch(error => {
    console.log(JSON.stringify(error))
    callback(error,"send failed")
  })
}

const with_attribute = (event, context, callback) => {
  const topicArn = process.env['TOPIC_ARN']
  console.log(`publishing to topic: ${topicArn}`)
  const params = {
    Message: 'should be received',
    MessageAttributes: {
      'attrib_one': {
        DataType: 'String',
        StringValue: 'foo'
      }
    },
    Subject: "Successful message",
    TopicArn: topicArn
  }

  publish_sns(params,callback)

}

module.exports = {
  with_attribute: with_attribute
}

Limitations

  • SNS Subscription Filters cannot be removed at the present time. If you need to remove a filter, then remove the SNS subscription and re-create it without the filter definition.

  • The plugin currently only supports a single SNS filter per function.

Contributing

  • The person who found a bug should fix the bug by themself.
  • If you find a bug and cannot fix it by yourself, send a pull request and attach test code to reproduce the bug, please.
  • The person who want a new feature should implement it by themself.
  • For above reasons, we accept pull requests only and disable issues.
  • If you're not sure how to fix an issue, please create a pull request demonstrating a minimal viable complete example as a failing test.
  • If you'd like to discuss about a new feature before implement it, make an empty commit and send a WIP pull request. But It is better that the WIP PR has some code than an empty commit.
  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Publishing

cd plugin
yarn version
yarn run test
yarn run compile
npm login
npm publish