0
votes

I have a AWS lambda which sends notification to SNS topic.

Lambda Function

def send_sns_notification(message):
    sns = boto3.client('sns')
    response = sns.publish(
        TopicArn='arn:aws:sns:ca-central-1:xxxxx',    
        Message=message, 
        MessageAttributes={
            'AWS.SNS.SMS.SenderID': {
              'DataType': 'String',
              'StringValue': 'Airflow'   
            },
            'AWS.SNS.SMS.SMSType': {
              'DataType': 'String',
              'StringValue': 'Transactional'   
            }
        }   
    )

And we use cloud formation template to deploy SNS Topic, AWS Lambda and SNS Subscription. How do I access the ARN for the topic created through CloudFormation.

Cloud Formation Template

 Lambda:
    Type: AWS::Serverless::Function
    Properties:
      Description: sends sns notification if dag in airflow fails
      FunctionName: "airflow-notification"
      Handler: send_sns_notification.py
      Runtime: python3.6
      ReservedConcurrentExecutions: !Ref AWS::NoValue
      Role: !FindInMap [EnvVariables, Parameters, LambdaRole]
      VpcConfig:
        SecurityGroupIds:
          - !FindInMap [EnvVariables, Parameters, VPCSecurityGroupId]
        SubnetIds:
          - !FindInMap [EnvVariables, Parameters, VPCSubnetId1]
          - !FindInMap [EnvVariables, Parameters, VPCSubnetId2]

  SNSTopic:
    Type: AWS::SNS::Topic
    Properties:
      TopicName: !FindInMap [EnvVariables, Parameters, SNSTopicName]
      DisplayName: airflow
      KmsMasterKeyId: !FindInMap [EnvVariables, Parameters, SNSCMK]


  SNSSubscription:
    Type: AWS::SNS::Subscription
    Properties:
      Endpoint: [email protected]
      Protocol: email
      TopicArn: !Ref 'SNSTopic'
2

2 Answers

1
votes

Pass the ARN in as an environment variable.

 Lambda:
    Type: AWS::Serverless::Function
    Properties:
      Description: sends sns notification if dag in airflow fails
      FunctionName: "airflow-notification"
      Handler: send_sns_notification.py
      Runtime: python3.6
      ReservedConcurrentExecutions: !Ref AWS::NoValue
      Role: !FindInMap [EnvVariables, Parameters, LambdaRole]
      VpcConfig:
        SecurityGroupIds:
          - !FindInMap [EnvVariables, Parameters, VPCSecurityGroupId]
        SubnetIds:
          - !FindInMap [EnvVariables, Parameters, VPCSubnetId1]
          - !FindInMap [EnvVariables, Parameters, VPCSubnetId2]
      Environment:
        Variables: 
          TopicArn: !Ref 'SNSTopic'
0
votes

Alternative, approach is as follows;

account_id = boto3.client('sts').get_caller_identity().get('Account')

currentRegion = os.environ['AWS_REGION']

# write logic to define SNS_TOPIC variable, you can keep/form it from environment variables or hardcode or other suitable alternative. Here, SNS_TOPIC will hold only name of the SNS Topic

Finally, set/form value for TopicArn using one of the following statements;

TopicArn = ":".join(["arn", "aws", "sns", currentRegion, account_id, SNS_TOPIC])

OR

TopicArn= ":".join(["arn:aws:sns:ca-central-1",SNS_TOPIC])