-
Notifications
You must be signed in to change notification settings - Fork 2
/
setup.js
executable file
·149 lines (141 loc) · 4.81 KB
/
setup.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
/**
* Configure the required AWS Services:
* - Create the IAM Role MKPBananaTweetRole with access to Comprehend, DynamoDB and CloudWatch logs
* - Create the Lamnda Function MKPBananaTweet and assigne it with the role MKPBananaTweetRole
* - Create the DynamDB table MKPTweets
**/
// Load the AWS SDK for Node.js
var AWS = require('aws-sdk')
function createIAMRole() {
// Ensure IAM MKPBananaTweetRole exists
var iam = new AWS.IAM()
var params = {RoleName: 'MKPBananaTweetRole'}
iam.getRole(params, function (err, data) {
if (!err) {
console.log('IAM Role MKPBananaTweetRole exists')
createLambdaFunction(data['Role']['Arn'])
} else {
console.log('Creating IAM Role MKPBananaTweetRole')
var fs = require('fs')
var policyJson = fs.readFileSync('./aws/MKPBananaTweetRolePolicy.json', 'utf8')
var params = {
AssumeRolePolicyDocument: policyJson,
Path: '/',
RoleName: 'MKPBananaTweetRole'
}
iam.createRole(params, function (err, data) {
if (err) console.log('CreateRole MKPBananaTweetRole error: ' + err, err.stack)
else {
var policies = ['ComprehendReadOnly', 'DynamoDBAccess', 'CloudWatchLogAccess']
for (var i in policies) {
var policy = policies[i]
var params = {
PolicyDocument: fs.readFileSync('./aws/' + policy + '.json', 'utf8'),
PolicyName: policy,
RoleName: 'MKPBananaTweetRole'
}
iam.putRolePolicy(params, function (err, data) {
if (err) console.log('PutRolePolicy error: ' + policy + ' => ' + err, err.stack)
})
}
createLambdaFunction(data['Role']['Arn'])
}
})
}
})
}
function createLambdaFunction(roleArn) {
// Create Lambda Function
var lambda = new AWS.Lambda({region: process.env.AWS_REGION_DYNAMODB})
var paramsLambda = {FunctionName: 'MKPBananaTweet'}
lambda.getFunction(paramsLambda, function (err, data) {
if (err) {
console.log('Creating Lambda Function MKPBananaTweet')
var fs = require('fs')
var zipFile = fs.readFileSync('mkp-banana-tweet-lambda.zip')
var paramsLambda = {
Code: {ZipFile: zipFile},
Description: 'MonkeyPatch Lambda Function example',
FunctionName: 'MKPBananaTweet',
Handler: 'lib/index.handler',
MemorySize: 128,
Publish: true,
Role: roleArn,
Runtime: 'nodejs6.10',
Timeout: 15,
VpcConfig: {}
}
lambda.createFunction(paramsLambda, function (err, data) {
if (err) {
console.log('Cannot create Lambda Function: ' + err)
}
else {
console.log('Lambda Function MKPBananaTweet created')
createTrigger(data['FunctionArn'])
}
})
} else {
console.log('Lambda Function MKPBananaTweet already exists')
createTrigger(data['Configuration']['FunctionArn'])
}
})
}
function createDynamoDBTable() {
// Ensure DynamoDB MKPTweets table exists
var dynamodb = new AWS.DynamoDB({region: process.env.AWS_REGION_DYNAMODB})
dynamodb.listTables({}, function (err, data) {
if (err) console.log(err, err.stack)
else {
// Find table MKPTweets
var tableExists = (data['TableNames'].indexOf('MKPTweets') > -1)
if (!tableExists) {
console.log('Creating DynamoDB Table MKPTweets')
var params = {
AttributeDefinitions: [
{AttributeName: 'id', AttributeType: 'S'}
],
KeySchema: [
{AttributeName: 'id', KeyType: 'HASH'}
],
ProvisionedThroughput: {ReadCapacityUnits: 1, WriteCapacityUnits: 1},
TableName: 'MKPTweets'
}
dynamodb.createTable(params, function (err, data) {
if (err) {
console.log('Cannot create DynamoDB Table: ' + err)
}
else {
console.log('DynamoDB Table MKPTweets created')
}
})
} else {
console.log('DynamoDB Table MKPTweets already exists')
}
}
})
}
function createTrigger(lambdaArn) {
console.log('Creating Lambda Trigger')
var cloudwatchevents = new AWS.CloudWatchEvents({region: process.env.AWS_REGION_DYNAMODB})
var params = {
Name: 'Every20Minutes',
Description: 'Cron expression event every 20 minutes',
ScheduleExpression: 'rate(20 minutes)',
State: 'DISABLED'
}
cloudwatchevents.putRule(params, function(err, data) {
if (err) console.log(err, err.stack);
else {
var paramsTarget = {
Rule: 'Every20Minutes',
Targets: [{Arn: lambdaArn, Id: '1'}]
}
cloudwatchevents.putTargets(paramsTarget, function(err, data) {
if (err) console.log(err, err.stack);
else console.log('Lambda Trigger Created')
});
}
})
}
createIAMRole()
createDynamoDBTable()