Created
June 7, 2020 20:15
-
-
Save logemann/8940f9668c04400ca172c11b28df134f to your computer and use it in GitHub Desktop.
change CName lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AWS = require('aws-sdk'); | |
var route53 = new AWS.Route53(); | |
exports.handler = async (event) => { | |
console.log("Invoking CName change Script"); | |
var ec2 = new AWS.EC2(); | |
let vpnUrl; | |
var data = await ec2.describeClientVpnEndpoints({}).promise(); | |
if (data.ClientVpnEndpoints.length > 0) { | |
console.log(data.ClientVpnEndpoints[0].DnsName); | |
vpnUrl = data.ClientVpnEndpoints[0].DnsName; | |
return route53.changeResourceRecordSets(getParameters(event.cname, event.dnsZoneId, vpnUrl)).promise(); | |
} else { | |
throw "No active VPN Endpoint! No DNS modification needed."; | |
} | |
}; | |
function getParameters(cname, hostedZoneId, vpnUrl) { | |
return { | |
ChangeBatch: { | |
"Comment": "Auto generated CName Record for VPN Endpoint", | |
"Changes": [ | |
{ | |
"Action": "UPSERT", | |
"ResourceRecordSet": { | |
"Name": cname, | |
"Type": "CNAME", | |
"TTL": 300, | |
"ResourceRecords": [ | |
{ | |
"Value": vpnUrl | |
} | |
] | |
} | |
} | |
] | |
}, | |
HostedZoneId: hostedZoneId | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment