Skip to content

Commit

Permalink
Switch to cloudformation native YAML support
Browse files Browse the repository at this point in the history
CloudFormation just added support for YAML, with some nice helper tags.
In order to keep parsing the YAML and using our !minify tag, we would have
had to implement all of the CF tags, which didn't sound fun, so now the
minimized code is injected directly into the YAML string in the build step.
  • Loading branch information
zwily committed Sep 20, 2016
1 parent be71a63 commit baff688
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 273 deletions.
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ First, create the CloudFormation stack. Inside a clone of this project:
```bash
aws --region us-east-1 cloudformation create-stack \
--stack-name sqs-to-lambda \
--template-body file://cloudformation.json \
--template-body file://sqs-to-lambda.yml \
--parameters 'ParameterKey=QueueToFunctionMapping,ParameterValue="<queue url 1>,<function 1>,<queue url 2>,<function 2>,..."' \
--capabilities CAPABILITY_IAM
```
Expand Down Expand Up @@ -70,23 +70,20 @@ runs for almost 5 minutes, constantly polling.
If you choose 1Minute frequency, the function is invoked every minute, but exits
once the queue returns 0 items to process.

### What's up with the `cloudformation.yml` file?

Yaml is a much easier format to write these in. Trust me.

### What's up with the Javascript in `cloudformation.json`?
### What's up with the Javascript in `sqs-to-lambda.yml`?

Embedding Javascript in the CloudFormation template is an easy way to make the
template self-contained. It also allows us to dynamically inject the configuration
into the SQS poller function. The javascript in the `cloudformation.json` file
into the SQS poller function. The javascript in the `sqs-to-lambda.yml` file
is minimized from `sqs-to-lambda.js`.

We minimize the Javascript because there is a 2KB limit to inline code in
CloudFormation templates.

## Building

The `cloudformation.json` file is a build artifact - don't edit it directly!
The `sqs-to-lambda.yml` file is a build artifact - don't edit it directly!
The original is in `src/cloudformation.yml`
To build:

```
Expand All @@ -96,5 +93,5 @@ $ npm run build

## Contributing

Make your changes, build a new `cloudformation.json` file (see above), and
Make your changes, build a new `sqs-to-lambda.yml` file (see above), and
submit a pull request.
23 changes: 6 additions & 17 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
var yaml = require('js-yaml');
var UglifyJS = require("uglify-js");
var fs = require('fs');

var MinifyYamlType = new yaml.Type('!minify', {
kind: 'scalar',
construct: function(data) {
return UglifyJS.minify(data, {
mangle: {
'toplevel': true
}
}).code;
}
});
var sqsToLambdaCode = UglifyJS.minify('./src/sqs-to-lambda.js', {
mangle: { 'toplevel': true }
}).code;

var SCHEMA = yaml.Schema.create([ MinifyYamlType ]);

var templateBody = fs.readFileSync('./cloudformation.yml', 'utf-8');
var template = yaml.load(templateBody, { schema: SCHEMA });

fs.writeFileSync('./cloudformation.json', JSON.stringify(template, null, ' '), 'utf-8');
var templateBody = fs.readFileSync('./src/cloudformation.yml', 'utf-8');
var builtTemplate = templateBody.replace('{SQSToLambdaCode}', sqsToLambdaCode);
fs.writeFileSync('./sqs-to-lambda.yml', builtTemplate);
203 changes: 0 additions & 203 deletions cloudformation.json

This file was deleted.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"build": "node build.js"
},
"dependencies": {
"js-yaml": "^3.5.4",
"uglify-js": "^2.6.2"
}
}
Loading

0 comments on commit baff688

Please sign in to comment.