簡體   English   中英

AWS - 如何將 ARN 從雲形成模板傳遞到 aws lambda function

[英]AWS - How to pass ARN from cloud formation template to aws lambda function

我有一個向 SNS 主題發送通知的 AWS lambda。

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'   
            }
        }   
    )

我們使用雲形成模板來部署SNS TopicAWS LambdaSNS Subscription 如何訪問通過 CloudFormation 創建的主題的 ARN。

雲形成模板

 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: gaurangnshah@gmail.com
      Protocol: email
      TopicArn: !Ref 'SNSTopic'

將 ARN 作為環境變量傳入。

 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'

替代方法如下;

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

最后,使用以下語句之一設置/形成 TopicArn 的值;

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

或者

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM