• Stars
    star
    190
  • Rank 203,739 (Top 5 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ“ฆ Create, deploy to, and shutdown Amazon EC2 instances

grunt-ec2 Build Status Dependency Status help me on gittip flattr.png ga

Grunt tasks to create, terminate, and deploy Node applications to AWS EC2 instances, then proxy with nginx

Abstracts away aws-sdk allowing you to easily launch, terminate, and deploy to AWS EC2 instances.

Note: This is a very, very opinionated package. You're invited to fork it and produce your own flow, and definitely encouraged to create pull requests with your awesome improvements.

Jump to the task reference to see what it can do.

Features

This is pretty feature packed

  • Launch EC2 instances and set them up with a single task. Look ma', no hands!
  • Shut them down from the console. No need to look up an id or anything.
  • Use individual SSH key-pairs for each different instance, for increased security
  • Deploy with a single Grunt task, using rsync for speed
  • Use pm2 to deploy and do hot code swaps!
  • Works after reboots, too
  • Introduced at Pony Foo
  • Put an nginx proxy server in front of Node
  • Get tons of Grunt tasks to manipulate your EC2 instances
  • Supports turning on ssl and spdy in your nginx server!

Installation

npm install --save-dev grunt-ec2

You'll need to set up the AWS configuration for the project. You'll also need to have a Security Group set up on AWS. Make sure to enable rules for inbound SSH (port 22) and HTTP (port 80) traffic.

Setup

grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    ec2: 'path/to/whatever.json'
});

grunt.loadNpmTasks('grunt-ec2');

Then, in your whatever.json file:

{
  "AWS_ACCESS_KEY_ID": "<redacted>",
  "AWS_SECRET_ACCESS_KEY": "<redacted>",
  "AWS_SECURITY_GROUP": "something"
}

You'll need to get an access key pair for AWS, as well as create a security group on AWS by hand. Creating security groups is not supported by this package yet.

The package.json entry is used to take the version number when deploying.

For the ec2 configuration, I don't take an object directly to encourage hiding away the deployment variables, granting them only to people who can actually perform deploys. You could provide an object directly, but it's strongly discouraged.

Configuration

If you're confident enough, you can use the tool with just those options. Here is the full set of options and their defaults.

Variable Name Purpose
"AWS_DEFAULT_REGION" Passed to the CLI directly, defaults to "us-east-1"
"AWS_ELB_NAME" The default Elastic Load Balancer you want to use with ec2-elb-attach and ec2-elb-detach.
"AWS_IMAGE_ID" Used when creating a new instance with the ec2-create-instance task. Defaults to the "ami-c30360aa" Ubuntu AMI.
"AWS_INSTANCE_TYPE" The magnitude for our instance. Defaults to "t1.micro". Used when creating instances.
"AWS_RSYNC_USER" The user to SSH into the instance when deploying through rsync.
"AWS_SECURITY_GROUP" The security group used for new instances. You'll have to create this one yourself.
"AWS_SSH_USER" The user used to SSH into the instance when setting it up for the first time, after creating it.
"ELASTIC_IP" Assign an AWS Elastic IP to new instances, and release it when terminating them. Defaults to true.
"ENV" Provided as a JSON object. Variables to set in the local environment before the app starts. Useful for setting up DB credentials for example.
"NGINX_ENABLED" Whether to install and use nginx. If installed, the Node application must listen on port NGINX_PROXY_PORT. Keep in mind that since we're going to use pm2 to spin up a cluster, a single port won't be an issue anyways.
"NGINX_PROXY_PORT" This is the port where nginx will proxy requests to, when it won't handle them by itself. This is the same port you'll want to use to listen with your Node application.
"NGINX_SERVER_NAME" The server name for your static server, for example: bevacqua.io.
"NGINX_STATIC_ERRORS" The relative path to your error HTML views folder root. For example bin/views/error.
"NGINX_STATIC_ROOT" The relative path to your static folder root, for example: bin/public. Used to serve static assets through nginx.
"NGINX_USER" The user to configure and run nginx with.
"NGINX_WORKERS" The amount of workers processes used by nginx.
"NODE_SCRIPT" The path to your script. Defaults to app.js, as in node app.js. Relative to your cwd.
"NPM_INSTALL_DISABLED" If true, won't npm install --production after deployments
"NPM_REBUILD" If true, will npm rebuild after deployments
"PAGESPEED_API_KEY" If provided, will run Google PageSpeed insights on every deployment. Get an API Key here. Requires you to setup grunt-pagespeed locally, in your own Gruntfile.js.
"PROJECT_ID" Just an identifier for your project, in case you're hosting multiple ones, for some stupid reason, in the same instance. Defaults to ec2. This is used when creating folders inside the instance.
"RSYNC_EXCLUDE_FROM" Relative path to an rsync exclusion patterns file. These are used to exclude files from being uploaded to the server during rsync on deploys. Defaults to ignoring .git and node_modules.
"RSYNC_EXCLUDES" An array of file patterns to explicitly exclude during deploys. The %NODE_ENV% string will be replaced by the name tag. Unset by default.
"RSYNC_INCLUDE_FROM" Relative path to an rsync inclusion patterns file. These are used to include files for upload to the server during rsync on deploys. Unset by default.
"RSYNC_INCLUDES" An array of file patterns to explicitly include during deploys. The %NODE_ENV% string will be replaced by the name tag. Useful for uploading environment configuration.
"SSH_KEYS_FOLDER" The relative path to a folder where you want to use with tasks that create SSH key-pairs. It doesn't need to exist, mkdir -p will take care of that. This defaults to a folder inside this package, which is pretty lame if you want to look at the key-pairs yourself. Although you shouldn't need to, I've got you covered.
"SSL_CERTIFICATE" Relative path to your unified SSL certificate.
"SSL_CERTIFICATE_KEY" Relative path to your private certificate key.
"SSL_ENABLED" Enables SSL configuration on nginx. Learn how to set it up for free.
"SSL_STRICT" Whether to send a Strict-Transport-Security header.
"VERBOSITY_NPM" Determines the output verbosity for npm during deployments, values are limited to loglevel option values for npm. Defaults to info, just like npm does.
"VERBOSITY_RSYNC" Determines the output verbosity for rsync. Possible values limited to 'v', 'vv', and 'vvv'. Defaults to '' (not verbose at all, my friend).
"PM2_INSTANCES_COUNT" Set number processes to start with pm2. default is 2 and valid values are integers or 'max' for setting the maximum processes depending on avaible CPUs

