如何在 CloudWatch Alarm Actions with python (boto3) 中使用变量作为参数传递 AWS 子账户的账户 ID?

编码

我正在使用 python boto3 为子账户(不是根账户)创建 CloudWatch 警报。我已将该子帐户的帐户 ID 存储在名为accnum的变量中一旦超过 10 的 CPU 百分比,我需要停止该子帐户中的 ec2 实例。根据 boto3 文档,我们需要在 AlarmActions 中传递 arn 值以启动/停止/终止 EC2 实例。

对于root帐户,它是这样的:

AlarmActions=[
  'arn:aws:swf:us-east-2:{CUSTOMER_ACCOUNT}:action/actions/AWS_EC2.InstanceId.Stop/1.0'
],

我如何为子账户做同样的事情。我尝试在 AlarmActions 中传递 accnum 变量,但它引发了语法错误。

我试过这样:

AlarmActions=[
  'arn:aws:swf:us-east-2:',accnum,':action/actions/AWS_EC2.InstanceId.Stop/1.0'
],

但是语法错误是这样抛出的:

 botocore.exceptions.ClientError: An error occurred (ValidationError) when calling the PutMetricAlarm operation: Invalid arn syntax: arn:aws:swf:us-east-2:

如何将 accnum 作为变量传递?或者有没有其他方法可以通过警报操作来停止 ec2 实例?或者是否有像 {CUSTOMER_ACCOUNT} 这样的子帐户的等价物用于 root 帐户?

苏达山·西瓦桑卡兰

Python 字符串和整数连接

>>> print("arn:aws:swf:us-east-2:{0}:action/actions/AWS_EC2.InstanceId.Stop/1.0".format(acccnum))
arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0

>>> print("arn:aws:swf:us-east-2:" + str(acccnum) + ":action/actions/AWS_EC2.InstanceId.Stop/1.0")
arn:aws:swf:us-east-2:12312312312312:action/actions/AWS_EC2.InstanceId.Stop/1.0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章