我们正在创建一个每 5 分钟运行一次的 cron 作业,它只是将“hello world”记录到我们的日志组中。这是通过 AWS Cloudformation 完成的,任务只是回显“hello world”。
这是预定的规则
ScheduledRule:
Type: "AWS::Events::Rule"
Properties:
Description: "ScheduledRule"
ScheduleExpression: "rate(5 minutes)"
State: "ENABLED"
Targets:
-
RoleArn:
Fn::GetAtt: [ RoleNeeded, Arn ]
Id: Cluster-Test-Cron-job
Arn:
Ref: ArnOfOurClusterTheScriptShouldExecute
EcsParameters:
TaskDefinitionArn:
Ref: TaskDefinitionThatExecutesLoggerScript
TaskCount: 1
Cloudwatch 指标显示任务已触发,但也指示 FailedInitation
。看来我们在这里遗漏了一些东西,ScheduleExpression
是否足以使脚本每 5 分钟执行一次?我们检查了所需的角色,它已经是与策略 AmazonEC2ContainerServiceEventsRole
一起使用的建议和推荐角色, self 们测试以来,ArnOfOurClusterTheScriptShouldExecute
和 TaskDefinitionThatExecutesLoggerScript
也可以工作使用 TaskDefinitionThatExecutesLoggerScript
设置手动运行任务,并将其记录到我们的日志组。
我找到了这个question但这也增加了 FailedInitation 指标。
为了让脚本正确执行,我们在上述设置中错过了什么?
最佳答案
我们假设我们需要一个事件模式,但我们的方向是错误的。但问题出在这个角色上。
角色设置有错误
RoleWeNeed:
Type: AWS::IAM::Role
Properties:
Path: /
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ecs.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceRole
- arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceEventsRole
我们尝试在通过cloudformation创建计划规则后手动更改计划规则,并注意到我们使用上述设置创建的角色虽然具有正确的策略,但在角色的下拉选项中不可用。
下面的策略允许适本地提供策略,但这并没有发生,因为首先,它对于使用我们的事件规则是不可见的。
arn:aws:iam::aws:policy/service-role/AmazonEC2ContainerServiceEventsRole
设置错误
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "ecs.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
基于文档的 ecs.amazonaws.com 不会使该角色可用于计划规则所属的事件。
In IAM roles, use the Principal element in the role's trust policy to specify who can assume the role.
正确的设置
允许事件通过 events.amazonaws.com 访问该角色
AssumeRolePolicyDocument: |
{
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": [ "events.amazonaws.com" ]},
"Action": [ "sts:AssumeRole" ]
}]
}
关于amazon-web-services - AWS::Event::Rule 上的 EventPattern 需要触发 bash 脚本记录器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47412830/