要确保AWS Batch从EventBridge调度器/规则触发,需要执行以下步骤:
import boto3
def create_eventbridge_rule():
client = boto3.client('events')
response = client.put_rule(
Name='MyEventBridgeRule',
ScheduleExpression='cron(0 12 * * ? *)', # 根据需要调整调度表达式
State='ENABLED', # 将规则设置为启用状态
)
response = client.put_targets(
Rule='MyEventBridgeRule',
Targets=[
{
'Id': 'Target1',
'Arn': 'arn:aws:batch:us-west-2:123456789012:job-definition/MyBatchJobDefinition:1', # 替换为AWS Batch作业定义的ARN
'RoleArn': 'arn:aws:iam::123456789012:role/MyBatchJobRole' # 替换为具有批处理作业执行权限的IAM角色的ARN
}
]
)
print("EventBridge rule created successfully")
create_eventbridge_rule()
import boto3
def create_batch_job_definition():
client = boto3.client('batch')
response = client.register_job_definition(
jobDefinitionName='MyBatchJobDefinition',
type='container',
containerProperties={
'image': 'my-container-image', # 替换为您的容器镜像
'vcpus': 1,
'memory': 1024,
'command': ['my-command'], # 替换为您的容器命令
'jobRoleArn': 'arn:aws:iam::123456789012:role/MyBatchJobRole' # 替换为具有批处理作业执行权限的IAM角色的ARN
},
retryStrategy={
'attempts': 1
}
)
print("Batch job definition created successfully")
create_batch_job_definition()
events:PutTargets
和events:PutRule
权限。batch:SubmitJob
权限。完成以上步骤后,AWS Batch作业应该能够从EventBridge调度器/规则触发。