简体   繁体   English

如何在python(boto3)的CloudWatch Alarm Actions中使用变量作为参数传递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)?

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.

相关问题 使用 boto3 在 EMR 上发出 AWS CloudWatch 警报 - AWS CloudWatch alarm on EMR using boto3 如何使用boto3更改AWS账户电子邮件? - How can I change the AWS Account Email with boto3? 如何使用 python boto3 从多个 AWS 账户生成 EC2 库存 - How generate EC2 inventory from multiple AWS Account using python boto3 如何使用 python boto3 仅从 AWS 账户中仅获取 KMS 客户托管密钥? - How to get only KMS Customer managed keys alone from AWS Account using python boto3? 给定aws-lambda,如何使用boto3更改与其相关的cloudwatch规则? - Given an aws-lambda, how can I change the cloudwatch rule associated with it using boto3? 使用boto3在AWS帐户中取消标记ec2实例的列表 - list of untagges ec2 instances in aws account using boto3 Cloudwatch 计费警报 Boto3 - Cloudwatch Billing Alarm Boto3 AWS:使用boto3检索具有交叉账户访问权限的S3存储桶的列表的Python脚本 - AWS : Python Script to Retrieve list of S3 bucket having Cross account access using boto3 当使用 boto3 推送指标时,AWS CloudWatch 警报处于“数据不足”state 中 - AWS CloudWatch alarm is in “insufficient data” state when metrics are pushed using boto3 如何使用 boto3 和 lambda python 获取 lambda 的 cloudwatch 指标? - How to get cloudwatch metrics of a lambda using boto3 and lambda python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM