Correct way to use AWS SDK within AWS CDK

Question:

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:

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?

Answer:

You could implement the AWS SDK calls using a AwsCustomResource

This allows calling AWS SDK functions on different Resource actions (oncreate, ondelete, onupdate)

The result data can be consumed by calling getData. This does further allow the cdk created CloudFormation Template to work without a client to run it.

One Example where I used this is the following:

Leave a Reply