I have been attempting to use the AWS CDK to programmatically build a CloudFormation stack template, but am having trouble with using CfnParameter
s to parameterize the template.
When I write a template manually in json/yaml, I declare ParameterGroups
with nested Parameters
lists, as well as defining the individual parameters including Type, Description, AllowedPattern, etc. Then in the definition of my resources, I can reference those parameters via Ref
values, eg
"CidrBlock": {
"Ref": "MyVpcCidr"
}
My question is how do I use this feature via the CDK, defining a parameter and referencing it later in the template?
I have tried the following using software.amazon.awscdk.core.CfnParameter
, but receive an error
final CfnParameter myVpcCidrParameter =
CfnParameter.Builder.create(this, "MyVpcCidr")
.type("String")
.description("The CIDR block for the VPC")
.build();
final Vpc vpc =
Vpc.Builder.create(this, "vpc")
.cidr(myVpcCidrParameter.getValueAsString()) // <-- error on this line
.enableDnsSupport(true)
.enableDnsHostnames(true)
.build();
...
The error states
JsiiException: 'cidr' property must be a concrete CIDR string, got a Token
which seems reasonable because myVpcCidrParameter.getValueAsString()
returns "${Token[TOKEN.10]}"
.
However I don't see any other method on CfnParameter
or the Vpc.Builder
to allow me to use a parameter reference to specify the property value as a reference to my parameter.
What is the proper way to use the CDK to build a template with parameters and resources defined using those parameters?