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

How can I pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?

我正在使用 python boto3 为子账户(不是根账户)创建 CloudWatch 警报。我将该子账户的账户 ID 存储在一个名为 accnum 的变量中。一旦它超过 10 的 CPU %,我需要停止该子帐户中的 ec2 实例。根据 boto3 文档,我们需要将 AlarmActions 中的 arn 值传递给 start/stop/terminate 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} 的子帐户对应于根帐户?

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