# 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.

- Source: [`assertions/lib/template.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/assertions/lib/template.ts#L21)
- Type last changed: v2.182.0 · 5 Mar 2025

## Example

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

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

## Methods

- `allResources(type: string, props: any): void` — 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.

  **Parameters**

  - `type` — the resource type;
  - `props` — the entire definition of the resources as they should be expected in the template.

- `allResourcesProperties(type: string, props: any): void` — 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.

  **Parameters**

  - `type` — the resource type;
  - `props` — the 'Properties' section of the resource as should be expected in the template.

- `findConditions(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }` — Get the set of matching Conditions that match the given properties in the CloudFormation template.

  **Parameters**

  - `logicalId` — the name of the condition, provide `'*'` to match all conditions in the template.
  - `props` — by default, matches all Conditions in the template.

- `findMappings(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }` — Get the set of matching Mappings that match the given properties in the CloudFormation template.

  **Parameters**

  - `logicalId` — the name of the mapping, provide `'*'` to match all mappings in the template.
  - `props` — by default, matches all Mappings in the template.

- `findOutputs(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }` — Get the set of matching Outputs that match the given properties in the CloudFormation template.

  **Parameters**

  - `logicalId` — the name of the output, provide `'*'` to match all outputs in the template.
  - `props` — by default, matches all Outputs in the template.

- `findParameters(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }` — Get the set of matching Parameters that match the given properties in the CloudFormation template.

  **Parameters**

  - `logicalId` — the name of the parameter, provide `'*'` to match all parameters in the template.
  - `props` — by default, matches all Parameters in the template.

- `findResources(type: string, props?: any): { [key: string]: { [key: string]: any } }` — Get the set of matching resources of a given type and properties in the CloudFormation template.

  **Parameters**

  - `type` — the type to match in the CloudFormation template.
  - `props` — by default, matches all resources with the given type.

- `fromJSON(template: { [key: string]: any }, templateParsingOptions?: TemplateParsingOptions): Template // chainable` (static) — Base your assertions from an existing CloudFormation template formatted as an in-memory JSON object.

  **Parameters**

  - `template` — the CloudFormation template formatted as a nested set of records.
  - `templateParsingOptions` — Optional param to configure template parsing behavior, such as disregarding circular dependencies.

- `fromStack(stack: Stack, templateParsingOptions?: TemplateParsingOptions): Template // chainable` (static) — Base your assertions on the CloudFormation template synthesized by a CDK `Stack`.

  **Parameters**

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

- `fromString(template: string, templateParsingOptions?: TemplateParsingOptions): Template // chainable` (static) — Base your assertions from an existing CloudFormation template formatted as a JSON string.

  **Parameters**

  - `template` — the CloudFormation template in.
  - `templateParsingOptions` — Optional param to configure template parsing behavior, such as disregarding circular dependencies.

- `getResourceId(type: string, props?: any): string` — Get the Resource ID of a matching resource, expects only to find one match.

  Throws AssertionError if none or multiple resources were found.

  **Parameters**

  - `type` — the resource type;
  - `props` — by default, matches all resources with the given type.

  **Returns:** The resource id of the matched resource. Performs a partial match via `Match.objectLike()`.

- `hasCondition(logicalId: string, props: any): void` — 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.

  **Parameters**

  - `logicalId` — the name of the mapping, provide `'*'` to match all conditions in the template.
  - `props` — the output as should be expected in the template.

- `hasMapping(logicalId: string, props: any): void` — 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.

  **Parameters**

  - `logicalId` — the name of the mapping, provide `'*'` to match all mappings in the template.
  - `props` — the output as should be expected in the template.

- `hasOutput(logicalId: string, props: any): void` — 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.

  **Parameters**

  - `logicalId` — the name of the output, provide `'*'` to match all outputs in the template.
  - `props` — the output as should be expected in the template.

- `hasParameter(logicalId: string, props: any): void` — 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.

  **Parameters**

  - `logicalId` — the name of the parameter, provide `'*'` to match all parameters in the template.
  - `props` — the parameter as should be expected in the template.

- `hasResource(type: string, props: any): void` — 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.

  **Parameters**

  - `type` — the resource type;
  - `props` — the entire definition of the resource as should be expected in the template.

- `hasResourceProperties(type: string, props: any): void` — 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.

  **Parameters**

  - `type` — the resource type;
  - `props` — the 'Properties' section of the resource as should be expected in the template.

- `resourceCountIs(type: string, count: number): void` — Assert that the given number of resources of the given type exist in the template.

  **Parameters**

  - `type` — the resource type;
  - `count` — number of expected instances.

- `resourcePropertiesCountIs(type: string, props: any, count: number): void` — Assert that the given number of resources of the given type and properties exists in the CloudFormation template.

  **Parameters**

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

- `templateMatches(expected: any): void` — Assert that the CloudFormation template matches the given value.

  **Parameters**

  - `expected` — the expected CloudFormation template as key-value pairs.

- `toJSON(): { [key: string]: any }` — The CloudFormation template deserialized into an object.
