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

cloudformation-extensions

v0.0.3

Published

Extensions to Amazon CloudFormation

Downloads

24

Readme

cloudformation-extensions

Build Status Docker Automated build Docker Pulls npm

Extensions to Amazon's CloudFormation Template JSON format.

Installation

Use the Docker Container (recommended)

docker run -v .:/cfnex joshbalfour/cfnex -i /cfnex/{inFile} -o /cfnex/{outFile}

(mapping the current directory to /cfnex in the container)

or grab from NPM

npm install -g cloudformation-extensions

cfnex -i {inFile} -o {outFile}

Description

This is designed to make using Amazon's CloudFormation template format easier, more understandable, and more maintainable. CloudFormation templates are incredibly powerful, but quickly become complicated, and you end up with multiple thousands of lines of JSON which isn't very practical. Writing CloudFormation templates using cfnex retains the power of vanilla CloudFormation templates whilst increasing readability and maintainability.

Writing a cfnex extension is as easy as creating a function which takes an object and returns a promise. You can find examples of this in the extensions folder and a tutorial below.

Writing Cfnex Templates

You write cfnex templates the same way you would regular CloudFormation templates, but with the ability to call cfnex extensions the same way you would "Intrinsic Functions" ( Fn::GetAtt, Fn::Join, etc. ), like so:

{ "cfnex::extensionName" : [ "aString", {"anObject": "argument2"}, 42 ] }

Where the array there are the arguments to be passed to the extension.

Out of the box cfnex comes with two extensions:

  • include
  • includeFile

include allows you to include another JSON or JS file, and includeFile allows you to include a raw file.

The output of an extension is then parsed as cfnex template, so you can do things recursively.

Built-in Extensions

include Extension

Example

A raw cloudformation.json file which looks like this:

{

    "AWSTemplateFormatVersion" : "2010-09-09",

    "Description" : "projectname Stack",
    "Parameters" : {
        "s3BucketName" : {
          "Type" : "String",
          "Description" : "S3 Bucket Name",
          "Default" : "project.name"
        },

        "redisSize": {
          "Type" : "String",
          "Description" : "Redis Size",
          "Default" : "cache.t2.micro"
        },

        "rdbmsSize": {
          "Type" : "String",
          "Description" : "RDBMS Size",
          "Default" : "db.t2.micro"
        }
    }

}

Becomes two files:

a root cloudformation.cfnex.json file

{
    "AWSTemplateFormatVersion" : "2010-09-09",

    "Description" : "projectname Stack",
    "Parameters" : { "cfnex::include" : [ "./parameters.json" ] },

}

and a separate parameters.json file

{
    "s3BucketName" : {
      "Type" : "String",
      "Description" : "S3 Bucket Name",
      "Default" : "project.name"
    },

    "redisSize": {
      "Type" : "String",
      "Description" : "Redis Size",
      "Default" : "cache.t2.micro"
    },

    "rdbmsSize": {
      "Type" : "String",
      "Description" : "RDBMS Size",
      "Default" : "db.t2.micro"
    }
}

include-file extension

A raw cloudformation.json file which includes this:

"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
            "#!/bin/bash\n",
            "echo ECS_CLUSTER=", { "Ref": "EcsCluster" }, " >> /etc/ecs/ecs.config"
          ] ] } }

a root cloudformation.cfnex.json file

"UserData" : { "Fn::Base64" : { "cfnex::include-file" : [ "./ec2/boot.sh" ] } }

and a separate ./ec2/boot.sh file

#!/bin/bash
echo ECS_CLUSTER=<%cfnex { "Ref": "EcsCluster" } cfnex%>  >> /etc/ecs/ecs.config

Note the usage of <%cfnex tags in the included file, these can be used to include JSON in the included file.

Configuration

Your can override where cfnex looks for your extensions by having a .cfnexrc JSON file in the directory you run cfnex in which looks like this:

{
    "extensionsDirectory": "customDirName",
}

Writing your own extensions

You can write your own extensions by having an extensions directory, and within it your custom extensions, where the name of the extension is the filename, so if I wanted to refer to my extension as

{ "cfnex::includeYaml" : [ "myfile.yml" ] }

My extensions directory would look like this:

extensions
    - includeYaml.js

A cfnex extension is a function which returns a Promise which resolves to be an object. The function to be ran must be module.exported from the file. The function will be called with two parameters: args and context. args is whatever the extension was passed in the cfnex template file, so for our includeYaml extension it'd be

[ "myfile.yml" ]

context is an object which contains two properties:

  • cwd - String - the absolute current working directory that you're being ran in
  • logger - Object - a logging helper which has ok, error, debug, and info properties for you to use instead of plain console.log etc.

The object you return must have a property called output which is the output of your extension which will be the object can optionally have another property called contextChanges which should be an object. You can use this to override the context for any cfnex templates your extension returns.

CLI Usage

Usage:
  CloudFormation Extensions (cfnex) [OPTIONS] [ARGS]

Options: 
  -i, --inFilePath FILE  Input cfnex JSON file
  -o, --outFilePath DIR  Write to FILE rather than the console
      --cwd DIR          Override the working directory (defaults to the input file's directory)
  -k, --no-color         Omit color from output
      --debug            Show debug information
  -v, --version          Display the current version
  -h, --help             Display help and usage details

License