Error: Bucket already exists when redeployed

Hi

severless / AWS noob here.

I have started with a simple version of a function (hello) which stores some data in an s3 bucket. This all worked fine.

Next I added a function that I wanted to get triggered whenever a s3 object is stored. The resulting severless.yaml looks like this:

service: aws-nodejs

provider:
  name: aws
  runtime: nodejs6.10

  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "s3:*"
      Resource: "arn:aws:s3:::test-s3bucket2/*"

functions:
  hello:
    handler: handler.hello
  stored:
    handler: handler.stored
    events:
      - s3: test-s3bucket2

resources:
  Resources:
    NewResource:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: test-s3bucket2

Now after I deploy it again I get an error that the bucket already exists:

$ sls deploy -s dev
Serverless: Packaging service...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (598 B)...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
....................Serverless: Deployment failed!

  Serverless Error ---------------------------------------

     An error occurred while provisioning your stack: S3BucketTests3bucket2
     - test-s3bucket2 already exists in stack arn:aws:cloudformation:us-east-1:339468856116:stack/aws-nodejs-dev/498e8ca0-4c6b-11e7-9da6-50fae98b1835.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Forums:        forum.serverless.com
     Chat:          gitter.im/serverless/serverless

  Your Environment Information -----------------------------
     OS:                 darwin
     Node Version:       7.10.0
     Serverless Version: 1.13.2

This brings me to some questions:

  • What does the error really say / mean? Removing the resource section from the yaml didn’t help.
  • How are (s3) resources handled? What is the proposed way of working with these?
  • How is cloudformation handling these? Are these calls not idempotent?

Thanks
Matthias

1 Like

So deleting the bucket manually and other resources via sls remove worked. But after adding a third test function and trying to redeploy I got the same error.

In your handler, you are already creating the resource, so remove the resource

  Resources:
    NewResource:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: test-s3bucket2

will work.

I have the same issue. What do you mean by “In your handler, you are already creating the resource”?

If you use the syntax

events:
      - s3: photos

[/quote]

Then the s3 bucket is automatically created and you don’t need to define it in the resources section.

If you do want to define it in the resources section then you need to name the resource correctly so Serverless knows to override its own definition otherwise you end up trying to create two buckets with the same name.

1 Like

you need to name the resource correctly

what would be the correct way to name it both in the event and the resources section?

@amitm02 Follow the example in the Serverless Framework docs.

1 Like

Hi , what about if i don’t want to recreated the bucket but to use the already exists bucket

1 Like

Then have a look at https://github.com/mayconbordin/serverless-plugin-existing-s3

2 Likes

Hi,
in my handler, i’m using s3 as an event, but i have a problem to enable versioning of s3. im trying to do in this way, but the version is not going to enable

events:
- s3:
bucket: test
VersioningConfiguration: Enabled
event: s3:ObjectCreated:*
rules:
- suffix: .jpg

Just add existing: true

events:
- s3:
bucket: test
VersioningConfiguration: Enabled
event: s3:ObjectCreated:*
existing: true
rules:
- suffix: .jpg

1 Like