Trying to create CF resources including SNS topic that is used in the stack & by a function

aws
#1

I’m trying to create an SNS topic in the Resource section of my serverless.yml and use this topic as an event source for a function.

The relevant bits of my serverless.yml are shown below.

What ends up happening is that the Lambda does not get the SNS topic as the trigger – it gets nothing.

The reason I’m trying to do it this way is that my project needs to also build other artifacts that depend on the SNS topic.

I’m thinking this should be easy to do, but I really cannot find it.

After trying a bunch of other things and meeting with no success I modeled this version after https://github.com/DavidWells/serverless-workshop/blob/master/lessons-code-complete/events/sns/sns-advanced/serverless.yml

Any help would be gratefully received.

service: cloudtrail-security
plugins:
  - serverless-python-requirements
  - serverless-pseudo-parameters

provider:
  name: aws
  runtime: python3.6
  stage: dev
  region: us-east-1
  custom:
    logGroupName: CloudTrail/DefaultLogGroup
    topicName: ${self:provider.stage}-securityAlarm
    topicArn:
      Fn::Join:
        - ''
        - - 'arn:aws:sns:'
          - Ref: AWS::Region
          - ":"
          - Ref: AWS::AccountId
          - ":"
          - Fn::GetAtt:
              - snsTopic
              - TopicName
    dev:
      logLevel: debug
      slackUrl: ######
      metricNamespace: cgt-dev
    prd:
      logLevel: info
      slackUrl: ######
      metricNamespace: cgt
  environment:
    SLACK: ${self:provider.custom.${self:provider.stage}.slackUrl}
    LOGLEVEL: ${self:provider.custom.${self:provider.stage}.logLevel}
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "sns:Publish"
      Resource: ${self:provder.custom.topicArn}
package:
  exclude:
    - venv/**
    - .git/**
    - '*~'

functions:
  post_to_slack:
    handler: handler.post_to_slack
    events:
      - sns:
        arn: arn:aws:sns:${self:provider.region}:#{AWS::AccountId}:${self:provider.custom.topicName}
        topicName: ${self:provider.custom.topicName}

resources:
  Resources:
    snsTopic:
      Type: "AWS::SNS::Topic"
      Properties:
        DisplayName: "Topic for security issues"
        TopicName: ${self:provider.custom.topicName}
0 Likes

#2

Hi, it seems to me that you have just forgotten to add a few spaces (before arn and topicName)
So it should be -

functions:
  post_to_slack:
    handler: handler.post_to_slack
    events:
      - sns:
           arn: arn:aws:sns:${self:provider.region}:#{AWS::AccountId}:${self:provider.custom.topicName}
           topicName: ${self:provider.custom.topicName}

Could you check it out?

p.s. you can try to use topicArn: !Ref snsTopic instead of Fn::Join …

0 Likes

#3

Both worked beautifully, thank you! It must have been the tears of frustration that prevented me from noticing the missing spaces.

I do appreciate your kind help!

0 Likes

#4

:slight_smile: yw, happy to help

0 Likes