Skip to content

aws-s3: Unable to use Bucket.fromBucketName on legacy s3 bucket with underscore in the name #22640

@NoahCarnahan

Description

@NoahCarnahan

Describe the bug

I'm attempting to reference an s3 bucket in my cdk code that was created years ago outside of cdk. The bucket has an underscore in its name, which was legal for buckets before March 1, 2018 (new buckets can't have underscores in their names). I've tried referencing the bucket with both Bucket.fromBucketName and Bucket.fromBucketAttributes, but both throw this error:

jsii.errors.JSIIError: Invalid S3 bucket name (value: xxxx_xxxxxx)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 4)

(note that I've censored the actual bucket name for privacy)

Also note that I'm using the python version of the cdk library. Is there any way I can reference this bucket from CDK?

Expected Behavior

I expect to be able to import the bucket.

Current Behavior

Error message is:

jsii.errors.JSIIError: Invalid S3 bucket name (value: xxxx_xxxxxx)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 4)

Stack trace (paths modified for privacy)

  File "<project root>/.venv/lib/python3.9/site-packages/aws_cdk/aws_s3/__init__.py", line 16101, in from_bucket_attributes
    return typing.cast(IBucket, jsii.sinvoke(cls, "fromBucketAttributes", [scope, id, attrs]))
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 148, in wrapped
    return _recursize_dereference(kernel, fn(kernel, *args, **kwargs))
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/__init__.py", line 405, in sinvoke
    response = self.provider.sinvoke(
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 365, in sinvoke
    return self._process.send(request, InvokeResponse)
  File "<project root>/.venv/lib/python3.9/site-packages/jsii/_kernel/providers/process.py", line 329, in send
    raise JSIIError(resp.error) from JavaScriptError(resp.stack)
jsii.errors.JSIIError: Invalid S3 bucket name (value: xxxx_xxxxxx)
Bucket name must only contain lowercase characters and the symbols, period (.) and dash (-) (offset: 4)

Reproduction Steps

from aws_cdk import aws_s3 as s3

bucket = s3.Bucket.from_bucket_attributes(
    self,
    "foo-bar",
    account="myAccount",
    bucket_arn="arn:aws:s3:::foo_bar"
)

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.46.0 (build 5a0595e)

Framework Version

No response

Node.js Version

v18.0.0

OS

macOS 12.6

Language

Python

Language Version

Python 3.9.12

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-s3Related to Amazon S3bugThis issue is a bug.effort/smallSmall work item – less than a day of effortp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions