当使用AWS CloudWatch的ScheduleExpression参数时,有几种可能导致参数无效的情况。下面是一些常见的解决方法和代码示例:
import boto3
cloudwatch = boto3.client('cloudwatch')
response = cloudwatch.put_rule(
Name='MyScheduledRule',
ScheduleExpression='cron(0 10 * * ? *)',
State='ENABLED'
)
cloudwatch:PutRule
和cloudwatch:PutTargets
权限。以下是一个示例,展示如何创建具有适当权限的IAM角色:{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CloudWatchPermissions",
"Effect": "Allow",
"Action": [
"cloudwatch:PutRule",
"cloudwatch:PutTargets"
],
"Resource": "*"
}
]
}
import boto3
lambda_client = boto3.client('lambda')
cloudwatch_events = boto3.client('events')
response = lambda_client.create_function(
FunctionName='MyLambdaFunction',
Runtime='python3.8',
Role='arn:aws:iam::1234567890:role/LambdaExecutionRole',
Handler='lambda_function.lambda_handler',
Code={
'ZipFile': b'...',
}
)
response = cloudwatch_events.put_targets(
Rule='MyScheduledRule',
Targets=[
{
'Id': '1',
'Arn': 'arn:aws:lambda:us-west-2:1234567890:function:MyLambdaFunction'
}
]
)
通过检查以上几个方面,您应该能够解决“AWS CloudWatch - 参数ScheduleExpression无效”的问题。