Error: Bucket already exists when redeployed


#1

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


#2

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.


#3

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.


#4

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


#5

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.


#6

you need to name the resource correctly

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


#7

@amitm02 Follow the example in the Serverless Framework docs.