aws-cdk-lib.assertions.Template

class Template

LanguageType name
.NETAmazon.CDK.Assertions.Template
Gogithub.com/aws/aws-cdk-go/awscdk/v2/assertions#Template
Javasoftware.amazon.awscdk.assertions.Template
Pythonaws_cdk.assertions.Template
TypeScript (source)aws-cdk-lib » assertions » Template

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

import { Stack } from 'aws-cdk-lib';
import { Template } from 'aws-cdk-lib/assertions';

const stack = new Stack(/* ... */);
// ...
const template = Template.fromStack(stack);

Methods

NameDescription
allResources(type, props)Assert that all resources of the given type contain the given definition in the CloudFormation template.
allResourcesProperties(type, props)Assert that all resources of the given type contain the given properties CloudFormation template.
findConditions(logicalId, props?)Get the set of matching Conditions that match the given properties in the CloudFormation template.
findMappings(logicalId, props?)Get the set of matching Mappings that match the given properties in the CloudFormation template.
findOutputs(logicalId, props?)Get the set of matching Outputs that match the given properties in the CloudFormation template.
findParameters(logicalId, props?)Get the set of matching Parameters that match the given properties in the CloudFormation template.
findResources(type, props?)Get the set of matching resources of a given type and properties in the CloudFormation template.
hasCondition(logicalId, props)Assert that a Condition with the given properties exists in the CloudFormation template.
hasMapping(logicalId, props)Assert that a Mapping with the given properties exists in the CloudFormation template.
hasOutput(logicalId, props)Assert that an Output with the given properties exists in the CloudFormation template.
hasParameter(logicalId, props)Assert that a Parameter with the given properties exists in the CloudFormation template.
hasResource(type, props)Assert that a resource of the given type and given definition exists in the CloudFormation template.
hasResourceProperties(type, props)Assert that a resource of the given type and properties exists in the CloudFormation template.
resourceCountIs(type, count)Assert that the given number of resources of the given type exist in the template.
resourcePropertiesCountIs(type, props, count)Assert that the given number of resources of the given type and properties exists in the CloudFormation template.
templateMatches(expected)Assert that the CloudFormation template matches the given value.
toJSON()The CloudFormation template deserialized into an object.
static fromJSON(template, templateParsingOptions?)Base your assertions from an existing CloudFormation template formatted as an in-memory JSON object.
static fromStack(stack, templateParsingOptions?)Base your assertions on the CloudFormation template synthesized by a CDK Stack.
static fromString(template, templateParsingOptions?)Base your assertions from an existing CloudFormation template formatted as a JSON string.

allResources(type, props)

public allResources(type: string, props: any): void

Parameters

  • type string — the resource type;
  • props any — the entire definition of the resources as they should be expected in the template.

Assert that all resources of the given type contain the given definition in the CloudFormation template.

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


allResourcesProperties(type, props)

public allResourcesProperties(type: string, props: any): void

Parameters

  • type string — the resource type;
  • props any — the 'Properties' section of the resource as should be expected in the template.

Assert that all resources of the given type contain the given properties CloudFormation template.

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


findConditions(logicalId, props?)

public findConditions(logicalId: string, props?: any): { [string]: { [string]: any } }

Parameters

  • logicalId string — the name of the condition.
  • props any — by default, matches all Conditions in the template.

Returns

  • { [string]: { [string]: any } }

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


findMappings(logicalId, props?)

public findMappings(logicalId: string, props?: any): { [string]: { [string]: any } }

Parameters

  • logicalId string — the name of the mapping.
  • props any — by default, matches all Mappings in the template.

Returns

  • { [string]: { [string]: any } }

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


findOutputs(logicalId, props?)

public findOutputs(logicalId: string, props?: any): { [string]: { [string]: any } }

Parameters

  • logicalId string — the name of the output.
  • props any — by default, matches all Outputs in the template.

Returns

  • { [string]: { [string]: any } }

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


findParameters(logicalId, props?)

public findParameters(logicalId: string, props?: any): { [string]: { [string]: any } }

Parameters

  • logicalId string — the name of the parameter.
  • props any — by default, matches all Parameters in the template.

Returns

  • { [string]: { [string]: any } }

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


findResources(type, props?)

public findResources(type: string, props?: any): { [string]: { [string]: any } }

Parameters

  • type string — the type to match in the CloudFormation template.
  • props any — by default, matches all resources with the given type.

Returns

  • { [string]: { [string]: any } }

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


hasCondition(logicalId, props)

public hasCondition(logicalId: string, props: any): void

Parameters

  • logicalId string — the name of the mapping.
  • props any — the output as should be expected in the template.

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

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


hasMapping(logicalId, props)

public hasMapping(logicalId: string, props: any): void

Parameters

  • logicalId string — the name of the mapping.
  • props any — the output as should be expected in the template.

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 behavior, use other matchers in the Match class.


hasOutput(logicalId, props)

public hasOutput(logicalId: string, props: any): void

Parameters

  • logicalId string — the name of the output.
  • props any — the output as should be expected in the template.

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 behavior, use other matchers in the Match class.


hasParameter(logicalId, props)

public hasParameter(logicalId: string, props: any): void

Parameters

  • logicalId string — the name of the parameter.
  • props any — the parameter as should be expected in the template.

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

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


hasResource(type, props)

public hasResource(type: string, props: any): void

Parameters

  • type string — the resource type;
  • props any — the entire definition of the resource as should be expected in the template.

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 behavior, use other matchers in the Match class.


hasResourceProperties(type, props)

public hasResourceProperties(type: string, props: any): void

Parameters

  • type string — the resource type;
  • props any — the 'Properties' section of the resource as should be expected in the template.

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 behavior, use other matchers in the Match class.


resourceCountIs(type, count)

public resourceCountIs(type: string, count: number): void

Parameters

  • type string — the resource type;
  • count number — number of expected instances.

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


resourcePropertiesCountIs(type, props, count)

public resourcePropertiesCountIs(type: string, props: any, count: number): void

Parameters

  • type string — the resource type;
  • props any — the 'Properties' section of the resource as should be expected in the template.
  • count number — number of expected instances.

Assert that the given number of resources of the given type and properties exists in the CloudFormation template.


templateMatches(expected)

public templateMatches(expected: any): void

Parameters

  • expected any — the expected CloudFormation template as key-value pairs.

Assert that the CloudFormation template matches the given value.


toJSON()

public toJSON(): { [string]: any }

Returns

  • { [string]: any }

The CloudFormation template deserialized into an object.


static fromJSON(template, templateParsingOptions?)

public static fromJSON(template: { [string]: any }, templateParsingOptions?: TemplateParsingOptions): Template

Parameters

  • template { [string]: any } — the CloudFormation template formatted as a nested set of records.
  • templateParsingOptions TemplateParsingOptions — Optional param to configure template parsing behavior, such as disregarding circular dependencies.

Returns

  • Template

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


static fromStack(stack, templateParsingOptions?)

public static fromStack(stack: Stack, templateParsingOptions?: TemplateParsingOptions): Template

Parameters

  • stack Stack — the CDK Stack to run assertions on.
  • templateParsingOptions TemplateParsingOptions — Optional param to configure template parsing behavior, such as disregarding circular dependencies.

Returns

  • Template

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


static fromString(template, templateParsingOptions?)

public static fromString(template: string, templateParsingOptions?: TemplateParsingOptions): Template

Parameters

  • template string — the CloudFormation template in.
  • templateParsingOptions TemplateParsingOptions — Optional param to configure template parsing behavior, such as disregarding circular dependencies.

Returns

  • Template

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