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:
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 referencesexport const envRef = Ref("Environment");
// GetAtt — resource attributesexport const bucketArn = GetAtt("DataBucket", "Arn");
// If — conditional valuesexport const conditionalName = If("IsProduction", "prod-data", "dev-data");
// Join — join values with delimiterexport const joined = Join("-", ["prefix", AWS.StackName, "suffix"]);
// Select + Split — select by index from split stringexport const first = Select(0, Split(",", "a,b,c"));
// Split — split string by delimiterexport const parts = Split(",", "a,b,c");
// Base64 — encode to Base64export const userData = Base64(Sub`#!/bin/bashecho "Stack: ${AWS.StackName}"yum update -y`);
// GetAZs — availability zones for a regionexport const azs = GetAZs();Sub — string substitution
Section titled “Sub — string substitution”Tagged template literal that produces Fn::Sub. The most common intrinsic — use it for dynamic naming with pseudo-parameters and attribute references:
// --- 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.
Ref — resource and parameter references
Section titled “Ref — resource and parameter references”References a resource’s physical ID or a parameter’s value:
// --- Ref: resource and parameter references ---// chant-disable-next-line COR003export 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).
GetAtt — resource attributes
Section titled “GetAtt — resource attributes”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.
If — conditional values
Section titled “If — conditional values”Returns one of two values based on a condition:
// --- 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.
Join — join values
Section titled “Join — join values”Joins values with a delimiter:
// --- Join: join values ---export const detailJoined = Join("-", ["prefix", AWS.StackName, "suffix"]);Select — select by index
Section titled “Select — select by index”Selects a value from a list by index:
// --- Select + Split ---export const detailFirst = Select(0, Split(",", "a,b,c"));Split — split string
Section titled “Split — split string”Splits a string by a delimiter:
// --- Split: split string ---export const detailParts = Split(",", "a,b,c");Base64 — encode to Base64
Section titled “Base64 — encode to Base64”Encodes a string to Base64, commonly used for EC2 user data:
// --- Base64: encode to Base64 ---export const detailUserData = Base64(Sub`#!/bin/bashecho "Stack: ${AWS.StackName}"yum update -y`);GetAZs — availability zones
Section titled “GetAZs — availability zones”Returns the list of Availability Zones for a region:
// --- GetAZs: availability zones ---export const firstAz = Select(0, GetAZs(AWS.Region));export const secondAz = Select(1, GetAZs(AWS.Region));