aws-cdk-lib.Fn

class Fn

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

CloudFormation intrinsic functions.

http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference.html

Example

declare const cfnTemplate: cfn_inc.CfnInclude;
const rule: core.CfnRule = cfnTemplate.getRule('MyRule');

// mutating the rule
declare const myParameter: core.CfnParameter;
rule.addAssertion(core.Fn.conditionContains(['m1.small'], myParameter.valueAsString),
  'MyParameter has to be m1.small');

Methods

NameDescription
static base64(data)The intrinsic function Fn::Base64 returns the Base64 representation of the input string.
static cidr(ipBlock, count, sizeMask?)The intrinsic function Fn::Cidr returns the specified Cidr address block.
static conditionAnd(...conditions)Returns true if all the specified conditions evaluate to true, or returns false if any one of the conditions evaluates to false.
static conditionContains(listOfStrings, value)Returns true if a specified string matches at least one value in a list of strings.
static conditionEachMemberEquals(listOfStrings, value)Returns true if a specified string matches all values in a list.
static conditionEachMemberIn(stringsToCheck, stringsToMatch)Returns true if each member in a list of strings matches at least one value in a second list of strings.
static conditionEquals(lhs, rhs)Compares if two values are equal.
static conditionIf(conditionId, valueIfTrue, valueIfFalse)Returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false.
static conditionNot(condition)Returns true for a condition that evaluates to false or returns false for a condition that evaluates to true.
static conditionOr(...conditions)Returns true if any one of the specified conditions evaluate to true, or returns false if all of the conditions evaluates to false.
static findInMap(mapName, topLevelKey, secondLevelKey)The intrinsic function Fn::FindInMap returns the value corresponding to keys in a two-level map that is declared in the Mappings section.
static getAtt(logicalNameOfResource, attributeName)The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template.
static getAzs(region?)The intrinsic function Fn::GetAZs returns an array that lists Availability Zones for a specified region.
static importListValue(sharedValueToImport, assumedLength, delimiter?)Like Fn.importValue, but import a list with a known length.
static importValue(sharedValueToImport)The intrinsic function Fn::ImportValue returns the value of an output exported by another stack.
static join(delimiter, listOfValues)The intrinsic function Fn::Join appends a set of values into a single value, separated by the specified delimiter.
static len(array)The intrinsic function Fn::Length returns the number of elements within an array or an intrinsic function that returns an array.
static parseDomainName(url)Given an url, parse the domain name.
static ref(logicalName)The Ref intrinsic function returns the value of the specified parameter or resource.
static refAll(parameterType)Returns all values for a specified parameter type.
static select(index, array)The intrinsic function Fn::Select returns a single object from a list of objects by index.
static split(delimiter, source, assumedLength?)Split a string token into a token list of string values.
static sub(body, variables?)The intrinsic function Fn::Sub substitutes variables in an input string with values that you specify.
static toJsonString(object)The Fn::ToJsonString intrinsic function converts an object or array to its corresponding JSON string.
static transform(macroName, parameters)Creates a token representing the Fn::Transform expression.
static valueOf(parameterOrLogicalId, attribute)Returns an attribute value or list of values for a specific parameter and attribute.
static valueOfAll(parameterType, attribute)Returns a list of all attribute values for a given parameter type and attribute.

static base64(data)

public static base64(data: string): string

Parameters

  • data string — The string value you want to convert to Base64.

Returns

  • string

The intrinsic function Fn::Base64 returns the Base64 representation of the input string.

This function is typically used to pass encoded data to Amazon EC2 instances by way of the UserData property.


static cidr(ipBlock, count, sizeMask?)

public static cidr(ipBlock: string, count: number, sizeMask?: string): string[]

Parameters

  • ipBlock string — The user-specified default Cidr address block.
  • count number — The number of subnets' Cidr block wanted.
  • sizeMask string — The digit covered in the subnet.

Returns

  • string[]

The intrinsic function Fn::Cidr returns the specified Cidr address block.


static conditionAnd(...conditions)

public static conditionAnd(...conditions: ICfnConditionExpression[]): ICfnRuleConditionExpression

Parameters

  • conditions ICfnConditionExpression — conditions to AND.

Returns

  • ICfnRuleConditionExpression

Returns true if all the specified conditions evaluate to true, or returns false if any one of the conditions evaluates to false.

Fn::And acts as an AND operator. The minimum number of conditions that you can include is


static conditionContains(listOfStrings, value)

public static conditionContains(listOfStrings: string[], value: string): ICfnRuleConditionExpression

Parameters

  • listOfStrings string[] — A list of strings, such as "A", "B", "C".
  • value string — A string, such as "A", that you want to compare against a list of strings.

Returns

  • ICfnRuleConditionExpression

Returns true if a specified string matches at least one value in a list of strings.


static conditionEachMemberEquals(listOfStrings, value)

public static conditionEachMemberEquals(listOfStrings: string[], value: string): ICfnRuleConditionExpression

