How to pass create SNS topic ARN to Lambda function

Hi,
I have serverless.yml configuration and in this file I trying to create some SNS Topic with some name. In Lambda function I want to use created SNS topic ARN for sns.publish(), but I don’t know how to pass created ARN to Lambda function. Thanks.
Bellow you can find some serverless.yml

custom:
  webpackIncludeModules: true
  region: ${opt:region, "us-east-1"}
  stage: ${opt:stage, "prod"}
  snsTopicName: some-sns-topic-${self:custom.stage}
  snsTopicArn:
    Fn::Join:
      - ':'
      - - arn
        - aws
        - sns
        - Ref: AWS::Region
        - Ref: AWS::AccountId
        - ${self:custom.snsTopicName}
provider:
  name: aws
  runtime: nodejs6.10
  stage: ${self:custom.stage}
  region: ${self:custom.region}
  cfLogs: true
  iamRoleStatements:
- Effect: Allow
  Action:
    - s3:GetObject
    - s3:ListBucket
  Resource:
    - arn:aws:s3:::${self:custom.bucket}
    - arn:aws:s3:::${self:custom.bucket}/*
- Effect: Allow
  Action:
    - sns:Publish
  Resource:
    - ${self:custom.snsTopicArn}
functions:
  process:
    handler:handler.process
    timeout: 300
    memorySize: 128
    events:
      - s3:
          bucket: ${self:custom.bucket}
          event: s3:ObjectCreated:*
          rules:
            - suffix: .XML
      - s3:
          bucket: ${self:custom.bucket}
          event: s3:ObjectCreated:*
          rules:
            - suffix: .xml
    environment:
      TOPIC_NAME: ${self:custom.snsTopicName}

Pass topic into the function via environment variables https://github.com/DavidWells/serverless-workshop/blob/master/lessons-code-complete/events/sns/sns-advanced/serverless.yml#L16

Thanks, but according to http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#publish-property
I should pass TopicARN and phone number.
/**
* The topic you want to publish to. If you don’t specify a value for the TopicArn parameter, you must specify a value for the PhoneNumber or TargetArn parameters.
/
TopicArn?: topicARN;
/
*
* Either TopicArn or EndpointArn, but not both. If you don’t specify a value for the TargetArn parameter, you must specify a value for the PhoneNumber or TopicArn parameters.
*/
TargetArn?: String;

1 Like