Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Apr 5, 2016
0 parents commit 8163909
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
@@ -0,0 +1 @@
* text=auto
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
node_modules
.nyc_output
8 changes: 8 additions & 0 deletions .travis.yml
@@ -0,0 +1,8 @@
language: node_js
node_js:
- '5'
- '4'
- '0.12'
- '0.10'
after_script:
- npm run coveralls
47 changes: 47 additions & 0 deletions index.js
@@ -0,0 +1,47 @@
'use strict';
var path = require('path');
var AWS = require('aws-sdk');
var Promise = require('pinkie-promise');
var utils = require('./lib/utils');

module.exports = function (name, alias, opts) {
if (typeof name !== 'string') {
return Promise.reject(new Error('Provide a AWS Lambda function name.'));
}

if (typeof alias !== 'string') {
return Promise.reject(new Error('Provide an alias name.'));
}

opts = opts || {};

// Load the credentials
AWS.config.region = opts.awsRegion || 'us-west-1';

if (opts.awsProfile) {
// Set the `credentials` property if a profile is provided
var objectCredentials = {
profile: opts.awsProfile
};

if (opts.awsFilename) {
objectCredentials.filename = path.resolve(process.cwd(), opts.awsFilename);
}

AWS.config.credentials = new AWS.SharedIniFileCredentials(objectCredentials);
}

// Create a lambda object
var lambda = new AWS.Lambda();

var options = {
FunctionName: name,
Name: alias
};

if (opts.version) {
options.FunctionVersion = opts.version;
}

return utils.updateOrCreate(lambda, options);
};
50 changes: 50 additions & 0 deletions lib/utils.js
@@ -0,0 +1,50 @@
'use strict';
var pify = require('pify');
var objectAssign = require('object-assign');
var Promise = require('pinkie-promise');

function findLatestVersion(lambda, opts) {
if (opts.FunctionVersion) {
// Return the function version if it was provided
return Promise.resolve(opts.FunctionVersion);
}

return pify(lambda.listVersionsByFunction.bind(lambda), Promise)({FunctionName: opts.FunctionName})
.then(function (data) {
if (!data.Versions || data.Versions.length === 0) {
throw new Error('No versions found.');
}

// Sort all the versions
data.Versions.sort(function (a, b) {
var aVersion = a.Version === '$LATEST' ? 0 : parseInt(a.Version, 10);
var bVersion = b.Version === '$LATEST' ? 0 : parseInt(b.Version, 10);

return bVersion - aVersion;
});

return data.Versions[0].Version;
});
}

function updateOrCreate(lambda, opts) {
var options = objectAssign({}, opts);

return findLatestVersion(lambda, opts)
.then(function (version) {
options.FunctionVersion = version;

// Try to update the version first
return pify(lambda.updateAlias.bind(lambda), Promise)(options);
})
.catch(function (err) {
if (err.code === 'ResourceNotFoundException') {
// If the alias does not exist yet, create it
return pify(lambda.createAlias.bind(lambda), Promise)(options);
}

throw err;
});
}

exports.updateOrCreate = updateOrCreate;
21 changes: 21 additions & 0 deletions license
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) Sam Verschueren <sam.verschueren@gmail.com> (github.com/SamVerschueren)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
43 changes: 43 additions & 0 deletions package.json
@@ -0,0 +1,43 @@
{
"name": "lambda-update-alias",
"version": "0.0.0",
"description": "Update or create a AWS lambda alias",
"license": "MIT",
"repository": "SamVerschueren/lambda-update-alias",
"author": {
"name": "Sam Verschueren",
"email": "sam.verschueren@gmail.com",
"url": "github.com/SamVerschueren"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "xo && nyc ava",
"coveralls": "nyc report --reporter=text-lcov | coveralls"
},
"files": [
"index.js",
"lib"
],
"keywords": [
"aws",
"lambda",
"alias",
"update",
"create"
],
"dependencies": {
"aws-sdk": "^2.3.0",
"object-assign": "^4.0.1",
"pify": "^2.3.0",
"pinkie-promise": "^2.0.0"
},
"devDependencies": {
"ava": "*",
"coveralls": "^2.11.9",
"nyc": "^6.1.1",
"sinon": "^1.17.3",
"xo": "*"
}
}
118 changes: 118 additions & 0 deletions readme.md
@@ -0,0 +1,118 @@
# lambda-update-alias

