-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlambda-set-stage.js
More file actions
94 lines (78 loc) · 3.4 KB
/
lambda-set-stage.js
File metadata and controls
94 lines (78 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
const program = require('commander');
const execSync = require('child_process').execSync;
const fs = require('fs');
const path = require('path');
program
.option('-p, --profile <profile>', 'The local profile to use when deploying')
.option('-r, --region <region>', 'The region in which to deploy the function')
.parse(process.argv);
var lambdaspec = program.args[0].trim();
var stage = program.args[1].trim();
var version = program.args[2].trim().replace("$","\$");
//get the actual lambda spec
var lambdaspecFullpath = fs.realpathSync(lambdaspec);
lambdaspec = require(lambdaspecFullpath);
//set the profile object according to the profile and region settings
var profile = program.profile ? `--profile ${program.profile}` : "";
profile += program.region ? ` --region ${program.region}` : "";
//first get the list of aliases that exist
//and determine whether we are updating an existing alias or creating a new one
var create = true;
try {
var aliases = execSync(`aws lambda list-aliases ${profile} --function-name ${lambdaspec.lambdaconfig.FunctionArn}`);
aliases = JSON.parse(aliases);
aliases.Aliases.forEach((alias) => {
//if we find an existing alias with the same name as the stage we are trying
//to update, then we stop iterating over the aliases and will do an alias
//update statement
if (stage == alias.Name) {
create = false;
}
});
} catch (err) {
console.error(`Error retrieving list of aliases from aws for '${lambdaspec.lambdaconfig.FunctionArn}': ${err.message}`);
process.exit(1);
}
if (create) {
try {
var res = execSync(`aws lambda create-alias ${profile} --function-name ${lambdaspec.lambdaconfig.FunctionArn} --name ${stage} --function-version '${version}'`);
} catch (err) {
console.error(`Error creating new stage '${stage}' for '${lambdaspec.lambdaconfig.FunctionArn}': ${err.message}`);
process.exit(1);
}
} else {
try {
var res = execSync(`aws lambda update-alias ${profile} --function-name ${lambdaspec.lambdaconfig.FunctionArn} --name ${stage} --function-version '${version}'`);
} catch (err) {
console.error(`Error updating stage '${stage}' for '${lambdaspec.lambdaconfig.FunctionArn}': ${err.message}`);
process.exit(1);
}
}
//update the version history object with the stage info
var lambdaspecPath = path.dirname(lambdaspecFullpath);
var lambdaspecHistory = `${path.basename(lambdaspecFullpath, '.json')}-history.json`;
var history = JSON.parse(fs.readFileSync(path.join(lambdaspecPath,lambdaspecHistory)));
history.aliases = history.aliases || {};
//if we did a create then simply create a new object for the stage in question and write
//it out
//in the event that we are updating an existing alias and pointing it to a new version
//then we set the current version to the one that was requested and then if the
//the version history doesn't contain the current version requested we push it onto the
//list
if (create) {
history.aliases[stage] = {
current: version,
versions: [version]
};
} else {
history.aliases[stage] = history.aliases[stage] || {current:"", versions:[]};
history.aliases[stage].current = version;
if (!history.aliases[stage].versions.includes(version))
history.aliases[stage].versions.push(version);
}
//now save the history object to file
fs.writeFileSync(path.join(lambdaspecPath, lambdaspecHistory),
JSON.stringify(history, null, 2));
console.log(`Lambda stage '${stage}' set to point to version '${version}'`);
process.exit(0);