gpt4 book ai didi

amazon-web-services - AWS - 如何将 ARN 从 cloudformation 模板传递到 aws lambda 函数

转载 作者:行者123 更新时间:2023-12-03 07:34:42 24 4
gpt4 key购买 nike

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

Lambda 函数

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主题AWS LambdaSNS订阅。如何访问通过 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: <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c4a3a5b1b6a5aaa3aab7aca5ac84a3a9a5ada8eaa7aba9" rel="noreferrer noopener nofollow">[email protected]</a>
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'

关于amazon-web-services - AWS - 如何将 ARN 从 cloudformation 模板传递到 aws lambda 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65325865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com