Sub topic 7
Tokens: values that exist only at deploy time
This chapter assumes you can define a bucket and a Lambda function in a stack, as in the earlier chapters. It explains why some values on those constructs cannot be read as plain strings.
What a token is
When you create a bucket without setting a name, CloudFormation picks the name at deploy time. Your code runs before that, so bucket.bucketName cannot hold the real name. The CDK returns a token instead. A token is a placeholder that the CDK replaces with a real value later.
During synthesis, the CDK turns each token into a CloudFormation expression such as { "Ref": "UploadsBucket..." }. CloudFormation then fills in the real name during deployment.
Seeing a token
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class TokenDemoStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const bucket = new s3.Bucket(this, 'UploadsBucket');
console.log(bucket.bucketName);
console.log(cdk.Token.isUnresolved(bucket.bucketName));
new cdk.CfnOutput(this, 'ReportsArn', {
value: cdk.Fn.join('/', [bucket.bucketArn, 'reports']),
});
new cdk.CfnOutput(this, 'SiteUrl', {
value: `https://${bucket.bucketName}.s3.amazonaws.com`,
});
}
}
const app = new cdk.App();
new TokenDemoStack(app, 'TokenDemoStack');The first console.log prints text like ${Token[TOKEN.123]}, and the second prints true. The exact number varies. Both outputs deploy correctly. The template literal is safe because the CDK recognizes the token inside the string and emits an Fn::Join.
The Python version uses core.Token.is_unresolved(bucket.bucket_name) from aws_cdk, and the behavior is the same.
Common errors
Tokens look like strings, so it is easy to treat them as strings. Do not do any of these with a token:
- Compare it, as in
if (bucket.bucketName === 'logs'). The condition is always false. - Change it, as in
bucket.bucketName.toUpperCase(). You get a mangled placeholder, not the name. - Use it as a construct ID or a map key. Construct IDs must be known at synth time.
- Parse it, for example by splitting an ARN. Use
cdk.Fn.selectandcdk.Fn.split, orcdk.Stack.of(this).splitArn, so the work happens in CloudFormation.
Choosing the right tool
| Need | Use |
|---|---|
| Join pieces into one value | Template literal or `Fn.join` |
| Check whether a value is a token | `Token.isUnresolved` |
| Compute a value at the end of synthesis | `Lazy.string` |
| Pick a piece out of a token | `Fn.select`, `Fn.split` |
Values you set yourself, such as versioned: true or a literal bucket name, are not tokens. You can branch on them freely.
Exercise
Why does if (bucket.bucketName === 'uploads') never behave as expected when the bucket name is generated?
Show answer
The generated name is a token, a placeholder such as ${Token[TOKEN.123]}, not the real name. The real name does not exist until CloudFormation deploys the stack, so the comparison runs against the placeholder text and is always false.
Exercise
How can you build the string <bucket ARN>/reports safely from a token?
Show answer
Use cdk.Fn.join('/', [bucket.bucketArn, 'reports']), or a template literal. Both produce a CloudFormation join that is resolved at deploy time.