gpt4 book ai didi

amazon-web-services - 事件规则不触发 lambda

转载 作者:行者123 更新时间:2023-12-03 07:38:17 33 4
gpt4 key购买 nike

SNSLambdaScheduleRule:
Type: AWS::Events::Rule
Properties:
RoleArn: "arn:aws:iam::119497028878:role/LabRole"
Description: "Schedule rule for SNS Lambda"
ScheduleExpression: "rate(2 minutes)"
State: ENABLED
Targets:
- Arn: !GetAtt SNSSubscriptionLambda.Arn
Id: "TargetFunctionV1"

我正在尝试实现每 2 分钟调用一次 lambda 的功能。它确实根据 Amazon eventbridge>buses>rules 部署到 aws,但它不会每 2 分钟触发一次 lambda。

上次我尝试通过 UI 创建类似的功能时,我使用了 Amazon eventbridge>Scheduler>schedule,每 2 分钟触发一次 lambda。我在这里做错了什么?

最佳答案

您的 Cloudformation 模板存在一些问题。以下是一些可能的问题和建议:

• 资源类型应为事件规则,而不是AWS::Scheduler::ScheduleAWS::Scheduler::Schedule 是 Amazon EventBridge Scheduler 的资源,它是与 Amazon EventBridge[1] 不同的服务。

• 目标属性应该是目标列表,而不是单个目标。每个目标都应该有一个 Arn 和一个 Id 属性,以及取决于目标类型的其他属性(可选)[1]。

• 灵活时间窗口的模式属性应该是FLEXIBLE 或STRICT,而不是带引号的“FLEXIBLE”。

要创建 CloudFormation 模板来计划每 2 分钟从 SQS 队列发送到 SNS 主题的消息,您可以使用 Amazon CloudWatch Events 以所需的时间间隔触发 Lambda 函数。

AWSTemplateFormatVersion: 2010-09-09
Description: CloudFormation template for scheduling SQS to SNS message delivery.

Resources:
EmailQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: MyEmailQueue

LambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: MyEmailFunction
Runtime: python3.8
Handler: lambda_function.handler
Role: !GetAtt LambdaExecutionRole.Arn
Code:
ZipFile: |
import boto3
import json

def handler(event, context):
# This Lambda function will be triggered by the CloudWatch Events rule
# Replace this with your logic to fetch messages from the SQS queue and send them to SNS
sqs = boto3.client('sqs')
sns = boto3.client('sns')
queue_url = 'YOUR_SQS_QUEUE_URL'
sns_topic_arn = 'YOUR_SNS_TOPIC_ARN'
while True:
response = sqs.receive_message(
QueueUrl=queue_url,
MaxNumberOfMessages=1,
WaitTimeSeconds=20
)

if 'Messages' in response:
for message in response['Messages']:
sns.publish(
TopicArn=sns_topic_arn,
Message=message['Body']
)
sqs.delete_message(
QueueUrl=queue_url,
ReceiptHandle=message['ReceiptHandle']
)

LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: MyLambdaRole
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Path: "/"
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole

ScheduleRule:
Type: AWS::Events::Rule
Properties:
Name: MyScheduleRule
Description: CloudWatch Events rule to trigger the Lambda function every 2 minutes
ScheduleExpression: rate(2 minutes)
State: ENABLED
Targets:
- Arn: !GetAtt LambdaFunction.Arn
Id: TargetFunctionV1

Outputs:
QueueURL:
Value: !Ref EmailQueue
Description: URL of the created SQS queue

FunctionName:
Value: !Ref LambdaFunction
Description: Name of the Lambda function

RuleName:
Value: !Ref ScheduleRule
Description: Name of the CloudWatch Events rule

部署此模板之前,请确保将“YOUR_SQS_QUEUE_URL”替换为 SQS 队列的实际 URL,将“YOUR_SNS_TOPIC_ARN”替换为 SNS 主题的 ARN。 Lambda 函数将由 CloudWatch Events 规则每 2 分钟触发一次,它将从 SQS 队列中获取消息并将其发送到指定的 SNS 主题。

确保您的 Lambda 函数具有从 SQS 队列接收消息、处理后删除消息以及将消息发布到 SNS 主题的必要权限。另外,检查 Lambda 函数的超时并确保它有足够的时间在计划的时间间隔内处理消息。

[1] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-scheduler-schedule.html

关于amazon-web-services - 事件规则不触发 lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76812657/

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