Octavian M
Octavian M

Reputation: 73

AWS CDK conditional ImportValue

I'm importing an ARN from another stack with the cdk.Fn.importValue method. This works fine if I know that the output value is always present, but I don't know how to handle the case when the value I try to import is optional.

How can I get something similar to: (checking if the value exists before importing it)

if(value exists) {
    cdk.Fn.importValue("value")
}

Upvotes: 2

Views: 2449

Answers (1)

Thomas Krause
Thomas Krause

Reputation: 170

AFAIK there currently is no way in CDK to perform a lookup of a CloudFormation exports during synthesis time.

If you don't want to fiddle around with performing CloudFormation API calls with the aws-sdk before creating the CDK stack, in my opinion the most elegant way to share conditional values between stacks, is to use SSM parameters instead of CloudFormation exports.

SSM parameters can be looked up during synthesis time. See docs: https://docs.aws.amazon.com/cdk/v2/guide/get_ssm_value.html

So, with StringParameter.valueFromLookup you are then able to only use the value if it exists (IIRC the method throws an error if the parameter doesn't exist, so try-catch is your friend here, but not 100% sure).

Upvotes: 2

Related Questions