Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Amazon web services AWS-如何将ARN从云形成模板传递到AWS lambda函数_Amazon Web Services_Aws Lambda_Amazon Cloudformation - Fatal编程技术网

Amazon web services AWS-如何将ARN从云形成模板传递到AWS lambda函数

Amazon web services AWS-如何将ARN从云形成模板传递到AWS lambda函数,amazon-web-services,aws-lambda,amazon-cloudformation,Amazon Web Services,Aws Lambda,Amazon Cloudformation,我有一个AWS lambda,它向SNS主题发送通知 Lambda函数 def发送sns_通知(消息): sns=boto3.客户端(“sns”) 响应=sns.publish( TopicArn='arn:aws:sns:ca-central-1:xxxxx', 消息=消息, 消息属性={ 'AWS.SNS.SMS.SenderID':{ “数据类型”:“字符串”, “StringValue”:“气流” }, “AWS.SNS.SMS.SMSType”:{ “数据类型”:“字符串”, “Str

我有一个AWS lambda,它向SNS主题发送通知

Lambda函数

def发送sns_通知(消息):
sns=boto3.客户端(“sns”)
响应=sns.publish(
TopicArn='arn:aws:sns:ca-central-1:xxxxx',
消息=消息,
消息属性={
'AWS.SNS.SMS.SenderID':{
“数据类型”:“字符串”,
“StringValue”:“气流”
},
“AWS.SNS.SMS.SMSType”:{
“数据类型”:“字符串”,
“StringValue”:“事务性”
}
}   
)
我们使用云形成模板部署
SNS主题
AWS Lambda
SNS订阅
。如何访问通过CloudFormation创建的主题的ARN

云形成模板

Lambda:
类型:AWS::Serverless::Function
特性:
描述:如果气流中的dag失败,则发送sns通知
功能名称:“气流通知”
处理程序:send_sns_notification.py
运行时:python3.6
ReservedConcurrentExecutions:!参考AWS::NoValue
角色:!FindInMap[环境变量、参数、LambdaRole]
VpcConfig:
SecurityGroupId:
- !FindInMap[EnvVariables,Parameters,VPCSecurityGroupId]
子网:
- !FindInMap[环境变量、参数、VPCSubnetId1]
- !FindInMap[环境变量、参数、VPCSubnetId2]
SNSTopic:
类型:AWS::SNS::Topic
特性:
主题名称:!FindInMap[EnvVariables,Parameters,SNSTopicName]
显示名称:气流
KmsMasterKeyId:!FindInMap[EnvVariables,Parameters,SNSCMK]
SNS订阅:
类型:AWS::SNS::订阅
特性:
终点:gaurangnshah@gmail.com
协议:电子邮件
托皮卡恩:!参考“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=“:”。加入([“arn”、“aws”、“sns”、当前地区、帐户\u id、sns\u主题])


TopicArn=“:”.join([“arn:aws:sns:ca-central-1”,sns\u TOPIC])

如何创建依赖关系,以便先创建sns TOPIC,然后创建aws lambda?@GaurangShah使用
时,依赖关系是隐式的!Ref