Serverless-application-model: SNS topic to being linked to Function?

Created on 21 Nov 2016  路  3Comments  路  Source: aws/serverless-application-model

Can I use !Ref to reference an SNS topic defined in the same template? The following pseudo-code is basically what I'm trying to achieve. The function and topic gets created but the function does not have a trigger for the SNS topic automatically created.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs4.3
      Timeout: 10
    Events:
      MyTopic:
        Type: SNS
        Properties:
          Topic: !Ref ExampleTopic
  ExampleTopic:
    Type: "AWS::SNS::Topic"
    Properties:
      DisplayName: example

Most helpful comment

It would be nice if sam validate would complain about it. I've just spent a few hours just to find out the indentation was wrong :)

All 3 comments

Your config has a bug. The Events section should be a key of the Properties dictionary like this for it to work:

  Properties:
      Handler: index.handler
      Runtime: nodejs4.3
      Timeout: 10
      Events:
        MyTopic:
          Type: SNS
          Properties:
             Topic: !Ref ExampleTopic

Hi, I have exatly the same issue. SNS trigger is not getting created when i deploy my template.
There seems to be an issue with "AutoPublishAlias: test".
If i remove the alias, it works just fine.

AWSTemplateFormatVersion:` '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An AWS Serverless Specification template describing your function.
Resources:
  SSM:
    Type: 'AWS::Serverless::Function'
    Properties:
      FunctionName: 'SSM'
      AutoPublishAlias: test
      Handler: index.handler
      Runtime: python3.6
      CodeUri: .
      Description: ''
      MemorySize: 128
      Timeout: 3
      Role: 'arn:aws:iam::712115679703:role/Lambda-ssm-ec2'
      DeploymentPreference:
        Type: AllAtOnce
      Events:
        MyTopic:
          Type: SNS
          Properties:
            Topic: !Ref SNSTopic

  SNSTopic:
    Type: 'AWS::SNS::Topic'
    Properties:
      DisplayName: SNSTopic

It would be nice if sam validate would complain about it. I've just spent a few hours just to find out the indentation was wrong :)

Was this page helpful?
0 / 5 - 0 ratings