Parameters

  • listOfStrings string[] — A list of strings, such as "A", "B", "C".
  • value string — A string, such as "A", that you want to compare against a list of strings.

Returns

  • ICfnRuleConditionExpression

Returns true if a specified string matches all values in a list.


static conditionEachMemberIn(stringsToCheck, stringsToMatch)

public static conditionEachMemberIn(stringsToCheck: string[], stringsToMatch: string[]): ICfnRuleConditionExpression

Parameters

  • stringsToCheck string[] — A list of strings, such as "A", "B", "C".
  • stringsToMatch string[] — A list of strings, such as "A", "B", "C".

Returns

  • ICfnRuleConditionExpression

Returns true if each member in a list of strings matches at least one value in a second list of strings.


static conditionEquals(lhs, rhs)

public static conditionEquals(lhs: any, rhs: any): ICfnRuleConditionExpression

Parameters

  • lhs any — A value of any type that you want to compare.
  • rhs any — A value of any type that you want to compare.

Returns

  • ICfnRuleConditionExpression

Compares if two values are equal.

Returns true if the two values are equal or false if they aren't.


static conditionIf(conditionId, valueIfTrue, valueIfFalse)

public static conditionIf(conditionId: string, valueIfTrue: any, valueIfFalse: any): ICfnRuleConditionExpression

Parameters

  • conditionId string — A reference to a condition in the Conditions section.
  • valueIfTrue any — A value to be returned if the specified condition evaluates to true.
  • valueIfFalse any — A value to be returned if the specified condition evaluates to false.

Returns

  • ICfnRuleConditionExpression

Returns one value if the specified condition evaluates to true and another value if the specified condition evaluates to false.

Currently, AWS CloudFormation supports the Fn::If intrinsic function in the metadata attribute, update policy attribute, and property values in the Resources section and Outputs sections of a template. You can use the AWS::NoValue pseudo parameter as a return value to remove the corresponding property.


static conditionNot(condition)

public static conditionNot(condition: ICfnConditionExpression): ICfnRuleConditionExpression

Parameters

  • condition ICfnConditionExpression — A condition such as Fn::Equals that evaluates to true or false.

Returns

  • ICfnRuleConditionExpression

Returns true for a condition that evaluates to false or returns false for a condition that evaluates to true.

Fn::Not acts as a NOT operator.


static conditionOr(...conditions)

public static conditionOr(...conditions: ICfnConditionExpression[]): ICfnRuleConditionExpression

Parameters

  • conditions ICfnConditionExpression — conditions that evaluates to true or false.

Returns

  • ICfnRuleConditionExpression

Returns true if any one of the specified conditions evaluate to true, or returns false if all of the conditions evaluates to false.

Fn::Or acts as an OR operator. The minimum number of conditions that you can include is


static findInMap(mapName, topLevelKey, secondLevelKey)

public static findInMap(mapName: string, topLevelKey: string, secondLevelKey: string): string

Parameters

  • mapName string
  • topLevelKey string
  • secondLevelKey string

Returns

  • string

The intrinsic function Fn::FindInMap returns the value corresponding to keys in a two-level map that is declared in the Mappings section.


static getAtt(logicalNameOfResource, attributeName)

public static getAtt(logicalNameOfResource: string, attributeName: string): IResolvable

Parameters

  • logicalNameOfResource string — The logical name (also called logical ID) of the resource that contains the attribute that you want.
  • attributeName string — The name of the resource-specific attribute whose value you want.

Returns

  • IResolvable

The Fn::GetAtt intrinsic function returns the value of an attribute from a resource in the template.


static getAzs(region?)

public static getAzs(region?: string): string[]

Parameters

  • region string — The name of the region for which you want to get the Availability Zones.

Returns

  • string[]

The intrinsic function Fn::GetAZs returns an array that lists Availability Zones for a specified region.

Because customers have access to different Availability Zones, the intrinsic function Fn::GetAZs enables template authors to write templates that adapt to the calling user's access. That way you don't have to hard-code a full list of Availability Zones for a specified region.


static importListValue(sharedValueToImport, assumedLength, delimiter?)

public static importListValue(sharedValueToImport: string, assumedLength: number, delimiter?: string): string[]

Parameters

  • sharedValueToImport string
  • assumedLength number
  • delimiter string

Returns

  • string[]

Like Fn.importValue, but import a list with a known length.

If you explicitly want a list with an unknown length, call Fn.split(',', Fn.importValue(exportName)). See the documentation of Fn.split to read more about the limitations of using lists of unknown length.

Fn.importListValue(exportName, assumedLength) is the same as Fn.split(',', Fn.importValue(exportName), assumedLength), but easier to read and impossible to forget to pass assumedLength.


static importValue(sharedValueToImport)

public static importValue(sharedValueToImport: string): string

Parameters

  • sharedValueToImport string — The stack output value that you want to import.

Returns

  • string

