How do I add an S3 trigger event to an AWS Lambda function using a serverless framework?

I want to add a trigger event to a Lambda function in an already existing bucket and for this I am using the following configuration:

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

      

where the bucket serverlesstest

already exists on S3.

These configurations throw an error:

An error occured while preparing your stack: S3BucketServerlesstest - serverlesstest already exists.

How can I resolve this error using Serverless Framework?

+7


source to share


4 answers


This is currently not possible in the underlying framework due to CloudFormation behavior. may be.

But you can use this plugin.

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

After installing serverless-plugin-existing-s3 from npm install serverless-plugin-existing-s3

.

And add plugins to serverless.yml

plugins:
  serverless-plugin-existing-s3

      

Give deployment permission to access the recycle bin.



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

      

And use an event existingS3

, it's not easy s3

.

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

      

After the command, sls deploy

you can attach the event using the command sls s3deploy

.

Feature of the offer

this will be added sometime in the future.

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

+9


source


Unfortunately, you cannot specify an existing S3 bucket to run a Lambda function because a serverless platform * cannot modify existing infrastructure using Cloud Formation. This configuration requires a new bucket to be created.

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



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

+4


source


If the bucket was created using Serverless elsewhere in the stack, you can use - s3: Bucket: { Ref: serverlesstest }

Otherwise you will have to create a name or ARN yourself.

+1


source


This is possible since serverless v1.47.0 by adding a flag existing: true

to your event configuration: https://serverless.com/framework/docs/providers/aws/events/s3/

example from 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 warnings:

IMPORTANT: You can only attach 1 existing S3 segment to each function.

NOTE. Using an existing config will add an additional lambda function and an IAM role to your stack. The Lambda function backs up an S3 user resource that is used to support existing S3 buckets.

0


source







All Articles