serverless: S3 event with Ref for bucket name causes error

This is a Bug Report

Description

  • What went wrong?

Tried creating an S3 event on a function, using a Ref object instead of a string for the bucket name. The object is treated like a string. The output contains a resource with logical name S3BucketObjectObject and BucketName [object Object].

  • What did you expect should have happened?

Ideally, the Ref to the resource should be recognized and the new configuration should be pushed onto that object. This behavior is supported if the bucket has already been created on a lambda function, but not if the bucket was created in the resource section.

While this is not supported, an error should be thrown if the bucket name is not a string.

  • What was the config you used?
service: test-buckets

provider:
  name: aws
  runtime: python2.7

resources:
  Resources:
    MyTopic:
      Type: AWS::SNS::Topic
      Properties:
        TopicName: ${opt:stage}-my-topic
    
    BarBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: ${opt:stage}-bar
        NotificationConfiguration:
          TopicConfigurations:
            - Event: "s3:ObjectCreated:*"
              Topic:
                Ref: MyTopic

functions:
  foo:
    handler: handler.foo
    events:
      - s3:
          bucket:
            Ref: BarBucket
          event: s3:ObjectCreated:*
          rules:
            - prefix: test/
  • What stacktrace or error message from your provider did you see?
 An error occurred while provisioning your stack: S3BucketObjectObject
     - Bucket name should not contain '['.

Similar or dependent issues:

Additional Data

  • Serverless Framework Version you’re using: 1.4.0
  • Operating System: macOS 10.12.1
  • Stack Trace:
  • Provider Error messages:

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 11
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

being able to refer to a bucket by ref would be great

It doesn’t seem to be fixed in v1.10.1, I’m facing a similar issue

Serverless: Packaging service...

  Type Error ---------------------------------------------

     name.replace is not a function

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

and in my serverless.yml I have:

provider:
  name: aws
  runtime: nodejs6.10
  stage: dev
  region: us-west-2
  iamRoleStatements:
    - Effect: Allow
      Action:
        - s3:*
      Resource: arn:aws:s3:::*

functions:
  s3eventlogger:
    handler: src/s3logger.logger
    events:
      - s3:
          bucket:
            Ref: applesbucket
          event: s3:ObjectCreated:*

resources:
  Resources:
    applesbucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: myawesomebucket-dev

when I replace

          bucket:
            Ref: applesbucket
          event: s3:ObjectCreated:*

by

          bucket: myawesomebucket-dev
          event: s3:ObjectCreated:*

it throws the error during deployment:

Serverless: Deployment failed!

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

     An error occurred while provisioning your stack: S3Buckettpldev
     - myawesomebucket-dev already exists in stack arn:aws:cloudformation:us-west-2:xxx:stack/tpl-dev/99566700-1546-11e7xxx.

Has there been a fix for this yet? Or another way to use bucket resource refs in the lambda trigger?

Everyone just uses an external plugin for that: https://github.com/matt-filion/serverless-external-s3-event

  preSignUp:
    handler: src/user/userService.preSignUp
    events:
      - cognitoUserPool:
          pool:
            - Ref: UserPool
          trigger: PreSignUp

The same issue, but I’m trying to reference a Cognito pool resource.