Tasks

Although this package exposes quite a few different tasks, here are the ones you'll want to be using directly.

Launch an EC2 instance ec2-launch:name

Launches an instance and sets it up.

  • Creates an SSH key-pair
  • Uploads the public key to AWS
  • Creates an AWS EC2 instance
  • Tags the instance with the friendly name you provided
  • Pings the instance until it warms up a DNS and provides SSH access (typically takes a minute)
  • Sets up the instance, installing Node, npm, and pm2

Example:

grunt ec2-launch:teddy

ec2-launch.png

Shutdown an EC2 instance ec2-shutdown:name

Terminates an instance and deletes related objects

  • Looks up the id of an instance tagged name
  • Terminates the AWS EC2 instance
  • Deletes the key-pair associated with the instance

Example:

grunt ec2-shutdown:teddy

ec2-shutdown.png

List running EC2 instances ec2-list-json

Returns a JSON list of running EC2 instances. Defaults to filtering by running state. You can use ec2-list-json:all to remove the filter, or pick another instance-state-name to filter by.

ec2-list.png

Describe an instance with ec2-lookup

Similar to ec2-list-json, but lets you get the properties of an instance by name, rather than state. Try it with grunt ec2-lookup:staging.

Get an SSH connection command for an instance ec2-ssh-text:name

Gives you a command you can copy and paste to connect to an EC2 instance through SSH. Useful to get down and dirty.

grunt ec2-ssh-text:teddy

ec2-ssh.png

Deploy to an EC2 instance ec2-deploy

Deploys to a running EC2 instance using rsync over SSH.

  • Connects to the instance through SSH
  • Uploads cwd to an rsync folder such as /srv/rsync/example/latest
  • Only transmits changes, similar to how git operates
  • Using pkg.version, creates a folder with the newest version, like /srv/apps/example/v/0.6.5
  • Creates a link from /srv/apps/example/v/0.6.5 to /srv/apps/example/current
  • Either starts the application, or reloads it with zero downtime, using pm2
  • Instance name can be accessed through process.env.NODE_ENV

Example:

grunt ec2-deploy:teddy

ec2-deploy.png

Deploy to multiple EC2 instances ec2-deploy-many

Queries EC2 for instances that match the given name and deploys to each on using ec2-deploy.

Example:

grunt ec2-deploy-many:teddy*

Reboot an instance with ec2-reboot

Reboots the instance by the specified name.

  • Looks up instance id for instance tagged name.
  • Reboots it

Example:

grunt ec2-reboot:teddy

Complete Task Reference

