Skip to content

Intrinsic Functions

CloudFormation intrinsic functions are available as imports from the lexicon. They produce the corresponding Fn:: calls in the serialized template.

Here is a complete example using all intrinsic functions:

intrinsics.ts
import { Sub, AWS, Ref, GetAtt, If, Join, Select, Split, Base64, GetAZs } from "@intentius/chant-lexicon-aws";
// Sub — string substitution (tagged template literal)
export const bucketName = Sub`${AWS.StackName}-data`;
export const arn = Sub`arn:aws:s3:::${AWS.AccountId}:${AWS.Region}:*`;
// Ref — resource and parameter references
export const envRef = Ref("Environment");
// GetAtt — resource attributes
export const bucketArn = GetAtt("DataBucket", "Arn");
// If — conditional values
export const conditionalName = If("IsProduction", "prod-data", "dev-data");
// Join — join values with delimiter
export const joined = Join("-", ["prefix", AWS.StackName, "suffix"]);
// Select + Split — select by index from split string
export const first = Select(0, Split(",", "a,b,c"));
// Split — split string by delimiter
export const parts = Split(",", "a,b,c");
// Base64 — encode to Base64
export const userData = Base64(Sub`#!/bin/bash
echo "Stack: ${AWS.StackName}"
yum update -y
`);
// GetAZs — availability zones for a region
export const azs = GetAZs();

Tagged template literal that produces Fn::Sub. The most common intrinsic — use it for dynamic naming with pseudo-parameters and attribute references:

intrinsics-detail.ts
// --- Sub: string substitution ---
export const detailBucketName = Sub`${AWS.StackName}-data`;
export const detailArn = Sub`arn:aws:s3:::${AWS.AccountId}:${AWS.Region}:*`;

Sub is a tagged template — use it with backticks, not as a function call.

References a resource’s physical ID or a parameter’s value:

intrinsics-detail.ts
// --- Ref: resource and parameter references ---
// chant-disable-next-line COR003
export const detailEnvRef = Ref("Environment");

In most cases you don’t need Ref directly — the serializer automatically generates Ref when you reference an imported resource (e.g. dataBucket imported from another file).

Preferred: Use AttrRef directly via the resource’s typed properties. When you write dataBucket.arn (imported from the file that defines it), the serializer automatically emits Fn::GetAtt. Explicit GetAtt is only needed for dynamic or imported resource names.

Returns one of two values based on a condition:

intrinsics-detail.ts
// --- If: conditional values ---
export const value = If("IsProduction", "prod-value", "dev-value");

Use with AWS.NoValue to conditionally omit a property — see Conditions on the CloudFormation Concepts page.

Joins values with a delimiter:

intrinsics-detail.ts
// --- Join: join values ---
export const detailJoined = Join("-", ["prefix", AWS.StackName, "suffix"]);

Selects a value from a list by index:

intrinsics-detail.ts
// --- Select + Split ---
export const detailFirst = Select(0, Split(",", "a,b,c"));

Splits a string by a delimiter:

intrinsics-detail.ts
// --- Split: split string ---
export const detailParts = Split(",", "a,b,c");

Encodes a string to Base64, commonly used for EC2 user data:

intrinsics-detail.ts
// --- Base64: encode to Base64 ---
export const detailUserData = Base64(Sub`#!/bin/bash
echo "Stack: ${AWS.StackName}"
yum update -y
`);

Returns the list of Availability Zones for a region:

intrinsics-detail.ts
// --- GetAZs: availability zones ---
export const firstAz = Select(0, GetAZs(AWS.Region));
export const secondAz = Select(1, GetAZs(AWS.Region));