-
Notifications
You must be signed in to change notification settings - Fork 15
/
Route53Function.js
175 lines (163 loc) · 5.62 KB
/
Route53Function.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
/**
*
* Handler called by Lambda function.
* @param {object} event - event parameter gets the attributes from CFN trigger.
* @param {object} context - context parameter used to log details to CloudWatch log stream.
*
*/
exports.handler = function(event, context) {
console.log('REQUEST RECEIVED:\n', JSON.stringify(event));
console.log('Assume Role: ' + event.ResourceProperties.RoleArn);
var responseStatus = 'FAILED';
var resourceaction = 'UPSERT';
var paramsstr = '';
if (event.RequestType === 'Delete') {
resourceaction = 'DELETE';
}
var params = {
RoleArn: event.ResourceProperties.RoleArn,
RoleSessionName: 'CrossAccountRoute53Role',
DurationSeconds: 900
};
var aws = require('aws-sdk');
var sts = new aws.STS();
console.log('Going to assume role. ');
sts.assumeRole(params, function(err, data) {
if (err) {
responseData = {Error: 'Failed to assume role'};
console.log(responseData.Error + ':\n', err);
}
else {
var accessparams = {
AccessID: data.Credentials.AccessKeyId,
SecretAccessKey: data.Credentials.SecretAccessKey,
SessionToken: data.Credentials.SessionToken
};
var route53 = new aws.Route53({accessKeyId: data.Credentials.AccessKeyId,
secretAccessKey: data.Credentials.SecretAccessKey, sessionToken: data.Credentials.SessionToken});
if (event.ResourceProperties.Type === 'A' && event.ResourceProperties.Alias === 'true') {
r53params = {
HostedZoneId: event.ResourceProperties.HostedZoneId,
ChangeBatch: {
Changes: [
{
Action: resourceaction,
ResourceRecordSet: {
Name: event.ResourceProperties.Name,
Type: event.ResourceProperties.Type,
AliasTarget: {
DNSName: event.ResourceProperties.DNSName,
EvaluateTargetHealth: false,
HostedZoneId: event.ResourceProperties.ResourceHostedZoneId
}
}
}
]
}
};
}
else if (event.ResourceProperties.Type === 'CNAME') {
r53params = {
HostedZoneId: event.ResourceProperties.HostedZoneId,
ChangeBatch: {
Changes: [
{
Action: resourceaction,
ResourceRecordSet: {
Name: event.ResourceProperties.Name,
Type: event.ResourceProperties.Type,
ResourceRecords: [
{
Value: event.ResourceProperties.DNSName
}
],
TTL: '300',
}
}
]
}
};
}
else if (event.ResourceProperties.Type === 'A' && event.ResourceProperties.Alias === 'false') {
r53params = {
HostedZoneId: event.ResourceProperties.HostedZoneId,
ChangeBatch: {
Changes: [
{
Action: resourceaction,
ResourceRecordSet: {
Name: event.ResourceProperties.Name,
Type: event.ResourceProperties.Type,
TTL: '300',
ResourceRecords: [
{
Value: event.ResourceProperties.IP
}
]
}
}
]
}
};
}
else {
responseData = {Error: 'Unsupported DNS Type' + event.ResourceProperties.Type};
console.log(responseData.Error);
console.log('Currently supports only A record & CNAME record.');
sendResponse(event, context, responseStatus, responseData);
}
console.log('Using the following parameters for Route53.');
console.log(JSON.stringify(r53params));
route53.changeResourceRecordSets(r53params, function(err, r53data) {
if (err) {
responseData = {Error: 'Failed to configure DNS record'};
console.log(responseData.Error + ':\n', err);
}
else {
responseStatus = 'SUCCESS';
responseData = {Success: 'State of DNS record ' + r53data.Status};
responseData.URL = event.ResourceProperties.Name;
console.log(r53data); // successful response
}
sendResponse(event, context, responseStatus, responseData);
});
}
});
};
// Sends a response to the pre-signed S3 URL
var sendResponse = function(event, context, responseStatus, responseData) {
var responseBody = JSON.stringify({
Status: responseStatus,
Reason: 'See the details in CloudWatch Log Stream: ' + context.logStreamName,
PhysicalResourceId: context.logStreamName,
StackId: event.StackId,
RequestId: event.RequestId,
LogicalResourceId: event.LogicalResourceId,
Data: responseData
});
console.log('RESPONSE BODY:\n', responseBody);
var https = require('https');
var url = require('url');
var parsedUrl = url.parse(event.ResponseURL);
var options = {
hostname: parsedUrl.hostname,
port: 443,
path: parsedUrl.path,
method: 'PUT',
headers: {
'Content-Type': '',
'Content-Length': responseBody.length
}
};
var req = https.request(options, function(res) {
console.log('STATUS:', res.statusCode);
console.log('HEADERS:', JSON.stringify(res.headers));
context.succeed('Successfully sent stack response!');
});
req.on('error', function(err) {
console.log('sendResponse Error:\n', err);
context.fail(err);
});
req.write(responseBody);
req.end();
};