-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cloudformation-include: CfnInclude multiple files into one template in CDK v2 #26188
Comments
Thank you for your report. Are you able to use the templateFile property for CfnInclude in CDK v2? What error would you see with that? |
@pahud Yes, we pass in the import { CfnInclude } from 'aws-cdk-lib/cloudformation-include';
const nestedResources: NestedStack = new NestedStack(this, 'NestedResources');
new CfnInclude(nestedStack, 'mappings', { templateFile: 'file/path/to/mappings.template.yml' });
new CfnInclude(nestedStack, 'endpoint', { templateFile: 'file/path/to/endpoint.template.yml' }); As far as I can tell, we don't have any other option anyways, as that is a required parameter. The error message we see is |
@mxl519 Is that the exact format of the mappings template? If so, there may be a layer missing to the mapping that could cause the error message you are seeing.
If not, could you try to create the mapping in your stack and then include the endpoint.templaye.yml file? Something like
(The code might not be flawless but you have the idea). Then if you are still getting the error it may be the include function. I know this is not the cleanest long term solution since reading in the template is much preferable to making the map object in the code by hand, but it may pinpoint your error. |
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: