aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.63k stars 3.91k forks source link

(cdk): Field from AWSCustomResource cannot be used as Parameter #14928

Closed rirze closed 3 years ago

rirze commented 3 years ago

I'm trying to use a value from an API call (via AWSCustomResource and AWSApiCall objects) to set a parameter for another resource. However, I am unable to do synthesize, since JSII complains about the parameter being an string ($Token[...]) instead of being an integer as expected.

Reproduction Steps

The following is a code sample to illustrate the idea, but is not exactly functional. If desired, I can post my exact code, but I believe this is enough to discuss the scope of the issue.

class CustomRuleGroup(cdk.Construct):
    def __init__(self, scope_, id, *, **kwargs):
        super().__init__(scope_, id)

        calculate_rule_capacity_call = cdk_custom_resources.AwsSdkCall(
            action="checkCapacity",
            service='WAFV2',
            parameters={
                "Rules": some_transformation(kwargs[rules])
            },
            physical_resource_id=cdk_custom_resources.PhysicalResourceId.of(f"{account_name}CalculateWAFRuleGroupCapacity")
        )

        calculate_rule_capacity = cdk_custom_resources.AwsCustomResource(
            self, f"{account_name}CalculateWAFRuleGroupCapacityResource",
            function_name="CalculateWAFRuleGroupCapacity",
            policy=cdk_custom_resources.AwsCustomResourcePolicy.from_sdk_calls(resources=[]),
            on_create=calculate_rule_capacity_call,
            on_update=calculate_rule_capacity_call
        )

        self.rule_group = cdk_wafv2.CfnRuleGroup(scope_, id, 
            capacity=calculate_rule_capacity.get_response_field_reference('Capacity'), **kwargs)

What did you expect to happen?

Ideally, I could have a smart WAF RuleGroup class that calls the Api call wafv2.CheckCapacity and dynamically sets the RuleGroup capacity automatically during synthesis. During deployment, it would ideally create all dependent resources first and then issue the call and then create the RuleGroup using the result from the Api call.

What actually happened?

Synthesis fails, since JSII doesn't like that a token/reference is being passed into one of the parameters instead of a proper integer.

Environment

Other


This is :bug: Bug Report

rix0rrr commented 3 years ago

You can turn the value into a fake number by calling Token.asNumber(). This will allow you to escape the type system of your language, if you are convinced that everything is alright at deploy time.

github-actions[bot] commented 3 years ago

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.