将cloudwatch事件添加到emr集群

时间:2017-11-07 00:35:24

标签: boto3 amazon-cloudwatch

使用boto3库,我想创建一个EMR集群,然后使用云监视事件基于集群更改创建事件。

所以我可以创建群集,但我对CloudWatchEvents section of boto3 works的方式感到困惑。

根据boto3库中CWE的文档,put_rule有一个名为EventPattern的参数,它是一个字符串。它描述如下:

EventPattern (字符串) - 事件模式。有关更多信息,请参阅Amazon CloudWatch Events用户指南中的Events and Event Patterns

按照说明中提供的链接,它显示了一个如下所示的JSON对象:

{
  "source": [ "aws.ec2" ],
  "detail-type": [ "EC2 Instance State-change Notification" ],
  "detail": {
    "state": [ "running" ]
  }
}

据我了解,这适用于自定义事件,但EMR提供events for CloudWatch

如果我要遵循为EMR事件提供的模式,我是否需要提供一个看起来像这样的字符串?

{
    "source": ["aws.emr"],
    "detail-type": "EMR Cluster State Change",
    "detail": {
         "clusterid": <clusterid>,
         "state": "STARTING"
    }
}

这种逻辑存在一些缺陷吗?我有点困惑它是如何与boto文档结合在一起的。

我尝试使用以下代码将字典转换为字符串:

    client.put_rule(
    Name='Cluster_starting',
    EventPattern=str({
        "source": ["aws.emr"],
        "detail-type": "EMR Cluster State Change",
        "detail": {
            "clusterid": cluster_id,
            "state": "STARTING"
        }
    }),
    State="ENABLED"
)

但得到了以下错误:

botocore.errorfactory.InvalidEventPatternException: An error occurred (InvalidEventPatternException) when calling the PutRule operation: Event pattern is not valid. Reason: Unexpected character (''' (code 39)): was expecting double-quote to start field name at [Source: {'source': ['aws.emr'], 'detail-type': 'EMR Cluster State Change', 'detail': {'state': 'STARTING', 'clusterid': '<cluster_id>'}}; line: 1, column: 3]

1 个答案:

答案 0 :(得分:0)

在与AWS支持人员交谈后,我指出了错误。

在示例字典中,每个值都保存在一个数组中,因此“clusterid”必须是“clusterid”=“[]”,并且引号需要转义。