cloudformation-include: CfnInclude multiple files into one template in CDK v2 #26188
Labels
@aws-cdk/cloudformation-include
Issues related to the "CFN include v.20" package
closed-for-staleness
This issue was automatically closed because it hadn't received any attention in a while.
effort/medium
Medium work item – several days of effort
feature-request
A feature should be added or improved.
p2
response-requested
Waiting on additional info and feedback. Will move to "closing-soon" in 7 days.
Describe the feature
The
CfnInclude
construct from CDK v1's@aws-cdk/core
allowed the import of multiple CloudFormation templates into a stack, with the official CDK docs)describing the result as "All elements of the template will be merged into the current stack, together with any elements created programmatically." Effectively this resulted in the final CloudFormation template being the input templates concatenated in the order that they were listed in theCfnInclude
.This merge functionality seems to have disappeared once
CfnInclude
got deprecated from@aws-cdk/core
in favour of@aws-cdk/cloudformation
, which is now the onlyCfnInclude
construct available in CDK v2. The CDK docs don't explicitly mention it, butCfnInclude
with cross-template dependencies no longer work because each template is validated independently, and we see CDK errors when attempting to build those resources.Use Case
For our use case, we import a CFN nested stack using a pair of YAML template files owned by a partner team for connecting to their VPCE. They have their template files modularized, so one file contains just a
Mappings
section, that is then referenced by other template files. The simplified version of it is something like:mappings.template.yml
endpoint.template.yml
We were able to import them into our nested stack with CDK v1 like so:
However, this is no longer possible after migrating to CDK v2, as we get the following build error:
Error: Mapping used in FindInMap expression with name 'EndpointsMap' was not found in the template
. Additionally, since the type ofCfnIncludeProps
in@aws-cdk/cloudformation
has changed to take intemplateFile
as the name of the template file, instead oftemplate
as the contents of the file, we're not able to work around this very easily by reading the files in together and concatenating the strings.Proposed Solution
Ideally, it'd be great to have some optionality to merge all files passed in via
CfnInclude
into the stack, as it worked in @aws-cdk_core.CfnInclude.Beyond that, allowing us to pass in the contents of the template file via
template
inCfnIncludeProps
could also work. Someone had also suggested a stopgap solution of reading in the files, writing out a temporary template YAML file by concatenation, and then passing in that temporary file name into a singleCfnInclude
parameter, but we were hoping for a more straightforward way to do it.Finally, I think maybe if we nested the Mappings resource into a nested nested stack, we could reference the mappings as an Output from the nested EndpointService stack, but we're not sure if that's even a good solution, as nesting stacks multiple levels deep just for a mapping seems like overkill.
Other Information
Apologies for any typos in the samples, I tried to take our actual templates as an example but there was a lot of data to redact, hopefully the concept of our issue is clear. Thanks!
Acknowledgements
CDK version used
1.177.0, 2.73.0
Environment details (OS name and version, etc.)
MacOS Ventura 13.3.1, Amazon Linux 2 x86_64
The text was updated successfully, but these errors were encountered: