Skip to main content
In this lesson you’ll learn how to give an Amazon S3 bucket a custom name when creating it with an AWS CloudFormation template. By default CloudFormation generates a unique name for S3 buckets unless you explicitly set the BucketName property in the resource’s Properties section. Why this matters:
  • A custom bucket name can make resources easier to identify across environments.
  • S3 bucket names must be globally unique, so plan names carefully.
  • Renaming a bucket in a template often triggers replacement of the resource during stack updates.
Example — set a specific bucket name
Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: eden-kodekloud-xcvt-bkt
What each part means:
  • Resources: top-level mapping that contains all resources in the template.
  • MyS3Bucket: logical name used inside the template.
  • Type: AWS resource type (AWS::S3::Bucket).
  • Properties: resource-specific configuration.
  • BucketName: the explicit S3 bucket name you want CloudFormation to create.
Resource elementPurposeExample
ResourcesContainer for all resource declarationsResources:
Logical IDTemplate-local name for the resourceMyS3Bucket
TypeAWS resource type identifierAWS::S3::Bucket
PropertiesSettings and options for the resourceProperties:
BucketNameThe explicit name for the S3 bucket (must be globally unique)BucketName: my-app-bucket-prod
S3 bucket names must be globally unique and follow S3 naming rules (3–63 characters, lowercase letters, numbers, and hyphens). If you omit BucketName, CloudFormation generates a unique name for you. For more on naming rules see the AWS S3 documentation.
Best practices and considerations
  • Use predictable naming conventions scoped by environment (for example, myapp-prod-bucket) to make lifecycle operations easier.
  • To avoid accidental deletion during stack updates or deletions, consider adding a DeletionPolicy: Retain to the bucket resource so CloudFormation will not delete the bucket automatically:
Resources:
  MyS3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: eden-kodekloud-xcvt-bkt
    DeletionPolicy: Retain
  • If you plan to manage bucket contents programmatically, remember CloudFormation does not empty a bucket before attempting deletion. Deleting a non-empty bucket fails and can cause stack updates to roll back.
Changing BucketName in a template typically causes CloudFormation to create a new bucket and attempt to delete the old one. If the new name is already owned by another account, the update will fail. Also, CloudFormation will not empty buckets for you — deletion of a bucket containing objects will fail. Plan carefully and use DeletionPolicy: Retain or other safeguards when renaming buckets.
Quick checklist before giving an explicit BucketName:
  • Confirm the desired name is available and follows S3 naming rules.
  • Decide whether the bucket should be retained on stack deletion (DeletionPolicy: Retain).
  • If renaming, ensure downstream references (applications, IAM policies, DNS, etc.) are updated.
  • Consider using environment-specific prefixes or account IDs to reduce naming collisions.
Links and references That’s all you need to specify a custom S3 bucket name in your CloudFormation template.

Watch Video