I'm trying to use the SDK within my CDK application. My stack is creating a directory, some networking stuff, then some instances which it joins to the domain using a PowerShell script which requires the AD DNS IPs, I'm currently using it like so:
const ds = new DirectoryService();
const result = new Promise(function (resolve: (value: string) => any, reject) {
ds.describeDirectories({}, function (err, data) {
if (err){
reject(data)
}else{
try{
if (data.DirectoryDescriptions){
if (data.DirectoryDescriptions[0].DnsIpAddrs){
resolve(data.DirectoryDescriptions[0].DnsIpAddrs.toString())
}
}
}catch (e) {
reject("Directory doesn't exist yet.")
}
}
})
});
result.then(value => {
const subs = {
"#{DNS_ADDRESSES}": value,
"#{SECRET_ID}": directory.directorySecret.secretArn
};
Object.entries(subs).forEach(
([key, value]) => {
domainJoinScript = domainJoinScript.replace(key, String(value));
}
);
new Stack(app, 'instances', networking.vpc, domainJoinScript);
}).catch(error => {
print(error)
});
Now this works, but it's far from a clean solution. My new Stack has multiple resources within it which means I have to pass the result of the SDK call through several levels, instead of just directly where it's needed, and if I had to make multiple SDK calls this would get even messier.
The core problem is the AWS SDK being purely asynchronous in JS, which means I have to use the fairly verbose pattern above to wrap it.
Does anyone have a better way to do this?