[![Build Status](https://travis-ci.org/SamVerschueren/lambda-update-alias.svg?branch=master)](https://travis-ci.org/SamVerschueren/lambda-update-alias)
[![Coverage Status](https://coveralls.io/repos/github/SamVerschueren/lambda-update-alias/badge.svg?branch=master)](https://coveralls.io/github/SamVerschueren/lambda-update-alias?branch=master)

> Update or create a AWS lambda alias

## Install

```
$ npm install --save lambda-update-alias
```


## Usage

```js
const updateAlias = require('lambda-update-alias');

updateAlias('myLambdaFunction', 'v1'}).then(result => {
console.log(result);
/*
{
AliasArn: 'arn:aws:lambda:us-west-1:123456789012:function:myLambdaFunction:v1',
Name: 'v1',
FunctionVersion: '3',
Description: 'My lambda function description'
}
*/
});
```


## API

### updateAlias(name, alias, [options])

Returns a promise for the result object.

#### name

Type: `string`

Name of the lambda function.

#### alias

Type: `string`

Name of the alias that should be attached to the lambda function.

#### options

##### version

Type: `string`<br>
Default: *`latest`*

Name of the version where the alias should be attached to. If not provided, the alias will be attached to the version
with the highest number. `$LATEST` is treated as version `0`.

##### awsProfile

Type: `string`

[AWS Profile](http://docs.aws.amazon.com/AWSJavaScriptSDK/guide/node-configuring.html). The user related to the profile should have
admin access to API Gateway and should be able to invoke `lambda:AddPermission`.

Can be overridden globally with the `AWS_PROFILE` environment variable.

##### awsFilename

Type: `string`

[Filename](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SharedIniFileCredentials.html#constructor-property) to use when loading credentials.

##### awsRegion

Type: `string`<br>
Default: `us-west-1`

AWS region.


## User Policy

The profile creating or updating the alias should be able to list the versions of the function and create and update the aliases.

```json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1454507191000",
"Effect": "Allow",
"Action": [
"lambda:CreateAlias",
"lambda:ListVersionsByFunction",
"lambda:UpdateAlias"
],
"Resource": [
"*"
]
}
]
}
```


## Related

- [lambda-update-alias-cli](https://github.com/SamVerschueren/lambda-update-alias-cli) - CLI for this module


## License

MIT © [Sam Verschueren](https://github.com/SamVerschueren)
13 changes: 13 additions & 0 deletions test/fixtures/aws.js
@@ -0,0 +1,13 @@
'use strict';
var AWS = require('aws-sdk');

var lambda = new AWS.Lambda();

AWS.Lambda = function () {
return lambda;
};

module.exports = {
lambda: lambda,
config: AWS.config
};
55 changes: 55 additions & 0 deletions test/test.js
@@ -0,0 +1,55 @@
import path from 'path';
import test from 'ava';
import sinon from 'sinon';
import aws from './fixtures/aws';
import utils from '../lib/utils';
import m from '../';

test.before(() => {
sinon.stub(utils, 'updateOrCreate');
});

test('throw error if no lambda function name is provided', t => {
t.throws(m(), 'Provide a AWS Lambda function name.');
});

test('throw error if no alias name is provided', t => {
t.throws(m('foo'), 'Provide an alias name.');
});

test('specify the aws profile', async t => {
await m('foo', 'v1', {awsProfile: 'foo-profile'});
t.is(aws.config.credentials.profile, 'foo-profile');
});

test('specify the aws filename', async t => {
await m('foo', 'v1', {awsProfile: 'foo-profile', awsFilename: './credentials'});
t.is(aws.config.credentials.filename, path.resolve(process.cwd(), 'credentials'));
});

test.serial('use `us-west-1` as default region', async t => {
await m('foo', 'v1');
t.is(aws.config.region, 'us-west-1');
});

test.serial('provide region property', async t => {
await m('foo', 'v1', {awsRegion: 'eu-west-1'});
t.is(aws.config.region, 'eu-west-1');
});

test.serial('update or create the alias', async t => {
await m('foo', 'v1');
t.same(utils.updateOrCreate.lastCall.args[1], {
FunctionName: 'foo',
Name: 'v1'
});
});

test.serial('update or create the alias on a specific version', async t => {
await m('foo', 'v1', {version: '1'});
t.same(utils.updateOrCreate.lastCall.args[1], {
FunctionName: 'foo',
FunctionVersion: '1',
Name: 'v1'
});
});

0 comments on commit 8163909

Please sign in to comment.