Task and Target(s) Purpose
ec2-assign-address:id Allocates an IP and assigns it to your instance
ec2-assign-existing-address:id:ip Assigns an IP address to an instance without allocating a new one
ec2-create-keypair:name Generates an RSA key pair and uploads the public key to AWS
ec2-create-tag:id:name Tags an instance with the provided name
ec2-delete-keypair:name Removes the remote and the local copies of the RSA key
ec2-delete-tag:id Deletes the associated name tag for an instance
ec2-rename-tag:old:replacement Tags an instance using a different name
ec2-deploy:name Deploys to the instance using rsync, reloads pm2 and nginx
ec2-deploy-many:name Gets instances filtered by name tag and deploys to the instance using rsync, reloads pm2 and nginx
ec2-elb-attach:instance-name:elb-name? Attaches an instance to an ELB
ec2-elb-detach:instance-name:elb-name? Detaches an instance from an ELB
ec2-launch:name Creates a new instance, giving it a key-pair, a name tag, and an IP. Then sets it up
ec2-list:state Lists instances filtered by state. Defaults to running filter, use all to disable filter.
ec2-list-json:state Lists instances filtered by state. Defaults to running filter, use all to disable filter. Prints results in JSON
ec2-logs-nginx-access:name Gets nginx access logs
ec2-logs-nginx-error:name Gets nginx error logs
ec2-logs-node:name Gets pm2 logs
ec2-lookup:name Gets instance filtered by name tag
ec2-lookup-json:name Gets instance filtered by name tag. Prints results in JSON
ec2-nginx-configure:name Installs nginx if necessary, updates its configuration files
ec2-nginx-reload:name Reloads nginx
ec2-nginx-restart:name Restarts nginx
ec2-nginx-start:name Starts nginx
ec2-nginx-stop:name Stops nginx
ec2-node-list:name Returns output for pm2 list
ec2-node-monit:name Runs pm2 monit
ec2-node-reload:name Reloads app using pm2 reload all
ec2-node-restart:name Restarts app using pm2 restart all
ec2-node-start:name Starts app using parameterized pm2 start
ec2-node-stop:name Stops app using pm2 stop all
ec2-pagespeed:ip Requests the Google PageSpeed API, prints insights
ec2-pm2-update:name Updates pm2 on an instance, using npm update -g pm2
ec2-reboot:name Reboots the EC2 instance
ec2-release-address:ip Releases an IP address
ec2-run-instance:name Spins up an EC2 instance, gives a name tag and assigns an IP
ec2-setup:name Sets up port forwarding, installs rsync, node, and pm2, enqueues ec2-nginx-configure
ec2-shutdown:name Terminates an instance, deleting its associated key-pair, IP address, and name tag
ec2-ssh:name Establishes an ssh connection to the instance, you can emit commands to your EC2 instance
ec2-ssh-text:name Displays a verbose command with which you can establish an ssh connection to the instance
ec2-terminate-instance:id Terminates an instance
ec2-version:name Get the version number currently deployed to EC2
ec2-wait:id Waits for an instance to report a public DNS and be accessible through ssh

Feedback

Enjoy it. Submit any issues you encounter, and send any feedback you might have my way.

More Repositories

1

dragula

๐Ÿ‘Œ Drag and drop so simple it hurts
JavaScript
21,936
star
2

es6

๐ŸŒŸ ES6 Overview in 350 Bullet Points
4,328
star
3

rome

๐Ÿ“† Customizable date (and time) picker. Opt-in UI, no jQuery!
JavaScript
2,913
star
4

js

๐ŸŽจ A JavaScript Quality Guide
2,874
star
5

fuzzysearch

๐Ÿ”ฎ Tiny and blazing-fast fuzzy search in JavaScript
JavaScript
2,711
star
6

woofmark

๐Ÿ• Barking up the DOM tree. A modular, progressive, and beautiful Markdown and HTML editor
JavaScript
1,624
star
7

promisees

๐Ÿ“จ Promise visualization playground for the adventurous
JavaScript
1,199
star
8

horsey

๐Ÿด Progressive and customizable autocomplete component
JavaScript
1,167
star
9

css

๐ŸŽจ CSS: The Good Parts
992
star
10

react-dragula

๐Ÿ‘Œ Drag and drop so simple it hurts
JavaScript
992
star
11

contra

๐Ÿ„ Asynchronous flow control with a functional taste to it
JavaScript
771
star
12

shots

๐Ÿ”ซ pull down the entire Internet into a single animated gif.
JavaScript
728
star
13

insignia

๐Ÿ”– Customizable tag input. Progressive. No non-sense!
JavaScript
674
star
14

campaign

