CfnInclude

class aws_cdk.cloudformation_include.CfnInclude(scope, id, *, template_file, allow_cyclical_references=None, load_nested_stacks=None, parameters=None, preserve_logical_ids=None)

Bases: CfnElement

Construct to import an existing CloudFormation template file into a CDK application.

All resources defined in the template file can be retrieved by calling the getResource method. Any modifications made on the returned resource objects will be reflected in the resulting CDK template.

ExampleMetadata:

infused

Example:

cfn_template = cfn_inc.CfnInclude(self, "Template",
    template_file="my-template.json"
)
Parameters:
  • scope (Construct) –

  • id (str) –

  • template_file (str) – Path to the template file. Both JSON and YAML template formats are supported.

  • allow_cyclical_references (Optional[bool]) – Specifies whether to allow cyclical references, effectively disregarding safeguards meant to avoid undeployable templates. This should only be set to true in the case of templates utilizing cloud transforms (e.g. SAM) that after processing the transform will no longer contain any circular references. Default: - will throw an error on detecting any cyclical references

  • load_nested_stacks (Optional[Mapping[str, Union[CfnIncludeProps, Dict[str, Any]]]]) – Specifies the template files that define nested stacks that should be included. If your template specifies a stack that isn’t included here, it won’t be created as a NestedStack resource, and it won’t be accessible from the CfnInclude.getNestedStack method (but will still be accessible from the CfnInclude.getResource method). If you include a stack here with an ID that isn’t in the template, or is in the template but is not a nested stack, template creation will fail and an error will be thrown. Default: - no nested stacks will be included

  • parameters (Optional[Mapping[str, Any]]) – Specifies parameters to be replaced by the values in this mapping. Any parameters in the template that aren’t specified here will be left unmodified. If you include a parameter here with an ID that isn’t in the template, template creation will fail and an error will be thrown. If you are importing a parameter from a live stack, we cannot know the value of that parameter. You will need to supply a value for your parameters, else the default value will be used. Default: - parameters will retain their original definitions

  • preserve_logical_ids (Optional[bool]) – Whether the resources should have the same logical IDs in the resulting CDK template as they did in the original CloudFormation template file. If you’re vending a Construct using an existing CloudFormation template, make sure to pass this as false. Note: regardless of whether this option is true or false, the CfnInclude.getResource and related methods always uses the original logical ID of the resource/element, as specified in the template file. Default: true

Methods

get_condition(condition_name)

Returns the CfnCondition object from the ‘Conditions’ section of the CloudFormation template with the given name.

Any modifications performed on that object will be reflected in the resulting CDK template.

If a Condition with the given name is not present in the template, throws an exception.

Parameters:

condition_name (str) – the name of the Condition in the CloudFormation template file.

Return type:

CfnCondition

get_hook(hook_logical_id)

Returns the CfnHook object from the ‘Hooks’ section of the included CloudFormation template with the given logical ID.

Any modifications performed on the returned object will be reflected in the resulting CDK template.

If a Hook with the given logical ID is not present in the template, an exception will be thrown.

Parameters:

hook_logical_id (str) – the logical ID of the Hook in the included CloudFormation template’s ‘Hooks’ section.

Return type:

CfnHook

get_mapping(mapping_name)

Returns the CfnMapping object from the ‘Mappings’ section of the included template.

Any modifications performed on that object will be reflected in the resulting CDK template.

If a Mapping with the given name is not present in the template, an exception will be thrown.

Parameters:

mapping_name (str) – the name of the Mapping in the template to retrieve.

Return type:

CfnMapping

get_nested_stack(logical_id)

Returns a loaded NestedStack with name logicalId.

For a nested stack to be returned by this method, it must be specified either in the CfnIncludeProps.loadNestedStacks property, or through the loadNestedStack method.

Parameters:

logical_id (str) – the ID of the stack to retrieve, as it appears in the template.

Return type:

IncludedNestedStack

get_output(logical_id)

Returns the CfnOutput object from the ‘Outputs’ section of the included template.

Any modifications performed on that object will be reflected in the resulting CDK template.

If an Output with the given name is not present in the template, throws an exception.

Parameters:

logical_id (str) – the name of the output to retrieve.

Return type:

CfnOutput

get_parameter(parameter_name)

Returns the CfnParameter object from the ‘Parameters’ section of the included template.

Any modifications performed on that object will be reflected in the resulting CDK template.

If a Parameter with the given name is not present in the template, throws an exception.

Parameters:

parameter_name (str) – the name of the parameter to retrieve.

Return type:

