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

stackbridge

v1.2.0

Published

Just another infrastructure abstraction layer.

Downloads

61

Readme

stackbridge

Just another infrastructure abstraction layer.

Stackbridge allows you to define your AWS resources once. Use the same resource objects you create to deploy and interact with using via the SDK.

Installing

npm install stackbridge

Supported resources

  • SNS Topics
  • Lambda Functions
  • S3 Buckets

Examples

Creating a StackBridge Instance

A stackbridge instance contains the AWS Environment specific parameters required to deploy and interact with resources.

const stack: StackBridge = new StackBrdige({
    account: "123456789",
    region: "us-east-1",
    environment: "staging"
});

Creating an S3 Bucket

const bucket: Bucket = stack.Bucket("my-unique-bucket-name");

Putting object to bucket

bucket.putObject(
    "/key/path", 
    JSON.stringify({"object_content", "foobar"})
    );

Getting object from bucket

const object = bucket.getObject("/key/path")

Creating an SNS Topic

interface CoolPayload {
    event: string
}
const topic = stack.SNS<CoolPayload>("topic-name1");

Publishing a topic

topic.publish({
    "event" : "foobar"
});

Creating a simple NodeJS Lambda

const lambda: Lambda = stack.Lambda.pathLambda(
    'my_lambda', 
    'test/lambdas/path-lambda.ts', 
    'handler')

Event Based Lambdas

You can create event based lambdas that have built in functionality to handle the specific event or payload they are expected to recieve.

SNS Lambda

To create an SNS lambda, create a new class that extends SNSLambda and define you handler function. The payload interface that is expected to be recieved should match the one defined in the SNS Topic object that the lambda will be subscribed to.

export class CoolSNSLambda extends SNSLambda<CoolPayload> {

    public async handler (
        event: SNSEvent): Promise<any> {
        const campaign: CoolPayload = this.getPayload(event)
        return campaign
    }
};

this.getPayload will return a serialized payload from the standard string format that is received in the SNS Event. If you require more complex serialization you can provide a serializer function to your class:

export class CoolSNSLambda extends SNSLambda<CoolPayload> {

    protected serializer = (stringPayload: string): CoolPayload => {
        const json = JSON.parse(stringPayload)
        json.message = json.message === 'bar' ? json.message : 'foobar'
        return json
    }

    public async handler (
        event: SNSEvent): Promise<any> {
        const campaign: CoolPayload = this.getPayload(event)
        return campaign
    }
};

Then create your lambda:

import { CoolSNSLambda } from .
const lambda: Lambda = stack.Lambda.snsLambda('my_cool_lambda', CoolSNSLambda)

Subscribing a Topic to a Lambda Functions

topic.addLambdaSubscription(lambda);

Allow Lambda to Read & Write a bucket

lambda.canReadBuckets([bucket]);
lambda.canWriteBuckets([bucket]);

Grant Lambda AWS Managed Policy

lambda.addAwsManagedPolicy("AmazonS3ReadOnlyAccess");

Deploying

Simply create any ts file in your project that defines or imports any stack resources created through your app.

Import your StackBridge instance and define all deployable resources and export the deploy function.

src/sbstack.ts

# import resources and StackBridge instance
import { deploy } from "stackbridge";

stack.deployableResources(
    {
        buckets: [bucket],
        topics: [topic],
        lambdas: [lambda]
    }
);

export default deploy(stack);

On your first deploy you may need to run cdk bootstrap

cdk bootstrap aws://ACCOUNT-NUMBER/REGION-1

To create a differential

npx stackbridge diff ./src/sbstack.ts (or your export location)

To synthesize a template

npx stackbridge synth ./src/sbstack.ts

To deploy the stack

npx stackbridge deploy ./src/sbstack.ts

Running the environment locally

You can run your stack locally using localstack. It requires docker to be running on your system. If you want to run locally you must define your StackBridge instance as follows:

const stack: StackBridge = new StackBrdige({
    account: "000000000000",
    region: "us-east-1",
});

Install Localstack and cdklocal

pip install localstack
npm install -g aws-cdk-local aws-cdk

Start Localstack

SERVICES=lambda,sns,s3 localstack start

Boostrap your local environment

cdklocal bootstrap aws://000000000000/us-east-1

Deploy your stack locally

cdklocal deploy --app "npx ts-node src/sbstack.ts"