๐Ÿ’Œ Compose responsive email templates easily, fill them with models, and send them out.
JavaScript
641
star
15

perfschool

๐ŸŒŠ Navigate the #perfmatters salt marsh waters in this NodeSchool workshopper
CSS
630
star
16

local-storage

๐Ÿ›… A simplified localStorage API that just works
JavaScript
523
star
17

angularjs-dragula

๐Ÿ‘Œ Drag and drop so simple it hurts
HTML
509
star
18

reads

๐Ÿ“š A list of physical books I own and read
486
star
19

insane

๐Ÿ˜พ Lean and configurable whitelist-oriented HTML sanitizer
JavaScript
449
star
20

hget

๐Ÿ‘ Render websites in plain text from your terminal
HTML
334
star
21

hit-that

โœŠ Render beautiful pixel perfect representations of websites in your terminal
JavaScript
332
star
22

hash-sum

๐ŸŽŠ Blazing fast unique hash generator
JavaScript
301
star
23

swivel

Message passing between ServiceWorker and pages made simple
JavaScript
294
star
24

dominus

๐Ÿ’‰ Lean DOM Manipulation
JavaScript
277
star
25

trunc-html

๐Ÿ“ truncate html by text length
JavaScript
220
star
26

beautify-text

โœ’๏ธ Automated typographic quotation and punctuation marks
JavaScript
186
star
27

sixflix

๐ŸŽฌ Detects whether a host environment supports ES6. Algorithm by Netflix.
JavaScript
175
star
28

twitter-for-github

๐Ÿฅ Twitter handles for GitHub
JavaScript
146
star
29

awesome-badges

๐Ÿ† Awesome, badges!
JavaScript
124
star
30

prop-tc39

Scraping microservice for TC39 proposals ๐Ÿ˜ธ
JavaScript
108
star
31

megamark

๐Ÿ˜ป Markdown with easy tokenization, a fast highlighter, and a lean HTML sanitizer
JavaScript
104
star
32

diferente

User-friendly virtual DOM diffing
JavaScript
95
star
33

domador

๐Ÿ˜ผ Dependency-free and lean DOM parser that outputs Markdown
JavaScript
86
star
34

proposal-undefined-coalescing-operator

Undefined Coalescing Operator proposal for ECMAScript
77
star
35

dotfiles

๐Ÿ’  Yay! @bevacqua does dotfiles \o/
Shell
75
star
36

assignment

๐Ÿ˜ฟ Assign property objects onto other objects, recursively
JavaScript
73
star
37

sektor

๐Ÿ“ A slim alternative to jQuery's Sizzle
JavaScript
65
star
38

map-tag

๐Ÿท Map template literal expression interpolations with ease.
JavaScript
65
star
39

unbox

Unbox a node application with a well-designed build-oriented approach in minutes
JavaScript
61
star
40

hint

Awesome tooltips at your fingertips
JavaScript
60
star
41

but

๐Ÿ›ฐ But expands your functional horizons to the edge of the universe
JavaScript
59
star
42

kanye

Smash your keyboards with ease
JavaScript
55
star
43

correcthorse

See XKCD for reference
JavaScript
52
star
44

easymap

๐Ÿ—บ simplified use of Google Maps API to render a bunch of markers.
JavaScript
52
star
45

flickr-cats

A demo page using the Flickr API, ServiceWorker, and plain JavaScript
HTML
49
star
46

hubby

๐Ÿ‘จ Hubby is a lowly attempt to describe public GitHub activity in natural language
JavaScript
46
star
47

ruta3

Route matcher devised for shared rendering JavaScript applications
JavaScript
45
star
48

poser

๐Ÿ“ฏ Create clean arrays, or anything else, which you can safely extend
JavaScript
45
star
49

baal

๐Ÿณ Automated, autoscaled, zero-downtime, immutable deployments using plain old bash, Packer, nginx, Node.js, and AWS. Made easy.
Shell
44
star
50

lipstick

๐Ÿ’„ sticky sessions for Node.js clustering done responsibly
JavaScript
43
star
51

crossvent

๐ŸŒ Cross-platform browser event handling
JavaScript
41
star
52

lazyjs

The minimalist JavaScript loader
JavaScript
39
star
53

spritesmith-cli

๐Ÿ˜ณ Adds a CLI to the spritesmith module
JavaScript
38
star
54

gulp-jsfuck

Fuck JavaScript and obfuscate it using only 6 characters ()+[]!
JavaScript
37
star
55

measly

A measly wrapper around XHR to help you contain your requests
JavaScript
36
star
56

keynote-extractor

๐ŸŽ Extract Keynote presentations to JSON and Markdown using a simple script.
AppleScript
35
star
57

