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

lambda-formation

v0.1.11

Published

A small framework for building nodejs AWS Lambda projects that are compatible with AWS CloudFormation Custom Resources

Downloads

501

Readme

lambda-formation

A small framework for building nodejs AWS Lambda projects that are compatible with AWS CloudFormation Custom Resources.

lambda-formation

NPM version Build Status Dependency Status Coverage percentage Code Climate

The functions in a lambda-formation project can also be run directly through the AWS Lambda API. They will work with but do not rely on AWS CloudFormation.

Getting Started

Use the Yeoman generator generator-lambda-formation

yo lambda-formation:project my-lambda-formation-project

Now lets create a resource:

cd my-lambda-formation-project

yo lambda-formation:resource resource1

Now you have a project structure similar to the following:

- my-lambda-formation-project
|- .gitignore
|- LICENSE
|- README.md
|- index.js
|- lib
 |- resources
 |- README.md
 |- resource1
  |- create.js
  |- delete.js
  |- index.js
  |- update.js
|- package.json

Add as many resources as necessary. A resource will map directly to a CloudFormation Custom Resource. Beneath each resource a skeleton for controling Create, Update and Delete request types have be generated.

How does it work?

The project is designed so that calling a function from CloudFormation is as seamless as possible.

For the following Custom Resource definition:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "MyLambdaFormationResource": {
      "Type": "Custom::resource1",
      "Properties": {
        "ServiceToken": ARN_OF_LAMBDA_FORMATION,
        "myFirstProperty": "First Property",
        "mySecondProperty": {
          "name": "A Sub Property"
        }
      }
    }
  }
}

CloudFormation will run the lamda code at ARN_OF_LAMBDA_FORMATION, which is the zip of the lambda-formation project.

In this example lambda-formation will load resource1 based on the Custom::resource1 type. If this project had multiple resources any other resource could be executed by NAME with Custom::NAME.

If this is the first execution of the CloudFormation stack the handler in create.js will be executed. When the stack is updated the handler in update.js is executed. When the stack is removed the handler in delete.js is executed.

Direct call through Lambda

While the structure is designed to work with CloudFormation any handler can be called directly though Lambda.

The main handler in the root index.js file is a router that can get to any resource in the project.

Parameters to the main handler support multiple cases for the first letter. CloudFormation defaults to capitals while it is convention for most other node project to use lower case. This project supports both.

resourceType|ResourceType - Name of the resource to load. Will forward to the handler in the index.js file of the named resource.

requestType|RequestType - Name for the type of request Create|Update|Delete. Can also be one of create|update|delete

Direct Lambda execution:

{
  "requestType": "delete",
  "resourceType": "resource1",
  "myCustomParam": "myCustomValue"
}

Cross-Region Support with SNS

For regions that do not support Lambda, an SNS topic can be set as the ServiceToken in the CloudFormation template.

Example

Run a CloudFormation Template sa-east-1 (as of this release does not support Lambda) that uses lambda-formation resources in us-east-1.

  1. Upload the lambda-formation project to us-east-1 (or other Lambda supported region)
  2. Create an SNS Topic in sa-east-1
  3. Subscribe the lambda-formation function from step #1 to the SNS Topic in step #2
  4. Launch CloudFormation in sa-east-1 with the ServiceToken set to the SNS Topic set in step #2

lambda-formation will take care of processing the SNS message and responding to the CloudFormation stack

Where does my code go?

The create.js, update.js and delete.js files under each resource in lib/resources is where any code or modules should be added.

Add code directly to the stubbed functions or write a module and require it here. Please do not alter the handler function. This function controls the routing and execution response based on direct Lambda versus CloudFormation calls.

Resources

Each resource file will have a create, update or delete funciton respectively. The only requirement is that util.done is called when any custom code has finish processing. The util.done function will prepare the responce for CloudFormation or as a return directly to Lambda and complete the context for the execution.

util.done(err,event,context[,data,id]);

On success err should be null

The event and context objects are required and can simply be passed through.

The data parameter is optional and if included must be null or an object of key/value pairs that describe resource. When executed though CloudFormation the keys will be availbale as Resource Outputs. For direct Lambda calls the object will be sent to context.done.

The id parameter is only required for 'create.js'. This will be the ID CloudFormation will use to track the resource. If id is provided then data must also be defined.

Logging

Logging is pre-configured via the winston library and can be included via lambda-formation:

var logger = require('lambda-formation').logger;

Within your code use the winston shortcut methods log, info, debug:

logger.log('info', 'My messages');
logger.info('My message');
logger.debug('My message');
...

Framework internal logs will be logged with log-level debug. To enable them to be logged to CloudWatch set the environment variable CFN_LOG_LEVEL to debug.

Examples

Sungard Availability Services | Labs

Sungard Availability Services | Labs

This project is maintained by the Labs team at Sungard Availability Services

GitHub: https://sungardas.github.io

Blog: http://blog.sungardas.com/CTOLabs/