Unable to import exported SNS topic arn

I am trying to setup a function in one stack that uses an imported SNS topic for an event. Below is the exported SNS topic.

resources:
  Resources:
    CustomSNSTopic:
      Type: "AWS::SNS::Topic"
      Properties:
        DisplayName: 'CustomSNSTopic'
        TopicName: ${self:custom.SNSTopicName}
  Outputs:
    ExportSNSArn:
      Description: The ARN for the exported SNS topic
      Value:
        "Ref": CustomSNSTopic
      Export:
        Name: ${opt:stage}-CustomSNSTopic

When I deploy this stack I can see the ARN in the CloudFormation dashboard.
I’m then trying to import this value so I can setup a lambda to have it as an event. When I assign an env variable with {'Fn::ImportValue': ${opt:stage}-CustomSNSTopic} I can see the arn in the lambda console. But when I try adding that here it fails.

  Function:
    handler: lambda.handler
    events:
      - http:
          path: somepath
          method: post
      - sns: {'Fn::ImportValue': {"Fn::Sub" : "${opt:stage}-CustomSNSTopic"}}

I haven’t been able to find any documentation to get this working and would love some help please.

I’ve got exactly same issue :frowning:

I think you need an object with the arn and topicName in your second snippet (you might need, or want, to output the topic name rather than hard code it as I have below)

Function:
  handler: lambda.handler
  events:
    - http:
        path: somepath
        method: post
    - sns:
        arn: 
           Fn::ImportValue: ${opt:stage}-CustomSNSTopic
        topicName: ${self:custom.SNSTopicName}

since i’m getting the exact same issue as OP i was hoping to get help.
edlea, thanks for your response, will try and come with an update.
i still need some help with my delice recipes projects. thanks

@Eque19 I was in fact able to solve it. I had to import the value into a custom var and then use the correct format when referencing the topic as an event as @edlea pointed out.

Function:
    handler: App::App.Functions::Function
    events:
      - http:
          path: function
          method: post
      - sns: 
          arn: ${self:custom.importedSnsTopic}
          topicName: "${opt:stage}-importedSnsTopic"

custom:
  importedSnsTopic:
    Fn::ImportValue: '${opt:stage, self:provider.stage}-ImportedTopic'

Hope that helps!

1 Like