Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploying S3 bucket with SNS notification event not possible #4797

Open
ffxsam opened this issue Mar 3, 2018 · 5 comments
Open

Deploying S3 bucket with SNS notification event not possible #4797

ffxsam opened this issue Mar 3, 2018 · 5 comments

Comments

@ffxsam
Copy link
Contributor

ffxsam commented Mar 3, 2018

This is a Bug Report

Description

For bug reports:

  • What went wrong?

It's not possible to deploy a stack with an S3 bucket that has an SNS topic as an event notification. Is something missing in the CloudFormation file generated by Serverless? According to this AWS support article, it seems an AWS::SNS::TopicPolicy is needed, and this is not generated by Serverless:
https://aws.amazon.com/premiumsupport/knowledge-center/unable-validate-destination-s3/

  • What did you expect should have happened?

The stack should deploy without errors.

  • What was the config you used?

See bottom for serverless.yml file.

  • What stacktrace or error message from your provider did you see?

Additional Data

  • Serverless Framework Version you're using: 1.26.1
  • Operating System: macOS 10.13.3
  • Stack Trace:
  • Provider Error messages:
  Serverless Error ---------------------------------------

  An error occurred: TestBucket - Unable to validate the following destination configurations.

serverless.yaml:

service:
  name: TestStack

# Add the serverless-webpack plugin
plugins:
  - serverless-webpack

provider:
  name: aws
  runtime: nodejs6.10
  variableSyntax: '\$\(([\s\S]+?)\)'

custom:
  stage: $(opt:stage, self:provider.stage)
  topicName: kerpow
  topic: $(self:service)-$(self:custom.topicName)-$(self:custom.stage)
  topicArn:
    Fn::Sub: arn:aws:sns:$(self:provider.region):${AWS::AccountId}:$(self:custom.topic)

functions:
  first:
    handler: first.hello
    events:
      - sns: $(self:custom.topic)

resources:
  Resources:
    TestBucket:
      Type: AWS::S3::Bucket
      DependsOn: SNSTopic$(self:service)$(self:custom.topicName)$(self:custom.stage)
      Properties:
        BucketName: aa-test-95872017
        NotificationConfiguration:
          TopicConfigurations:
            - Event: s3:ObjectCreated:Put
              Filter:
                S3Key:
                  Rules:
                    - Name: prefix
                      Value: staging/
              Topic: $(self:custom.topicArn)
@ffxsam
Copy link
Contributor Author

ffxsam commented Mar 3, 2018

This template works. It's a bit messier than I'd like. IMO Serverless should handle this automatically.

service:
  name: TestStack

# Add the serverless-webpack plugin
plugins:
  - serverless-webpack

provider:
  name: aws
  runtime: nodejs6.10
  variableSyntax: '\$\(([\s\S]+?)\)'

custom:
  stage: $(opt:stage, self:provider.stage)
  topicName: kerpow
  topic: $(self:service)-$(self:custom.topicName)-$(self:custom.stage)
  topicArn:
    Fn::Sub: arn:aws:sns:$(self:provider.region):${AWS::AccountId}:$(self:custom.topic)

functions:
  first:
    handler: first.hello
    events:
      - sns: $(self:custom.topic)

resources:
  Resources:
    TestBucket:
      Type: AWS::S3::Bucket
      DependsOn: SNSTopicPolicy
      Properties:
        BucketName: aa-test-95872017
        NotificationConfiguration:
          TopicConfigurations:
            - Event: s3:ObjectCreated:Put
              Filter:
                S3Key:
                  Rules:
                    - Name: prefix
                      Value: staging/
              Topic: $(self:custom.topicArn)
    SNSTopicPolicy:
      Type: AWS::SNS::TopicPolicy
      Properties:
        PolicyDocument:
          Version: "2012-10-17"
          Statement:
            - Effect: Allow
              Principal:
                AWS: "*"
              Action: sns:Publish
              Resource: $(self:custom.topicArn)
              Condition:
                ArnLike:
                  aws:SourceArn: arn:aws:s3:::aa-test-95872017
        Topics:
          - Ref: SNSTopic$(self:service)$(self:custom.topicName)$(self:custom.stage)

@ffxsam
Copy link
Contributor Author

ffxsam commented Nov 5, 2018

@HyperBrain @dschep Just following up on an old issue that's probably still existent.

@hdryx
Copy link

hdryx commented Dec 20, 2018

Hi,
I'm trying this code but i'm getting this error :
The CloudFormation template is invalid: Template format error: Unresolved resource dependencies [SNSTopic$(self:service)$(self:custom.topicName)$(self:custom.stage)] in the Resources block of the template

How to solve that ?

@technicallyfeasible
Copy link

@hdryx you will get this error if any of your variables contain "-" characters. Serverless generates the final topic name out of the original name while removing "-".

So if your topic is called "normalize-lambda-dev-raw-ingest" then the Ref would be "SNSTopicNormalizelambda${self:custom.stage}rawingest".

Hope that helps

@RuiLoureiro
Copy link

What is the state of this? Is the solution provided by @ffxsam still the best option?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants