-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
177 lines (154 loc) · 3.89 KB
/
create.js
File metadata and controls
177 lines (154 loc) · 3.89 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
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
const AWS = require('aws-sdk');
const awsRegion = 'us-east-1';
AWS.config.update({region: awsRegion});
const ec2 = new AWS.EC2({apiVersion: '2016-11-15'});
const elb = new AWS.ELB({apiVersion: '2012-06-01'});
const autoScaling = new AWS.AutoScaling({apiVersion: '2011-01-01'});
const launchName = 'simple-launch-configuration';
const elbName = 'simple-elb';
encodeUserData = function(userDataStr) {
return new Buffer(userDataStr).toString('base64');
};
const userDataStr = `#!/bin/bash
echo 'test'
sudo yum update -y
sudo yum install git -y
mkdir /app
chmod 755 /app
cd /app
git clone https://github.com/rp4fx12/hello-world.git
curl -sL https://rpm.nodesource.com/setup_6.x | sudo -E bash -
sudo yum install nodejs --enablerepo=nodesource -y
node --version > nodeVersion.txt
cd /app/hello-world
npm install
npm start
`
const elbParams = {
Listeners: [
{
InstancePort: 3000,
InstanceProtocol: 'HTTP',
LoadBalancerPort: 80,
Protocol: 'HTTP'
}
],
LoadBalancerName: elbName,
SecurityGroups: [
'sg-ae5a19d4'
],
Subnets: [
'subnet-e29f62cf',
'subnet-7470dd3d'
],
Tags: [
{
Key: 'State',
Value: 'Green'
}
]
};
const healthCheckParams = {
HealthCheck: {
HealthyThreshold: 2,
Interval: 30,
Target: 'HTTP:3000/',
Timeout: 5,
UnhealthyThreshold: 2
},
LoadBalancerName: elbName
};
// const cloudAlarmParams = {
// AlarmName: 'Cloud-Green-Test',
// ComparisonOperator: GreaterThanOrEqualToThreshold,
// EvaluationPeriods: 20,
// MetricName: 'HealthyHostCount',
// NameSpace: 'AWS/ELB',
// Period: 30,
// Threshold: 1.0,
// ActionsEnabled: true,
// OKActions: [
// ]
// }
// const cloudEventParams = {
// Name: 'Trigger Lambda after 20 minutes',
// Description: 'Triggers Lambda after 20 minutes if instance is not healthy',
// ScheduleExpression: 'rate(20 minutes)',
// State: 'ENABLED'
// }
const launchParams = {
LaunchConfigurationName: launchName,
IamInstanceProfile: 'EC2Access',
ImageId: 'ami-a4c7edb2',
InstanceType: 't2.micro',
SecurityGroups: [
'sg-ae5a19d4'
],
KeyName: 'HelloWorld',
UserData: encodeUserData(userDataStr)
};
const autoScaleParams = {
AutoScalingGroupName: 'simple-auto-scaling',
AvailabilityZones: [
'us-east-1a',
'us-east-1b',
],
HealthCheckGracePeriod: 100,
HealthCheckType: 'EC2',
LaunchConfigurationName: launchName,
MaxSize: 1,
MinSize: 1,
LoadBalancerNames: [
elbName
],
Tags: [
{
Key: 'Name',
Value: 'simple-ec2-green'
},
]
}
createELB = function() {
return new Promise(function (resolve, reject) {
elb.createLoadBalancer(elbParams, function(err, data) {
if (err) return reject(err);
else return resolve(data);
});
});
};
addHealthCheck = function() {
return new Promise(function (resolve, reject) {
elb.configureHealthCheck(healthCheckParams, function(err, data) {
if (err) return reject(err);
else return resolve(data);
});
});
};
createLaunchConf = function() {
return new Promise(function (resolve, reject) {
autoScaling.createLaunchConfiguration(launchParams, function(err, data) {
if (err) return reject(err);
else return resolve(data);
});
});
};
createAutoScalingGroup = function() {
return new Promise(function (resolve, reject) {
autoScaling.createAutoScalingGroup(autoScaleParams, function(err, data) {
if (err) return reject(err);
else return resolve(data);
});
});
};
// const elbLaunchConfPromise = Promise.all([createELB(), createLaunchConf()]);
// elbLaunchConfPromise.then(addHealthCheck)
// .then( function (data) {
// createAutoScalingGroup().then( function(data){
// console.log(data);
// }, function(err) {
// console.log(err);
// })
// }, function (err) {
// console.log(err);
// });
createAutoScalingGroup();