The intrinsic function Fn::ImportValue returns the value of an output exported by another stack.

You typically use this function to create cross-stack references. In the following example template snippets, Stack A exports VPC security group values and Stack B imports them.


static join(delimiter, listOfValues)

public static join(delimiter: string, listOfValues: string[]): string

Parameters

  • delimiter string — The value you want to occur between fragments.
  • listOfValues string[] — The list of values you want combined.

Returns

  • string

The intrinsic function Fn::Join appends a set of values into a single value, separated by the specified delimiter.

If a delimiter is the empty string, the set of values are concatenated with no delimiter.


static len(array)

public static len(array: any): number

Parameters

  • array any — The array you want to return the number of elements from.

Returns

  • number

The intrinsic function Fn::Length returns the number of elements within an array or an intrinsic function that returns an array.


static parseDomainName(url)

public static parseDomainName(url: string): string

Parameters

  • url string — the url to parse.

Returns

  • string

Given an url, parse the domain name.


static ref(logicalName)

public static ref(logicalName: string): string

Parameters

  • logicalName string — The logical name of a parameter/resource for which you want to retrieve its value.

Returns

  • string

The Ref intrinsic function returns the value of the specified parameter or resource.

Note that it doesn't validate the logicalName, it mainly serves parameter/resource reference defined in a CfnInclude template.


static refAll(parameterType)

public static refAll(parameterType: string): string[]

Parameters

  • parameterType string — An AWS-specific parameter type, such as AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id.

Returns

  • string[]

Returns all values for a specified parameter type.


static select(index, array)

public static select(index: number, array: string[]): string

Parameters

  • index number — The index of the object to retrieve.
  • array string[] — The list of objects to select from.

Returns

  • string

The intrinsic function Fn::Select returns a single object from a list of objects by index.


static split(delimiter, source, assumedLength?)

public static split(delimiter: string, source: string, assumedLength?: number): string[]

Parameters

  • delimiter string — A string value that determines where the source string is divided.
  • source string — The string value that you want to split.
  • assumedLength number — The length of the list that will be produced by splitting.

Returns

  • string[]

Split a string token into a token list of string values.

Specify the location of splits with a delimiter such as ',' (a comma). Renders to the Fn::Split intrinsic function.

Lists with unknown lengths (default)

Since this function is used to work with deploy-time values, if assumedLength is not given the CDK cannot know the length of the resulting list at synthesis time. This brings the following restrictions:

  • You must use Fn.select(i, list) to pick elements out of the list (you must not use list[i]).
  • You cannot add elements to the list, remove elements from the list, combine two such lists together, or take a slice of the list.
  • You cannot pass the list to constructs that do any of the above.

The only valid operation with such a tokenized list is to pass it unmodified to a CloudFormation Resource construct.

Lists with assumed lengths

Pass assumedLength if you know the length of the list that will be produced by splitting. The actual list length at deploy time may be longer than the number you pass, but not shorter.

The returned list will look like:

[Fn.select(0, split), Fn.select(1, split), Fn.select(2, split), ...]

The restrictions from the section "Lists with unknown lengths" will now be lifted, at the expense of having to know and fix the length of the list.


static sub(body, variables?)

public static sub(body: string, variables?: { [string]: string }): string

Parameters

  • body string — A string with variables that AWS CloudFormation substitutes with their associated values at runtime.
  • variables { [string]: string } — The name of a variable that you included in the String parameter.

Returns

  • string

The intrinsic function Fn::Sub substitutes variables in an input string with values that you specify.

In your templates, you can use this function to construct commands or outputs that include values that aren't available until you create or update a stack.


static toJsonString(object)

public static toJsonString(object: any): string

Parameters

  • object any — The object or array to stringify.

Returns

  • string

The Fn::ToJsonString intrinsic function converts an object or array to its corresponding JSON string.


static transform(macroName, parameters)

public static transform(macroName: string, parameters: { [string]: any }): IResolvable

Parameters

  • macroName string — The name of the macro to perform the processing.
  • parameters { [string]: any } — The parameters to be passed to the macro.

Returns

  • IResolvable

Creates a token representing the Fn::Transform expression.

See also: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-transform.html


static valueOf(parameterOrLogicalId, attribute)

public static valueOf(parameterOrLogicalId: string, attribute: string): string

Parameters

  • parameterOrLogicalId string — The name of a parameter for which you want to retrieve attribute values.
  • attribute string — The name of an attribute from which you want to retrieve a value.

Returns

  • string

Returns an attribute value or list of values for a specific parameter and attribute.


static valueOfAll(parameterType, attribute)

public static valueOfAll(parameterType: string, attribute: string): string[]

Parameters

  • parameterType string — An AWS-specific parameter type, such as AWS::EC2::SecurityGroup::Id or AWS::EC2::VPC::Id.
  • attribute string — The name of an attribute from which you want to retrieve a value.

Returns

  • string[]

Returns a list of all attribute values for a given parameter type and attribute.