Working with AWS CDK Token Values
Bhuvesh Dhiman
Software Engineer @Appfire | Backend & Cloud enthusiast | Delivering Quality Solutions & Simplifying Complex Problems
In AWS CDK, Token values represent placeholders for data only resolved at deployment time, such as ARNs or resource names. Sometimes, you need to extract specific details from these tokens. AWS CDK provides helper functions like Fn.split and Fn.select to manipulate these values.
Splitting Token Values with Fn.split
Fn.split breaks a string into an array using a delimiter. For example, you can split an ARN to extract parts like the resource name:
const splitArn = Fn.split(':', bucket.bucketArn, assumedLength);
This splits the ARN into components, like arn, aws, s3, and the bucket name.
Selecting Elements with Fn.select
Fn.select retrieves a specific element from an array. After splitting the ARN, you can extract the bucket name:
const bucketName = Fn.select(5, splitArn);
This selects the 6th element (index 5) from the split ARN, the bucket name.
Example: Extracting a Bucket Name
Here’s how you can combine Fn.split and Fn.select in your CDK stack:
const bucketArn = bucket.bucketArn;
const splitArn = Fn.split(':', bucketArn, assumedLength);
const bucketName = Fn.select(5, splitArn);
This approach helps you extract and use parts of token values, like ARNs, in your AWS CDK applications.
For more details, check out: