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

aws-cfn-custom-resource

v2.0.1

Published

Library to help write AWS CloudFormation custom resources.

Downloads

22

Readme

CloudFormation Custom Resource

A node.js library to help write CloudFormation custom resources.

Usage

Install

Install the module: npm install aws-cfn-custom-resource --save

Import the module: var resource = require('aws-cfn-custom-resource');

Handler

Create a module for the main custom resource handler. This handler maps custom resource type names to modules that process the events for that resource. This is the handler that is provided to the lambda function that executes the custom resource.

Example

resource.js

var resource = require('aws-cfn-custom-resource');

exports.handler = resource.handler({
  'Custom::MyCustomResource': function () { return require('./lib/my-custom-resource'); }
});

Resource Module

Implement request handlers for a custom resource type.

The resource module must export three functions:

  • handleDelete
  • handleCreate
  • handleUpdate

Input

The single parameter to a handler is a CloudFormationEvent.

The event includes all the properties from a custom resource request, plus some additional information detailed below.

<dt>stack.accountId</dt>
<dd>
<p>ID of the AWS account the stack that created the resource is located in.</p>
</dd>

<dt>stack.name</dt>
<dd>
<p>Name of the CloudFormation stack that the resource is a part of.</p>
</dd>

<dt>stack.arn</dt>
<dd>
<p>Full ARN of the CloudFormation stack that the resource is a part of. Alias for <em>StackId</em>.</p>
</dd>

Output

A handler should return one of the following:

  • undefined/null to indicate success
  • an object with keys:
    • physicalResourceId - optional - Identifier unique to the custom resource vendor. If not provided, a resource id is generated.
    • data - optional - Custom resource provider-defined name-value pairs to send with the response. The values provided here can be accessed by name in the template with Fn::GetAtt.
  • a promise that results in one of the above values

A handler may also throw an error to indicate failure.

AWS API

This library provides a helper for the AWS JavaScript SDK that converts SDK functions from callback to promises. The helper can make it easier to stub/mock AWS APIs in unit tests.

var resource = require('aws-cfn-custom-resource');

exports.handleCreate = function (event) {
  var session = new resource.aws.Session();
  var ec2 = session.client('EC2');

  return ec2.describeInstances({InstanceIds: ['i-234232']})
  .then(function (result) {
    // do something magical
  });
};

Example

lib/my-custom-resource.js

exports.handleDelete = function (event) {
  // Return nothing and the custom resource responds with success.
  // Can also:
  //   throw new Error("some message");
  //   -- or
  //   var resource = require('aws-cfn-custom-resource');
  //   throw resource.newError("MyException", "some message");
  //   -- or
  //   return {physicalResourceId: "myResource"};

  // You can also do the following:
  //   throw "some message";
  // However, you will not get a stack trace in this case. It is recommended
  // to throw Error instead.
};

exports.handleUpdate = function (event) {
  // Async handlers should respond with a promise.
  var Q = require('q');
  var deferred = Q.defer();

  doSomethingAsync(function (err, result) {
    if (err) {
      deferred.reject(err);
    } else {
      deferred.resolve({
        physicalResourceId: 'SomeResourceId',
        data: {
          key1: "Value",
          key2: "Value2"
        }
      });
    }
  });

  return deferred.promise();
};

// Sometimes it makes sense to use the same implementation for create & update.
exports.handleCreate = exports.handleUpdate;