AWS提供了一些与事件相关的服务,例如CloudWatch事件、S3事件、Kinesis数据流、SNS通知等,可以用于创建定时和基于事件的复杂事件模式。以下是示例代码:
import boto3
client = boto3.client('events')
response = client.put_rule(
Name='my-event-rule',
ScheduleExpression='cron(0 0 * * ? *)', # a cron expression that triggers the rule every day at midnight
State='ENABLED'
)
# define a Lambda function that receives the event data and performs some action
def lambda_handler(event, context):
print('Received scheduled AWS event:', event)
# do something
import boto3
client = boto3.client('s3')
response = client.put_bucket_notification_configuration(
Bucket='my-bucket',
NotificationConfiguration={
'LambdaFunctionConfigurations': [
{
'LambdaFunctionArn': 'arn:aws:lambda:::function:',
'Events': ['s3:ObjectCreated:*']
}
]
}
)
# define a Lambda function that receives the S3 event data and performs some action
def lambda_handler(event, context):
print('Received S3 event:', event)
# do something with the uploaded file
以上示例代码分别展示了如何使用AWS CloudWatch规则和Lambda函数来创建计划事件模式,以及如何使用AWS S3事件和Lambda函数来创建基于事件的复杂事件模式。