Template Class
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
allResources(type: string, props: any): voiddef all_resources(self, type: str, props: Any) -> Nonepublic void allResources(String type, Object props)public void AllResources(string type, object props)func AllResources(Type *string, Props interface{})#By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.
2 parameters
Parameters
typetypetypetypeType— the resource type;propspropspropspropsProps— the entire definition of the resources as they should be expected in the template.
allResourcesProperties(type: string, props: any): voiddef all_resources_properties(self, type: str, props: Any) -> Nonepublic void allResourcesProperties(String type, Object props)public void AllResourcesProperties(string type, object props)func AllResourcesProperties(Type *string, Props interface{})#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.
2 parameters
Parameters
typetypetypetypeType— the resource type;propspropspropspropsProps— the 'Properties' section of the resource as should be expected in the template.
findConditions(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }def find_conditions(self, logical_id: str, props: typing.Optional[Any] = None) -> typing.Mapping[str, typing.Mapping[str, Any]]public Map<String, Map<String, Object>> findConditions(String logicalId, Object props)public IDictionary<string, IDictionary<string, object>> FindConditions(string logicalId, object? props = null)func FindConditions(LogicalId *string, Props interface{}) *map[string]*map[string]interface{}#2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the condition, provide'*'to match all conditions in the template.propspropspropspropsProps— by default, matches all Conditions in the template.
findMappings(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }def find_mappings(self, logical_id: str, props: typing.Optional[Any] = None) -> typing.Mapping[str, typing.Mapping[str, Any]]public Map<String, Map<String, Object>> findMappings(String logicalId, Object props)public IDictionary<string, IDictionary<string, object>> FindMappings(string logicalId, object? props = null)func FindMappings(LogicalId *string, Props interface{}) *map[string]*map[string]interface{}#2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the mapping, provide'*'to match all mappings in the template.propspropspropspropsProps— by default, matches all Mappings in the template.
findOutputs(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }def find_outputs(self, logical_id: str, props: typing.Optional[Any] = None) -> typing.Mapping[str, typing.Mapping[str, Any]]public Map<String, Map<String, Object>> findOutputs(String logicalId, Object props)public IDictionary<string, IDictionary<string, object>> FindOutputs(string logicalId, object? props = null)func FindOutputs(LogicalId *string, Props interface{}) *map[string]*map[string]interface{}#2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the output, provide'*'to match all outputs in the template.propspropspropspropsProps— by default, matches all Outputs in the template.
findParameters(logicalId: string, props?: any): { [key: string]: { [key: string]: any } }def find_parameters(self, logical_id: str, props: typing.Optional[Any] = None) -> typing.Mapping[str, typing.Mapping[str, Any]]public Map<String, Map<String, Object>> findParameters(String logicalId, Object props)public IDictionary<string, IDictionary<string, object>> FindParameters(string logicalId, object? props = null)func FindParameters(LogicalId *string, Props interface{}) *map[string]*map[string]interface{}#2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the parameter, provide'*'to match all parameters in the template.propspropspropspropsProps— by default, matches all Parameters in the template.
findResources(type: string, props?: any): { [key: string]: { [key: string]: any } }def find_resources(self, type: str, props: typing.Optional[Any] = None) -> typing.Mapping[str, typing.Mapping[str, Any]]public Map<String, Map<String, Object>> findResources(String type, Object props)public IDictionary<string, IDictionary<string, object>> FindResources(string type, object? props = null)func FindResources(Type *string, Props interface{}) *map[string]*map[string]interface{}#2 parameters
Parameters
typetypetypetypeType— the type to match in the CloudFormation template.propspropspropspropsProps— by default, matches all resources with the given type.
fromJSON(template: { [key: string]: any }, templateParsingOptions?: TemplateParsingOptions): Template // chainable@staticmethod def from_json(template: typing.Mapping[str, Any], template_parsing_options: typing.Optional[TemplateParsingOptions] = None) -> Templatepublic static Template fromJSON(Map<String, Object> template, TemplateParsingOptions templateParsingOptions)public static Template FromJSON(IDictionary<string, object> template, TemplateParsingOptions? templateParsingOptions = null)func FromJSON(Template *map[string]interface{}, TemplateParsingOptions TemplateParsingOptions) Templatestatic#2 parameters
Parameters
templatetemplatetemplatetemplateTemplate— the CloudFormation template formatted as a nested set of records.templateParsingOptionstemplate_parsing_optionstemplateParsingOptionstemplateParsingOptionsTemplateParsingOptions— Optional param to configure template parsing behavior, such as disregarding circular dependencies.
fromStack(stack: Stack, templateParsingOptions?: TemplateParsingOptions): Template // chainable@staticmethod def from_stack(stack: Stack, template_parsing_options: typing.Optional[TemplateParsingOptions] = None) -> Templatepublic static Template fromStack(Stack stack, TemplateParsingOptions templateParsingOptions)public static Template FromStack(Stack stack, TemplateParsingOptions? templateParsingOptions = null)func FromStack(Stack Stack, TemplateParsingOptions TemplateParsingOptions) Templatestatic#2 parameters
Parameters
stackstackstackstackStack— the CDK Stack to run assertions on.templateParsingOptionstemplate_parsing_optionstemplateParsingOptionstemplateParsingOptionsTemplateParsingOptions— Optional param to configure template parsing behavior, such as disregarding circular dependencies.
fromString(template: string, templateParsingOptions?: TemplateParsingOptions): Template // chainable@staticmethod def from_string(template: str, template_parsing_options: typing.Optional[TemplateParsingOptions] = None) -> Templatepublic static Template fromString(String template, TemplateParsingOptions templateParsingOptions)public static Template FromString(string template, TemplateParsingOptions? templateParsingOptions = null)func FromString(Template *string, TemplateParsingOptions TemplateParsingOptions) Templatestatic#2 parameters
Parameters
templatetemplatetemplatetemplateTemplate— the CloudFormation template in.templateParsingOptionstemplate_parsing_optionstemplateParsingOptionstemplateParsingOptionsTemplateParsingOptions— Optional param to configure template parsing behavior, such as disregarding circular dependencies.
getResourceId(type: string, props?: any): stringdef get_resource_id(self, type: str, props: typing.Optional[Any] = None) -> strpublic String getResourceId(String type, Object props)public string GetResourceId(string type, object? props = null)func GetResourceId(Type *string, Props interface{}) *string#Throws AssertionError if none or multiple resources were found.
2 parameters · returns
Parameters
typetypetypetypeType— the resource type;propspropspropspropsProps— 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): voiddef has_condition(self, logical_id: str, props: Any) -> Nonepublic void hasCondition(String logicalId, Object props)public void HasCondition(string logicalId, object props)func HasCondition(LogicalId *string, Props interface{})#By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.
2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the mapping, provide'*'to match all conditions in the template.propspropspropspropsProps— the output as should be expected in the template.
hasMapping(logicalId: string, props: any): voiddef has_mapping(self, logical_id: str, props: Any) -> Nonepublic void hasMapping(String logicalId, Object props)public void HasMapping(string logicalId, object props)func HasMapping(LogicalId *string, Props interface{})#By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.
2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the mapping, provide'*'to match all mappings in the template.propspropspropspropsProps— the output as should be expected in the template.
hasOutput(logicalId: string, props: any): voiddef has_output(self, logical_id: str, props: Any) -> Nonepublic void hasOutput(String logicalId, Object props)public void HasOutput(string logicalId, object props)func HasOutput(LogicalId *string, Props interface{})#By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.
2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the output, provide'*'to match all outputs in the template.propspropspropspropsProps— the output as should be expected in the template.
hasParameter(logicalId: string, props: any): voiddef has_parameter(self, logical_id: str, props: Any) -> Nonepublic void hasParameter(String logicalId, Object props)public void HasParameter(string logicalId, object props)func HasParameter(LogicalId *string, Props interface{})#By default, performs partial matching on the parameter, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.
2 parameters
Parameters
logicalIdlogical_idlogicalIdlogicalIdLogicalId— the name of the parameter, provide'*'to match all parameters in the template.propspropspropspropsProps— the parameter as should be expected in the template.
hasResource(type: string, props: any): voiddef has_resource(self, type: str, props: Any) -> Nonepublic void hasResource(String type, Object props)public void HasResource(string type, object props)func HasResource(Type *string, Props interface{})#By default, performs partial matching on the resource, via the Match.objectLike(). To configure different behavior, use other matchers in the Match class.
2 parameters
Parameters
typetypetypetypeType— the resource type;propspropspropspropsProps— the entire definition of the resource as should be expected in the template.
hasResourceProperties(type: string, props: any): voiddef has_resource_properties(self, type: str, props: Any) -> Nonepublic void hasResourceProperties(String type, Object props)public void HasResourceProperties(string type, object props)func HasResourceProperties(Type *string, Props interface{})#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.
2 parameters
Parameters
typetypetypetypeType— the resource type;propspropspropspropsProps— the 'Properties' section of the resource as should be expected in the template.
resourceCountIs(type: string, count: number): voiddef resource_count_is(self, type: str, count: int | float) -> Nonepublic void resourceCountIs(String type, Number count)public void ResourceCountIs(string type, double count)func ResourceCountIs(Type *string, Count *float64)#2 parameters
Parameters
typetypetypetypeType— the resource type;countcountcountcountCount— number of expected instances.
resourcePropertiesCountIs(type: string, props: any, count: number): voiddef resource_properties_count_is(self, type: str, props: Any, count: int | float) -> Nonepublic void resourcePropertiesCountIs(String type, Object props, Number count)public void ResourcePropertiesCountIs(string type, object props, double count)func ResourcePropertiesCountIs(Type *string, Props interface{}, Count *float64)#3 parameters
Parameters
typetypetypetypeType— the resource type;propspropspropspropsProps— the 'Properties' section of the resource as should be expected in the template.countcountcountcountCount— number of expected instances.
templateMatches(expected: any): voiddef template_matches(self, expected: Any) -> Nonepublic void templateMatches(Object expected)public void TemplateMatches(object expected)func TemplateMatches(Expected interface{})#1 parameter
Parameters
expectedexpectedexpectedexpectedExpected— the expected CloudFormation template as key-value pairs.
toJSON(): { [key: string]: any }def to_json(self, ) -> typing.Mapping[str, Any]public Map<String, Object> toJSON()public IDictionary<string, object> ToJSON()func ToJSON() *map[string]interface{}#