问题描述
我们可以设置事件规则来触发ECS任务,但是我看不到触发事件是否传递给正在运行的ECS任务,以及在任务中如何获取此事件的内容.如果触发了Lambda,则可以从 event
变量中获取它,例如,在Python中:
We can set up event rules to trigger an ECS task, but I don't see if the triggering event is passed to the runing ECS task and in the task how to fetch the content of this event. If a Lambda is triggered, we can get it from the event
variable, for example, in Python:
def lambda_handler(event, context):
...
但是在ECS中,我看不到如何做类似的事情.进入cloudtrail日志存储桶听起来不是一个好方法,因为它需要大约5分钟的延迟才能显示新的日志/事件,这需要ECS等待,并且需要额外的逻辑才能与S3进行对话并查找&阅读日志.而且,当触发事件频繁发生时,这听起来很难处理.
But in ECS I don't see how I can do things similar. Going to the cloudtrail log bucket doesn't sound to be a good way because it has around 5 mins delay for the new log/event to show up, which requires ECS to be waiting and additional logic to talk to S3 and find & read the log. And when the triggering events are frequent, this sounds hard to handle.
推荐答案
一种解决方法是在Cloud监视规则中设置两个目标.
One way to handle this is to set two targets In the Cloud watch rule.
- 一个目标将启动ECS任务
- 一个目标会将同一事件推送到SQS
因此SQS将包含类似信息
So the SQS will contain info like
{
"version": "0",
"id": "89d1a02d-5ec7-412e-82f5-13505f849b41",
"detail-type": "Scheduled Event",
"source": "aws.events",
"account": "123456789012",
"time": "2016-12-30T18:44:49Z",
"region": "us-east-1",
"resources": [
"arn:aws:events:us-east-1:123456789012:rule/SampleRule"
],
"detail": {}
}
因此,当ECS任务启动时,它将能够从SQS读取事件.
So when the ECS TASK up, it will be able to read event from the SQS.
例如在Docker入口点
For example in Docker entrypoint
#!/bin/sh
echo "Starting container"
echo "Process SQS event"
node process_schdule_event.sj
#or if you need process at run time
schdule_event=$(aws sqs receive-message --queue-url https://sqs.us-west-2.amazonaws.com/123456789/demo --attribute-names All --message-attribute-names All --max-number-of-messages 1)
echo "Schdule Event: ${schdule_event}"
# one process done, start the main process of the container
exec "$@"
这篇关于由cloudwatch/eventbridge事件调用时,如何在ECS中获取事件内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!