Pass account id of an AWS sub account using a variable as an argument in CloudWatch Alarm Actions with python boto3

0 votes

I am using python boto3 to create a CloudWatch alarm for a sub account (not a root account). I have stored the account id of that sub account in a variable called 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.

For root account, it is like this:

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.

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 ? Or is there any other way to pass Alarm Actions to stop an ec2 instance ? Or is there an equivalent for sub account like {CUSTOMER_ACCOUNT} is for root account ?

Oct 5, 2018 in AWS by eatcodesleeprepeat
• 4,710 points
1,582 views

1 answer to this question.

0 votes

Python String and Integer concatenation

>>> 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
answered Oct 5, 2018 by Priyaj
• 58,020 points

Related Questions In AWS

0 votes
1 answer
0 votes
1 answer

Import my AWS credentials using python script

Using AWS Cli  Configure your IAM user then ...READ MORE

answered Nov 16, 2018 in AWS by Jino
• 5,820 points
2,868 views
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to Pass the VPC ID while creating the Ec2 instance in AWS using Python Boto3

import boto3 ec2 = boto3.resource('ec2') instance = ec2.create_instances( ...READ MORE

answered Jan 29, 2019 in AWS by Priyaj
• 58,020 points
3,547 views
0 votes
1 answer

How can I disable a user's password in AWS using boto3

delete_login_profile is the one you should use if ...READ MORE

answered Sep 27, 2018 in AWS by Priyaj
• 58,020 points
3,049 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP