在部署后的定时事件下,Lambda函数的行为可以通过AWS CloudWatch Events进行配置和管理。以下是一个包含代码示例的解决方法:
import datetime
def lambda_handler(event, context):
current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"定时事件触发时间:{current_time}")
# 在这里可以添加其他你需要执行的逻辑
import boto3
def create_cloudwatch_event_rule():
client = boto3.client('events')
rule_name = 'MyScheduledRule'
schedule_expression = 'cron(0 12 * * ? *)' # 每天中午12点触发
target_arn = 'arn:aws:lambda:us-west-2:1234567890:function:MyLambdaFunction' # Lambda函数的ARN
response = client.put_rule(
Name=rule_name,
ScheduleExpression=schedule_expression,
State='ENABLED'
)
# 添加Lambda函数作为目标
response = client.put_targets(
Rule=rule_name,
Targets=[
{
'Id': '1',
'Arn': target_arn
},
]
)
print("定时规则创建成功!")
create_cloudwatch_event_rule()
请注意,以上示例代码是使用Python和Boto3 SDK创建定时规则和Lambda函数的示例。你可以根据自己的需求和所使用的编程语言,调整和修改代码。