So you learned about the new S3 bucket account regional namespace feature and you're thinking "this is amazing, let me try in CDK."
Well... This is where you'll learn that the L2 Bucket construct has not yet been updated to allow for this.
Currently there are the following items which are tracking the long term fix.
You should give both of these a 👍 to help them get prioritized and shipped.
Workaround
Good news there is a workaround! By using the L1 construct as an escape hatch we can then pass in bucketNamePrefix and bucketNamespace.
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
export class Cdk101Stack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// WORKAROUND: use the L1 bucket construct
new s3.CfnBucket(this, "ConfigBucket", {
bucketNamePrefix: "config",
bucketNamespace: "account-regional",
});
}
}
After deploying this you'll see your new bucket with {name}-{account}-{region}-an (the an suffix indicates account namespace).
That will work if you only need to deploy the bucket. But if you need an L2 reference (IBucket), use s3.Bucket.fromCfnBucket to convert it.
Full code demo:
import * as cdk from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
import * as path from 'path';
export class Cdk101Stack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// NOTE: workaround for the following CDK issues
// https://github.com/aws/aws-cdk/issues/37760
// https://github.com/aws/aws-cdk/pull/37386
const cfnBucket = new s3.CfnBucket(this, "ConfigBucket", {
bucketNamePrefix: "config",
bucketNamespace: "account-regional",
});
const bucket = s3.Bucket.fromCfnBucket(cfnBucket);
// Upload a config file to the bucket on deploy
new s3deploy.BucketDeployment(this, 'DeployConfig', {
sources: [s3deploy.Source.asset(path.join(__dirname, '../config'))],
destinationBucket: bucket,
});
}
}
Hope this blog helped you get a workaround for now and once aws-cdk-lib is patched I'll update this blog.
Happy coding 😃!
Follow AWS for more articles like this

Top comments (0)