serverless: Can't subscribe to events of existing S3 bucket

This is a Bug Report

Description

When specifying an already existing S3 bucket (xxx) in a function event, I get this error on deploy:

- xxx already exists.

I could not find documentation on how to subscribe to events of an existing S3 bucket in 1.0 rc1.

This worked in 0.5.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 48
  • Comments: 48 (15 by maintainers)

Most upvoted comments

Its not possible to do this through Cloudformation. Basically you’re not able to change existing infrastructure through CF.

On a more fundamental level I think its also best practice to create/change configuration of a resource only in one place. While we will probably have the functionality to add events to existing infrastructure I don’t think its a good thing to do in general, as then you create very strong dependencies and expectations between different services, e.g. if you rename a piece of infrastructure or you want to rename the S3 bucket you would have to update every serverless service that references the bucket.

I’m not sure that it will be a good experience for users.

I’m going to close here as this functionality is not going to be resolved soon, but would be interested in getting your thought on how to integrate with existing infrastructure through Serverless.

@flomotlik I feel like we need this support, but for a different reason than most of the other commenters and original requestor. They were coming at it mostly from the angle of “I don’t want to / can’t / whatever / use CloudFormation.” I’m not coming at it from that angle.

For me, I want to be able to have one microservice (serverless service) that owns a bucket and performs operation X on the bucket, and another microservice (a seperate serverless service) that performs operation Y on the bucket. So the bucket is created in one service - but events on that bucket are subscribed to by multiple services.

It’s a similar concept to why I needed to create the https://github.com/silvermine/serverless-plugin-external-sns-events/ plugin - in Serverless if you subscribe to an SNS event, Serverless tries to create the SNS topic, but that’s not always desired.

I think in general Serverless will need to allow for subscribing to events of “existing” infrastructure - whether that’s (very unfortunately) manually created like some of the other commenters, or whether that’s cross-stack subscriptions.

Speaking of cross-stack subscriptions, maybe support could at least be added for cross-stack resource event subscriptions using Fn::ImportValue (as described here)?

There is a plugin for handling this https://github.com/matt-filion/serverless-external-s3-event It should do what you want @kennu

We are also moving more to the direction of multiple CloudFront stacks, so that everything belongs to a stack but not all stacks are managed by Serverless (we don’t want to delete data stacks when upgrading a service to 1.0 or 2.0). So I think that for new services, cross-stack event subscriptions are the thing.

In addition to that, there are of course many cases where buckets already exists outside CloudFormation (e.g. existing Kinesis Firehose or Elastic Transcoder setups) and it will be necessary to somehow subscribe to those events.

I am having the same issue. Unable to subscribe to existing S3 bucket.

I have check this plugin https://github.com/matt-filion/serverless-external-s3-event and it is very buggy.

Is their an alternative solution to this.

Warm regards, Javed Gardezi

Still having to manually set up the trigger myself. Tried deleting my entire stack and recreating and did not work 😦

Not sure that syntax for plugins is correct. Try this

plugins:
  - serverless-plugin-existing-s3

@velulev FWIW, the add-permission call can be replaced by the following configuration in serverless.yml

resources:
  Resources:
    LambdaInvokePermission:
      Type: AWS::Lambda::Permission
      Properties:
        Action: lambda:InvokeFunction
        FunctionName: service-stage-myLambdaFunctionName
        Principal: s3.amazonaws.com
        SourceArn: arn:aws:s3:::my-bucket

For others who are visiting this link, the plug-in seems to have been patched in the last few days, and I can confirm that it works for me, as of 29-Jan-2017

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

Follow the steps as mentioned in the github page, but you can only do step 2 after deployment of the function (unless your function already exists),

so the steps are,

  1. sls deploy

  2. aws lambda add-permission --function-name FUNCTION_NAME --region us-west-2 --statement-id ANY_ID --action “lambda:InvokeFunction” --principal s3.amazonaws.com --source-arn arn:aws:s3:::BUCKET_NAME --source-account YOUR_AWS_ACCOUNT_NUM

  3. sls s3deploy

No worries @pmuens

I have already opened the issue https://github.com/matt-filion/serverless-external-s3-event/issues/2 on the plugin project page.

Great to hear that @oyeanuj 👍

Here’s a comment / thread which could be helpful: https://github.com/serverless/serverless/issues/3257#issuecomment-310290685.

You could use smth. like this but leave out the S3 bucket resource and only use the S3 bucket permission resource in your resources section.

Let us know if you need anything else!