AWS CDK S3 Construct Library (v1)

1.204.0 · deprecated · verified Thu Apr 16

The `aws-cdk-aws-s3` library provides L2 constructs for defining Amazon S3 buckets and related resources using the AWS Cloud Development Kit (CDK). This entry refers to CDK v1, which reached End-of-Support (EOS) on June 1, 2023. This package is no longer being updated. Users are strongly encouraged to migrate to AWS CDK v2 for continued support and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a basic S3 bucket using `aws-cdk.aws-s3` in a Python CDK application. It sets up a minimal stack and creates an S3 bucket with a globally unique name using the AWS account ID. Remember that `cdk.App().synth()` is used to generate the CloudFormation template. To deploy, you would run `cdk deploy` after bootstrapping your AWS environment.

import os
from aws_cdk import (  # type: ignore
    core as cdk,
    aws_s3 as s3
)

class MyS3Stack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        # Define an S3 bucket
        # For production, consider adding specific bucket_name, versioned, encryption, and public access settings
        # A removal_policy of RETAIN is the default for stateful resources like S3 buckets.
        # If you want the bucket and its contents to be deleted with the stack, use RemovalPolicy.DESTROY and auto_delete_objects=True (see warnings).
        s3.Bucket(self, "MyFirstS3Bucket",
            versioned=False,
            bucket_name=f"my-unique-bucket-{cdk.Aws.ACCOUNT_ID}", # Bucket names must be globally unique
            removal_policy=cdk.RemovalPolicy.RETAIN, # Default for stateful resources
            # auto_delete_objects=True # Use with caution and ONLY if removal_policy is DESTROY
        )

app = cdk.App()
MyS3Stack(app, "MyS3Stack")
app.synth()

view raw JSON →