DependsOn for Functions?

Hey All,

I Have a function which should publish to an AWS SNS topic.

The SNS topic is created from the resources section of the serverless.yml file. However the output, CloudFormation doesn’t make the function depend on the SNS Topic.

I need the function to receive the SNS topic ARN.I’m trying to do this with “Fn:GetAtt” but that causes dependency problems as the functions is created before the SNS topic.

Can DependsOn be specified in function sections of serverless.yml?

If not how can i achieve this?

I’ve reproduced this issue here: https://github.com/ProbablePrime/serverless-sns-issue

Is there a reason you’re manually setting up the SNS topic in the resource section? Normally you would just do this and let Serverless handle the rest.

functions:
  myfunction:
    handler: handler.myfunc
    events:
      - sns: snsTopic

Assuming you need to set it up manually then something like this should work.

custom:
  region: ${opt:region, self:provider.region}

functions:
  hello:
    handler: handler.hello
    environment:
      SNS_ARN: { "Fn::Join" : [":", ["arn:aws:sns:${self:custom.region}", { "Ref" : "AWS::AccountId" }, "snsTopic" ] ]  }

I’m using a variation on this in my serverless.yml

I dont intend to subscribe to this SNS topic from this service. It will be exported from the cloud formation stack to be used in another serverless micro-service so events is not suitable.

Your Fn::Join solution is interesting but quite long and unwieldy. If i manually edit the outputted cloudformation i can fix this with a simple depends on.

I might need to open an issue on the framework.

Thanks for your reply!

DependsOn only impacts the order of creation so it won’t help you find the Arn for the SNS topic. Even if the stack is created in the right order you still can’t use Fn::GetAtt to find the Arn because CloudFormation doesn’t support that. The only attribute you can retrieve is the TopicName. At least that’s what the doc’s say - http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html

As far as I’m aware the only way to get the Arn is to build it using Fn::Join or Fn::Sub.

Ahh, that seems problematic. In the JS SDK i can only publish to a topic via ARN.

Ok thank you for the full explanation, i’ll use the join for now :).

I’ll also see what resources in the AWS space are available to help out here, from an outside perspective i would assume GetAtt should be able to get an Arn if the resource has one.

I figured this out using Ref https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html which will get you the sns topic ARN :slight_smile:

Thank you!

1 Like

It’s been a while since this question was asked but now, you can use cloud formation and do something like:

Stack A - where the topic is created - serverless.yml

resources:
    Resources:
        customerTopic:
            Type: AWS::SNS::Topic
            Properties:
                TopicName: customer-topic
    Outputs:
        customerTopicArn:
            Value: !customerTopic
            Export:
                Name: customerTopicArn

Stack B - where the topic is referenced - serverless.yml

customerTopicArn: ${cf:stackA-environment.customerTopicArn}

I would recommand you this plugin to write your SNS_ARN.