AWS CDK Number Parameter as Integer
Question:
I’m using the AWS CDK and I’m struggling with the number parameter. The documentation says that numbers can be either an int or a float. Here’s how a number parameter is setup in the code:
number_parameter = CfnParameter(self, "number_parameter", type="Number",
description="Number Parameter")
And here’s how I’m accessing the value:
number_parameter.value_as_number
The problem I’m running into, is that whenever I use cdk synth, it’s assuming that this parameter is a float. I know this because I’m trying to use the parameter in functions that only accept integers, not floats, which is causing an error. I also cannot convert it to an integer using the int() operator, because I get an error that it’s too large to convert to an integer.
How do I specify that I only want integers, not floats, for this parameter? Or is there some other way I can convert it to an integer, even though I can’t control how the CDK initializes this value?
Answers:
It turns out that this question was just me misunderstanding how the AWS CDK actually works. I was trying to use CDK parameters in a way that is not supported. There were 2 issues with what I was trying to do:
- Parameters can only be resolved at runtime. I was trying to set them using cdk synth. Upon actually deploying my solution, I was able to input an integer parameter fine.
- Parameters cannot be used in CDK code. So, you cannot set a parameter to 1 and then use it in a function or a statement in the code. You can only use it to set values in the resulting CloudFormation template.
I’m using the AWS CDK and I’m struggling with the number parameter. The documentation says that numbers can be either an int or a float. Here’s how a number parameter is setup in the code:
number_parameter = CfnParameter(self, "number_parameter", type="Number",
description="Number Parameter")
And here’s how I’m accessing the value:
number_parameter.value_as_number
The problem I’m running into, is that whenever I use cdk synth, it’s assuming that this parameter is a float. I know this because I’m trying to use the parameter in functions that only accept integers, not floats, which is causing an error. I also cannot convert it to an integer using the int() operator, because I get an error that it’s too large to convert to an integer.
How do I specify that I only want integers, not floats, for this parameter? Or is there some other way I can convert it to an integer, even though I can’t control how the CDK initializes this value?
It turns out that this question was just me misunderstanding how the AWS CDK actually works. I was trying to use CDK parameters in a way that is not supported. There were 2 issues with what I was trying to do:
- Parameters can only be resolved at runtime. I was trying to set them using cdk synth. Upon actually deploying my solution, I was able to input an integer parameter fine.
- Parameters cannot be used in CDK code. So, you cannot set a parameter to 1 and then use it in a function or a statement in the code. You can only use it to set values in the resulting CloudFormation template.