-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.js
73 lines (65 loc) · 1.36 KB
/
handler.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
"use strict"
const Route53 = require("aws-sdk/clients/route53")
const dns = require("dns").promises
const configList = require("./config")
module.exports.updateRecord = async (event) => {
const domain = event.queryStringParameters.domain
let config
for (const c of configList) {
if (c.domain === domain) {
config = c
break
}
}
if (!config) {
return {
statusCode: 400,
body: "domain not found in config",
}
}
if (config.auth !== event.queryStringParameters.auth) {
return {
statusCode: 401,
body: "bad auth",
}
}
const sourceIp = event.requestContext.identity.sourceIp
const addresses = await dns.resolve4(domain)
for (const addr of addresses) {
if (addr === sourceIp) {
return {
statusCode: 200,
body: `${sourceIp} already assigned to ${domain}`,
}
}
}
const route53 = new Route53({
apiVersion: "2013-04-01",
accessKeyId: config.accessKeyId,
secretAccessKey: config.secretAccessKey,
})
const res = await route53.changeResourceRecordSets({
ChangeBatch: {
Changes: [
{
Action: "UPSERT",
ResourceRecordSet: {
Name: config.domain,
ResourceRecords: [
{
Value: sourceIp,
},
],
TTL: 3600 * 3, // 3 hours
Type: "A",
},
},
],
},
HostedZoneId: config.hostedZoneId
}).promise()
return {
statusCode: 200,
body: JSON.stringify(res),
}
}