CfnParameter

get_resource(logical_id)

Returns the low-level CfnResource from the template with the given logical ID.

Any modifications performed on that resource will be reflected in the resulting CDK template.

The returned object will be of the proper underlying class; you can always cast it to the correct type in your code:

// assume the template contains an AWS::S3::Bucket with logical ID 'Bucket'
const cfnBucket = cfnTemplate.getResource('Bucket') as s3.CfnBucket;
// cfnBucket is of type s3.CfnBucket

If the template does not contain a resource with the given logical ID, an exception will be thrown.

Parameters:

logical_id (str) – the logical ID of the resource in the CloudFormation template file.

Return type:

CfnResource

get_rule(rule_name)

Returns the CfnRule object from the ‘Rules’ section of the CloudFormation template with the given name.

Any modifications performed on that object will be reflected in the resulting CDK template.

If a Rule with the given name is not present in the template, an exception will be thrown.

Parameters:

rule_name (str) – the name of the Rule in the CloudFormation template.

Return type:

CfnRule

load_nested_stack(logical_id, *, template_file, allow_cyclical_references=None, load_nested_stacks=None, parameters=None, preserve_logical_ids=None)

Includes a template for a child stack inside of this parent template.

A child with this logical ID must exist in the template, and be of type AWS::CloudFormation::Stack. This is equivalent to specifying the value in the CfnIncludeProps.loadNestedStacks property on object construction.

Parameters:
  • logical_id (str) – the ID of the stack to retrieve, as it appears in the template.

  • template_file (str) – Path to the template file. Both JSON and YAML template formats are supported.

  • allow_cyclical_references (Optional[bool]) – Specifies whether to allow cyclical references, effectively disregarding safeguards meant to avoid undeployable templates. This should only be set to true in the case of templates utilizing cloud transforms (e.g. SAM) that after processing the transform will no longer contain any circular references. Default: - will throw an error on detecting any cyclical references

  • load_nested_stacks (Optional[Mapping[str, Union[CfnIncludeProps, Dict[str, Any]]]]) – Specifies the template files that define nested stacks that should be included. If your template specifies a stack that isn’t included here, it won’t be created as a NestedStack resource, and it won’t be accessible from the CfnInclude.getNestedStack method (but will still be accessible from the CfnInclude.getResource method). If you include a stack here with an ID that isn’t in the template, or is in the template but is not a nested stack, template creation will fail and an error will be thrown. Default: - no nested stacks will be included

  • parameters (Optional[Mapping[str, Any]]) – Specifies parameters to be replaced by the values in this mapping. Any parameters in the template that aren’t specified here will be left unmodified. If you include a parameter here with an ID that isn’t in the template, template creation will fail and an error will be thrown. If you are importing a parameter from a live stack, we cannot know the value of that parameter. You will need to supply a value for your parameters, else the default value will be used. Default: - parameters will retain their original definitions

  • preserve_logical_ids (Optional[bool]) – Whether the resources should have the same logical IDs in the resulting CDK template as they did in the original CloudFormation template file. If you’re vending a Construct using an existing CloudFormation template, make sure to pass this as false. Note: regardless of whether this option is true or false, the CfnInclude.getResource and related methods always uses the original logical ID of the resource/element, as specified in the template file. Default: true

Return type:

IncludedNestedStack

Returns:

the same IncludedNestedStack object that getNestedStack returns for this logical ID

override_logical_id(new_logical_id)

Overrides the auto-generated logical ID with a specific ID.

Parameters:

new_logical_id (str) – The new logical ID to use for this stack element.

Return type:

None

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

creation_stack

return:

the stack trace of the point where this Resource was created from, sourced from the +metadata+ entry typed +aws:cdk:logicalId+, and with the bottom-most node +internal+ entries filtered.

logical_id

The logical ID for this CloudFormation stack element.

The logical ID of the element is calculated from the path of the resource node in the construct tree.

To override this value, use overrideLogicalId(newLogicalId).

Returns:

the logical ID as a stringified token. This value will only get resolved during synthesis.

node

The tree node.

stack

The stack in which this element is defined.

CfnElements must be defined within a stack scope (directly or indirectly).

Static Methods

classmethod is_cfn_element(x)

Returns true if a construct is a stack element (i.e. part of the synthesized cloudformation template).

Uses duck-typing instead of instanceof to allow stack elements from different versions of this library to be included in the same stack.

Parameters:

x (Any) –

Return type:

bool

Returns:

The construct as a stack element or undefined if it is not a stack element.

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters:

x (Any) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.