hyperterm-working-directory

๐Ÿ–ฅ๐Ÿ‘ท๐Ÿ“‚ Adds a default working directory setting. Opens new tabs using that working directory.
JavaScript
34
star
58

gitcanvas

๐Ÿ› Use your GitHub account's commit history as a canvas. Express the artist in you!
JavaScript
34
star
59

cave

Remove critical CSS from your stylesheet after inlining it in your pages
JavaScript
33
star
60

scrape-metadata

๐Ÿ“œ HTML metadata scraper
JavaScript
31
star
61

feeds

๐ŸŽ RSS feeds I follow and maintain
31
star
62

suchjs

Provides essential jQuery-like methods for your evergreen browser, in under 200 lines of code. Such small.
JavaScript
30
star
63

ponyedit

An interface between contentEditable and your UI
JavaScript
29
star
64

grunt-grunt

Spawn Grunt tasks in other Gruntfiles easily from a Grunt task
JavaScript
29
star
65

ultramarked

Marked with built-in syntax highlighting and input sanitizing that doesn't encode all HTML.
JavaScript
28
star
66

sell

๐Ÿ’ฐ Cross-browser text input selection made simple
JavaScript
28
star
67

icons

Free icon sets gathered around the open web
27
star
68

insert-rule

Insert rules into a stylesheet programatically with a simple API
JavaScript
26
star
69

hose

Redirect any domain to localhost for convenience or productivity!
JavaScript
26
star
70

ponymark

Next-generation PageDown fork
JavaScript
25
star
71

omnibox

Fast url parsing with a tiny footprint and extensive browser support
JavaScript
25
star
72

estimate

Calculate remaining reading time estimates in real-time
JavaScript
24
star
73

node-emoji-random

Creates a random emoji string. This is as useless as it gets.
JavaScript
24
star
74

sluggish

๐Ÿ Sluggish slug generator that works universally
JavaScript
24
star
75

seleccion

๐Ÿ’ต A getSelection polyfill and a setSelection ranch dressing
JavaScript
24
star
76

bullseye

๐ŸŽฏ Attach elements onto their target
JavaScript
23
star
77

vectorcam

๐ŸŽฅ Record gifs out of <svg> elements painlessly
JavaScript
22
star
78

paqui

Dead simple, packager-agnostic package management solution for front-end component developers
JavaScript
22
star
79

grunt-ngdoc

Grunt task for generating documentation using AngularJS' @ngdoc comments
JavaScript
20
star
80

ftco

โšก Browser extension that unshortens t.co links in TweetDeck and Twitter
JavaScript
19
star
81

jadum

๐Ÿ’ A lean Jade compiler that understands Browserify and reuses partials
JavaScript
19
star
82

trunc-text

๐Ÿ“ truncate text by length, doesn't cut words
JavaScript
16
star
83

flexarea

Pretty flexible areas!
JavaScript
16
star
84

music-manager

๐Ÿ“ป Manages a list of favorite artists and opens playlists on youtube.
JavaScript
16
star
85

grunt-integration

Run Integration Tests using Selenium, Mocha, a Server, and a Browser
JavaScript
15
star
86

apartment

๐Ÿก Remove undesirable properties from a piece of css
JavaScript
14
star
87

mongotape

Run integration tests using mongoose and tape
JavaScript
14
star
88

rehearsal

Persist standard input to a file, then simulate real-time program execution.
JavaScript
13
star
89

queso

Turn a plain object into a query string
JavaScript
13
star
90

bitfin

๐Ÿฆ Finance utility for Bitstamp
JavaScript
13
star
91

pandora-box

๐Ÿผ What will it be?
JavaScript
12
star
92

grunt-spriting-example

An example on how to seamlessly use spritesheets with Grunt.
12
star
93

artists

๐ŸŽค Big list of artists pulled from Wikipedia.
JavaScript
11
star
94

twitter-leads

๐Ÿฆ Pull list of leads from a Twitter Ads Lead Generation Card
JavaScript
11
star
95

reaver

Minimal asset hashing CLI and API
JavaScript
11
star
96

atoa

Creates a true array based on `arraylike`, starting at `startIndex`.
JavaScript
10
star
97

BridgeStack

.NET StackExchange API v2.0 client library wrapper
C#
10
star
98

ama

๐Ÿ“– A repository to ask @bevacqua anything.
10
star
99

virtual-host

Create virtual, self-contained `connect` or `express` applications using a very simple API.
JavaScript
10
star
100

banksy

๐ŸŒ‡ Street art between woofmark and horsey
JavaScript
10
star