[英]How can I pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python (boto3)?
I am using python boto3 to create a CloudWatch alarm for a sub account (not a root account). 我正在使用python boto3为子帐户(而非root帐户)创建CloudWatch警报。 I have stored the account id of that sub account in a variable called accnum . 我已经将该子帐户的帐户ID存储在名为accnum的变量中。 I need to stop an ec2 instance in that sub account once it exceeds a CPU % of 10. Going by the boto3 docs, we need to pass the arn value in the AlarmActions to start/stop/terminate an EC2 instance. 一旦CPU百分比超过10,我需要在该子帐户中停止ec2实例。通过boto3文档,我们需要在AlarmActions中传递arn值,以启动/停止/终止EC2实例。
For root account, it is like this: 对于root帐户,它是这样的:
AlarmActions=[
'arn:aws:swf:us-east-2:{CUSTOMER_ACCOUNT}:action/actions/AWS_EC2.InstanceId.Stop/1.0'
],
How can I do the same for a sub account. 如何为子帐户执行相同操作。 I tried passing the accnum variable in the AlarmActions but it throws a syntax error. 我尝试在AlarmActions中传递accnum变量,但它引发了语法错误。
I tried like this : 我这样尝试过:
AlarmActions=[
'arn:aws:swf:us-east-2:',accnum,':action/actions/AWS_EC2.InstanceId.Stop/1.0'
],
But a syntax error is thrown like this : 但是这样会引发语法错误:
botocore.exceptions.ClientError: An error occurred (ValidationError) when calling the PutMetricAlarm operation: Invalid arn syntax: arn:aws:swf:us-east-2:
How can I pass the accnum as a variable ? 如何将accnum作为变量传递? Or is there any other way to pass Alarm Actions to stop an ec2 instance ? 还是有其他方法可以通过传递警报操作来停止ec2实例? Or is there an equivalent for sub account like {CUSTOMER_ACCOUNT} is for root account ? 还是有与{CUSTOMER_ACCOUNT}这样的子帐户等效的根帐户?
Python String and Integer concatenation 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.