-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (152 loc) · 8.12 KB
/
index.js
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
# ***********************************************************************************************************************************************
# * *
# * @License Starts *
# * *
# * Copyright © 2015 - present. MongoExpUser. All Rights Reserved. *
# * *
# * License: MIT - https://github.com/MongoExpUser/AWS-CloudFormation-Stack-with-AWS-SDK-JS-V3/blob/main/LICENSE *
# * *
# * @License Ends *
# ***********************************************************************************************************************************************
# * *
# * index.js implements a module for creating/deploying or deleting an AWS ClouddFormation stack with AWS-SDK for JavaScript/Node.JS V3 *
# * * *
# ***********************************************************************************************************************************************
*/
class DeployCloudFormationStack
{
constructor()
{
return null;
}
async uuid4()
{
let timeNow = new Date().getTime();
let uuidValue = 'xxxxxxxx-xxxx-7xxx-kxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(constant)
{
let random = (timeNow + Math.random() *16 ) % 16 | 0;
timeNow = Math.floor(timeNow / 16);
return (constant === 'x' ? random : (random & 0x3| 0x8)).toString(16);
});
return uuidValue;
}
async prettyPrint(value)
{
const util = require('util');
console.log(util.inspect(value, { showHidden: true, colors: true, depth: 4 }));
}
async sendCommand(client, command, commandName)
{
const dcfs = new DeployCloudFormationStack();
try
{
const data = await client.send(command);
dcfs.prettyPrint({ "data" : data } );
}
catch(error)
{
return console.log(error);
}
await dcfs.separator();
console.log(`Successful ${commandName} Command`);
await dcfs.separator();
console.log(`Time is:`, new Date(), `.....`);
await dcfs.separator();
}
async createDeployStack(options, config)
{
// define variables and use some in tags variables defintion
const fs = require('fs');
const dcfs = new DeployCloudFormationStack();
const createResources = config.createResources;
const updateResources = config.updateResources;
const deleteResources = config.deleteResources;
const { CloudFormationClient, CreateStackCommand, DeleteStackCommand, UpdateStackCommand } = require("@aws-sdk/client-cloudformation");
const cloudFormationClient = new CloudFormationClient(options);
if( (createResources === true) || (updateResources === true) )
{
const addSuffix = true;
const suffix = (await dcfs.uuid4()).substring(0,3);
const orgName = config.orgName;
const projectName = config.projectName;
const environment = config.environment;
const regionName = config.regionName;
const preOrPostFix = `${orgName}-${environment}`;
const resoureName = config.resoureName;
const creator = config.creator;
// these tags' key-value pairs will be used if the keys are not specified for the resources on the CloudFormation YAML/JSON file
// otherwise, the key-value pairs specified for the resources on the CloudFormation YAML/JSON file over-ride the key-value pairs below.
const tags = [
{ Key: "region", Value: regionName },
{ Key: "environment", Value: environment },
{ Key: "project", Value: projectName },
{ Key: "creator", Value: creator },
];
const stackName = config.stackName;
const stackTerminationProtection = config.stackTerminationProtection;
const stackTemplateBodyInput = config.stackTemplateBodyFileName;
console.log(stackTemplateBodyInput);
const stackTemplateBody = JSON.parse(JSON.stringify(fs.readFileSync(stackTemplateBodyInput).toString()));
const stackCapabilities = config.stackCapabilities;
const stackOnFailure = config.stackOnFailure;
const stackTimeoutInMinutes = config.stackTimeoutInMinutes;
if(addSuffix === true)
{
tags.push( { Key: "name", Value: `${preOrPostFix}-${resoureName}-${suffix}` } );
}
else if(addSuffix === false)
{
tags.push( { Key: "name", Value: `${preOrPostFix}-${resoureName}` } );
}
let params = {
StackName: stackName,
EnableTerminationProtection: stackTerminationProtection,
TemplateBody: stackTemplateBody,
Capabilities: config.stackCapabilities,
OnFailure: config.stackOnFailure,
TimeoutInMinutes: config.stackTimeoutInMinutes,
Tags: tags
};
let commandName;
let command;
if(createResources === true)
{
commandName = "CreateStack";
command = new CreateStackCommand(params);
}
else if(updateResources === true)
{
commandName = "UpdateStack";
command = new UpdateStackCommand(params);
}
await dcfs.sendCommand(cloudFormationClient, command, commandName);
}
else if(deleteResources === true)
{
const stackName = config.stackName;
const params = { StackName: stackName };
const command = new DeleteStackCommand(params);
const commandName = "DeleteStack";
await dcfs.sendCommand(cloudFormationClient, command, commandName);
}
}
async separator()
{
console.log("-------------------------------------------------------------------");
}
}
(async function main()
{
const fs = require("fs");
const dcfs = new DeployCloudFormationStack();
const inputConfigJsonFilePath = "inputConfig.json";
let inputConfig = JSON.parse(fs.readFileSync(inputConfigJsonFilePath));
const credentialJsonFilePath = inputConfig.credentials;
let credentials = JSON.parse(fs.readFileSync(credentialJsonFilePath));
// let options = { credentials: { accessKeyId : credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey }, region: credentials.region };
let options = { accessKeyId : credentials.accessKeyId, secretAccessKey: credentials.secretAccessKey, region: credentials.region };
const config = JSON.parse(fs.readFileSync(inputConfigJsonFilePath));
await dcfs.createDeployStack(options, config);
}());
// module.exports = { DeployCloudFormationStack };