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

gulp-cf-deploy

v2.2.1

Published

Gulp plugin for deploying AWS CloudFormation stacks

Downloads

2,959

Readme

Deploy CloudFormation stacks in Gulp

Does what it says on the can.

Why this package?

The headline benefit over other packages is the surfacing of stack errors: Example console error output

Other benefits:

  • Lists resources created / updated / deleted
  • Errors are displayed ASAP, so you can get to fixing them while the rollback completes
  • On failure a URL is provided direct to the stack details in the AWS console for full details
  • Parameters can be passed as a hash / plain object (rather than in CloudFormation's verbose format)
  • Waits for the stack to be deployed before finishing, so tasks in series can rely on the resources being available
  • But does not wait for cleanup of old resources, which should not impact following tasks
  • Keeps you updated while waiting for the deployment to complete
  • Pipes the outputs of the stack as simplified JSON through the Gulp stream so you can save the outputs or process them further
  • Defaults to deleting a failed stack creation rather than the CloudFormation default of 'rollback' which then has to be manually deleted to try again (however, even when the delete has completed the full details of the failed stack are still available at the console URL)

Requirements

This packages has a peer dependency on Gulp v4, which you can currently install as gulp@next.

Install

yarn add --dev gulp-cf-deploy

Or if you're still in npm world: npm install --save-dev gulp-cf-deploy

API

import cfDeploy from 'gulp-cf-deploy'

gulp.task('deploy:aws', () =>
  gulp
    .src('resources.yaml')
    .pipe(cfDeploy(awsServiceOptions, stackNameOrOptions, parameters))
    .pipe(gulp.dest('build')),
)

Will deploy (create or update) the CloudFormation stack defined in resources.yaml and save it's outputs as build/resources.json

awsServiceOptions: passed to new AWS.CloudFormation(). Provide your AWS credentials (if not already set in AWS.config) and region.

stackNameOrOptions: passed to createStack() or updateStack(). Often all that's needed is the StackName in which case you can just pass the name as a string. In addition:

  • StackName: defaulted to the source file's 'stem', in the example above the stack will be named resources
  • Parameters: will be supplemented with the 3rd argument parameters
  • TemplateBody: is pulled from the source file's content
  • OnFailure: set to DELETE when creating a stack, otherwise the stack needs to be manually deleted to try again (even when deleted the stack can be inspected in the AWS Console for 30 days)
  • Capabilities: Needs to be set if creating IAM resources, see the SDK docs.

parameters: a hash / plain object of parameters that is merged into Parameters (if any) of stackNameOrOptions. For example: { Foo: 123, ... } becomes [{ ParameterKey: 'Foo', ParameterValue: 123 }, ...]

Output Vinyl file: The Outputs of the stack is simplified into a hash / plain object. For example [{ OutputKey: 'Foo', OutputValue: 123 }, ...] becomes { Foo: 123, ... }. The stream output is a file with the same properties as the source file expect:

  • contents: JSON of the simplified outputs, pipe through gulp-json-editor to easily modify
  • data: the simplified outputs object
  • extname: extension is set to json
  • Name is unchanged: pipe through gulp-rename if you want to change it

Post-processing example

If you are creating IAM access keys then there are a couple of considerations.

  1. CloudFormation will only output the SecretAccessKey when the key is first created (or regenerated)
  2. When doing anything with IAM users you need to specify so in the stack options

---
Resources:
  fooBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: 'bucket-of-foo'
  bobUser:
    Type: 'AWS::IAM::User'
    Properties:
      Policies: ...
  bobAccessKey:
    Type: 'AWS::IAM::AccessKey'
    Properties:
      Serial: 1
      UserName: !Ref bobUser
Outputs:
  fooBucketArn:
    Value: !Sub 'arn:aws:s3:::${fooBucket}'
  bobAccessKey:
    Value: !Ref bobAccessKey
  bobSecretKey:
    Value: !GetAtt bobAccessKey.SecretAccessKey

CloudFormation will output fooBucketArn and bobAccessKey each deployment. But bobSecretKey will only be output when the keys are first created, or regenerated (for example, if the Serial was changed). Also, for security, you would want to be managing you secret and access keys separately from other resource information (such as fooBucketArn).

You can handle this as follows:

import AWS from 'aws-sdk/global'
import gulp from 'gulp'
import clone from 'gulp-clone'
import filter from 'gulp-filter'
import rename from 'gulp-rename'
import streamToPromise from 'stream-to-promise'
import jsonEditor from 'gulp-json-editor'
import omitBy from 'lodash/omitBy'

const region = 'ap-southeast-2'
gulp.task('deploy:aws', () => {
  const cfOutput = gulp.src('deploy/resources.yaml').pipe(
    cfDeploy(
      {
        credentials: new AWS.Config().credentials,
        region,
      },
      {
        Capabilities: ['CAPABILITY_IAM'], // Required because we are creating a user
        StackName: `project-resources`,
      },
    ),
  )
  const secrets = cfOutput
    .pipe(clone())
    .pipe(filter(file => file.data.bobSecretKey)) // Only perform if secret key was output
    .pipe(
      jsonEditor(resources => ({
        accessKeyId: resources.bobAccessKey,
        secretAccessKey: resources.bobSecretKey,
      })),
    )
    .pipe(rename('bob-credentials.json'))
    .pipe(gulp.dest('secrets', { mode: 0o600 }))
  const resources = cfOutput
    .pipe(
      jsonEditor(resources => ({
        ...omitBy(resources, (v, k) => k.endsWith('Key')),
        region,
      })),
    )
    .pipe(gulp.dest('build')) // No need to rename, will be resources.json
  return Promise.all([secrets, resources].map(streamToPromise))
})