Template

class aws_cdk.assertions.Template(*args: Any, **kwargs)

Bases: object

Suite of assertions that can be run on a CDK stack.

Typically used, as part of unit tests, to validate that the rendered CloudFormation template has expected resources and properties.

Example:

from aws_cdk.core import Stack
from aws_cdk.assertions import Template

stack = Stack()
# ...
template = Template.from_stack(stack)

Methods

find_mappings(logical_id, props=None)

Get the set of matching Mappings that match the given properties in the CloudFormation template.

Parameters
  • logical_id (str) – the name of the mapping. Provide '*' to match all mappings in the template.

  • props (Optional[Any]) – by default, matches all Mappings in the template. When a literal object is provided, performs a partial match via Match.objectLike(). Use the Match APIs to configure a different behaviour.

Return type

Mapping[str, Mapping[str, Any]]

find_outputs(logical_id, props=None)

Get the set of matching Outputs that match the given properties in the CloudFormation template.

Parameters
  • logical_id (str) – the name of the output. Provide '*' to match all outputs in the template.

  • props (Optional[Any]) – by default, matches all Outputs in the template. When a literal object is provided, performs a partial match via Match.objectLike(). Use the Match APIs to configure a different behaviour.

Return type

Mapping[str, Mapping[str, Any]]

find_resources(type, props=None)

Get the set of matching resources of a given type and properties in the CloudFormation template.

Parameters
  • type (str) – the type to match in the CloudFormation template.

  • props (Optional[Any]) – by default, matches all resources with the given type. When a literal is provided, performs a partial match via Match.objectLike(). Use the Match APIs to configure a different behaviour.

Return type

Mapping[str, Mapping[str, Any]]

has_mapping(logical_id, props)

Assert that a Mapping with the given properties exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavour, use other matchers in the Match class.

Parameters
  • logical_id (str) – the name of the mapping. Provide '*' to match all mappings in the template.

  • props (Any) – the output as should be expected in the template.

Return type

None

has_output(logical_id, props)

Assert that an Output with the given properties exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavour, use other matchers in the Match class.

Parameters
  • logical_id (str) – the name of the output. Provide '*' to match all outputs in the template.

  • props (Any) – the output as should be expected in the template.

Return type

None

has_resource(type, props)

Assert that a resource of the given type and given definition exists in the CloudFormation template.

By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavour, use other matchers in the Match class.

Parameters
  • type (str) – the resource type; ex: AWS::S3::Bucket

  • props (Any) – the entire defintion of the resource as should be expected in the template.

Return type

None

has_resource_properties(type, props)

Assert that a resource of the given type and properties exists in the CloudFormation template.

By default, performs partial matching on the Properties key of the resource, via the Match.objectLike(). To configure different behavour, use other matchers in the Match class.

Parameters
  • type (str) – the resource type; ex: AWS::S3::Bucket

  • props (Any) – the ‘Properties’ section of the resource as should be expected in the template.

Return type

None

resource_count_is(type, count)

Assert that the given number of resources of the given type exist in the template.

Parameters
  • type (str) – the resource type; ex: AWS::S3::Bucket

  • count (Union[int, float]) – number of expected instances.

Return type

None

template_matches(expected)

Assert that the CloudFormation template matches the given value.

Parameters

expected (Any) – the expected CloudFormation template as key-value pairs.

Return type

None

to_json()

The CloudFormation template deserialized into an object.

Return type

Mapping[str, Any]

Static Methods

classmethod from_json(template)

Base your assertions from an existing CloudFormation template formatted as an in-memory JSON object.

Parameters

template (Mapping[str, Any]) – the CloudFormation template formatted as a nested set of records.

Return type

Template

classmethod from_stack(stack)

Base your assertions on the CloudFormation template synthesized by a CDK Stack.

Parameters

stack (Stack) – the CDK Stack to run assertions on.

Return type

Template

classmethod from_string(template)

Base your assertions from an existing CloudFormation template formatted as a JSON string.

Parameters

template (str) – the CloudFormation template in.

Return type

Template