-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeployment.js
More file actions
147 lines (132 loc) · 3.98 KB
/
Copy pathdeployment.js
File metadata and controls
147 lines (132 loc) · 3.98 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
var AWS = require('aws-sdk');
// Load credentials and set region from JSON file
AWS.config.loadFromPath('./config-wasif.json');
var userData64 = new Buffer(`
#!/bin/bash
set -e -x
curl --silent --location https://rpm.nodesource.com/setup_9.x | bash -
sudo yum install -y git nodejs
git clone https://github.com/Vaaceph/BackEndCloud.git && cd BackEndCloud
sudo npm install
DEBUG=express:* node server.js
`).toString('base64');
var autoscaling = new AWS.AutoScaling();
var cloudwatch = new AWS.CloudWatch();
var launchConfiguration = {
IamInstanceProfile: "arn:aws:iam::491934600746:instance-profile/S3Access",
ImageId: "ami-a0cfeed8",
InstanceType: "t2.micro",
LaunchConfigurationName: "sdk-launch-config",
SecurityGroups: ['launch-wizard-1'],
UserData: userData64,
KeyName: 'wasif'
};
var AutoScalingGroup = {
AutoScalingGroupName: "autoScalingSDK",
AvailabilityZones: [
"us-west-2c",
"us-west-2b"
],
HealthCheckGracePeriod: 120,
HealthCheckType: "ELB",
LaunchConfigurationName: "sdk-launch-config",
LoadBalancerNames: [
"sdk-load-balancer"
],
MaxSize: 3,
MinSize: 1
};
var scaleOut = {
AdjustmentType: "ChangeInCapacity",
AutoScalingGroupName: "autoScalingSDK",
PolicyName: "ScaleOut",
ScalingAdjustment: 1,
Cooldown: 30
};
var scaleIn = {
AdjustmentType: "ChangeInCapacity",
AutoScalingGroupName: "autoScalingSDK",
PolicyName: "ScaleIn",
ScalingAdjustment: -1,
Cooldown: 30
};
var data1;
var data2;
function launchConfigurationCallBack(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
autoscaling.createAutoScalingGroup(AutoScalingGroup, AutoScalingGroupCreation);
}
}
function scaleOutPolicyCallBack(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
data1 = data.PolicyARN;
var scaleOut = {
AlarmName: 'ScaleOut', /* required */
ComparisonOperator: 'GreaterThanOrEqualToThreshold', /* required */
EvaluationPeriods: 1, /* required */
MetricName: 'RequestCount', /* required */
Namespace: 'AWS/ELB', /* required */
Period: 60, /* required */
Threshold: 7.0, /* required */
AlarmDescription: 'This will be used to scale out',
DatapointsToAlarm: 1,
Dimensions: [
{
Name: 'LoadBalancerName', /* required */
Value: 'sdk-load-balancer' /* required */
},
/* more items */
],
AlarmActions: [data1],
Statistic: 'Sum'
};
cloudwatch.putMetricAlarm(scaleOut, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
}
function scaleInPolicyCallBack(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
data2 = data.PolicyARN;
var scaleIn = {
AlarmName: 'ScaleIn', /* required */
ComparisonOperator: 'LessThanOrEqualToThreshold', /* required */
EvaluationPeriods: 1, /* required */
MetricName: 'RequestCount', /* required */
Namespace: 'AWS/ELB', /* required */
Period: 60, /* required */
Threshold: 2.0, /* required */
AlarmDescription: 'This will be used to scale in',
DatapointsToAlarm: 1,
Dimensions: [
{
Name: 'LoadBalancerName', /* required */
Value: 'sdk-load-balancer' /* required */
},
/* more items */
],
AlarmActions: [data2],
Statistic: 'Minimum'
};
cloudwatch.putMetricAlarm(scaleIn, function (err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
}
}
function AutoScalingGroupCreation(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
autoscaling.putScalingPolicy(scaleOut, scaleOutPolicyCallBack);
autoscaling.putScalingPolicy(scaleIn, scaleInPolicyCallBack);
}
}
autoscaling.createLaunchConfiguration(launchConfiguration, launchConfigurationCallBack);