13

I want to add trigger event on a Lambda function on an already existing bucket and for that I am using below configuration:

 events:
      - s3:
          bucket: serverlesstest
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .pdf

where bucket serverlesstest is already existing on S3.

This configurations is throwing the error:

An error occurred while provisioning your stack: S3BucketServerlesstest - serverlesstest already exists.

How can I resolve this error using Serverless Framework?

5 Answers 5

13

It’s not currently possible in the core framework because of CloudFormation behavior. maybe.

But you can use this plugin.

https://github.com/matt-filion/serverless-external-s3-event

After installing serverless-plugin-existing-s3 by npm install serverless-plugin-existing-s3.

And add plugins to serverless.yml

plugins:
  serverless-plugin-existing-s3

Give your deploy permission to access the bucket.

provider:
  name: aws
  runtime: nodejs4.3
  iamRoleStatements:
    ...
    -  Effect: "Allow"
       Action:
         - "s3:PutBucketNotification"
       Resource:
         Fn::Join:
           - ""
       - - "arn:aws:s3:::BUCKET_NAME or *"

And use existingS3 event, it is not just s3.

functions:
  someFunction:
    handler: index.handler
    events:
      - existingS3:
          bucket: BUCKET_NAME
          events:
            - s3:ObjectCreated:*
          rules:
            - prefix: images/
            - suffix: .jpg

After sls deploy command, You can attach event by using sls s3deploy command.

Feature Proposal

it will be added someday in the future.

https://github.com/serverless/serverless/issues/4241

Sign up to request clarification or add additional context in comments.

1 Comment

NOTE: This property was added in version 1.47.0. Older versions don't support this feature. (source: serverless.com/framework/docs/providers/aws/events/…)
13

This is possible as of serverless version v1.47.0, by adding the existing: true flag to your event configuration: https://serverless.com/framework/docs/providers/aws/events/s3/

example from the source:

functions:
  users:
    handler: users.handler
    events:
      - s3:
          bucket: legacy-photos
          event: s3:ObjectCreated:*
          rules:
            - prefix: uploads/
            - suffix: .jpg
          existing: true # <- this makes it work with existing objects

The source provides the following caveats:

IMPORTANT: You can only attach 1 existing S3 bucket per function.

NOTE: Using the existing config will add an additional Lambda function and IAM Role to your stack. The Lambda function backs-up the Custom S3 Resource which is used to support existing S3 buckets.

4 Comments

It does nothing. I'm on Serverless 1.5.0 and it completely ignore this property. I literally copy-pasted the code from the docs with a few renames. Completely broken feature.
I'm having the same problem on 1.58.0 (latest at the time of writing). I had to manually create the event trigger. I really wished this would work...
@DemonGyro @garryp Note that this functionality creates an additional lambda function yourservicenameandstage-custom-resource-existing-s3. If this additional lambda function is not created try checking the indentation of parameters in - s3 tag. Note that parameters have 4 spaces instead of 2 spaces after the parent tag. I had a similar problem and this fixed it for me so hope it helps.
Thanks for the tip. I have decided to handle things a little differently, so I don't need this functionality anymore, but I will definitely keep the spacing in mind later if I try this again.
5

Unfortunately, you can't specify an existing S3 bucket to trigger the Lambda function because the Serverless Framework* can't change existing infrastructure using Cloud Formation. This configuration requires that you create a new bucket.

You can read more in the following issues that were open on GitHub:

* I would try to configure this trigger using AWS Console or the SDK instead of the Serverelss Framework.

Comments

3

If the bucket was created using Serverless elsewhere in the stack, then you could use - s3: Bucket: { Ref: serverlesstest } Otherwise you'll have to construct the name or ARN yourself.

1 Comment

Now I can able to upload function successfully but trigger event still missing on Lambda function.
3

serverless.yml seems to be very sensitive to spaces. For me this advice was helpful.

If config looks like this

functions:
  hello:
    handler: handler.main
    events:
      - s3: 
        bucket: codepipeline-us-east-1-213458767560
        event: s3:ObjectCreated:*
        rules:
          - prefix: test/MyAppBuild

you need to add 2 more spaces to the indent of bucket, event & rules:

functions:
  hello:
    handler: handler.main
    events:
      - s3: 
          bucket: codepipeline-us-east-1-213458767560
          event: s3:ObjectCreated:*
          rules:
            - prefix: test/MyAppBuild

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.