I was trying to fetch auto scaling groups with Application tag value as ‘CCC’.
The list is as below,
JavaScript
x
13
13
1
gweb
2
prd-dcc-eap-w2
3
gweb
4
prd-dcc-emc
5
gweb
6
prd-dcc-ems
7
CCC
8
dev-ccc-wer
9
CCC
10
dev-ccc-gbg
11
CCC
12
dev-ccc-wer
13
The script I coded below gives output which includes one ASG without CCC tag.
JavaScript
1
24
24
1
#!/usr/bin/python
2
import boto3
3
4
client = boto3.client('autoscaling',region_name='us-west-2')
5
6
response = client.describe_auto_scaling_groups()
7
8
ccc_asg = []
9
10
all_asg = response['AutoScalingGroups']
11
for i in range(len(all_asg)):
12
all_tags = all_asg[i]['Tags']
13
for j in range(len(all_tags)):
14
if all_tags[j]['Key'] == 'Name':
15
asg_name = all_tags[j]['Value']
16
# print asg_name
17
if all_tags[j]['Key'] == 'Application':
18
app = all_tags[j]['Value']
19
# print app
20
if all_tags[j]['Value'] == 'CCC':
21
ccc_asg.append(asg_name)
22
23
print ccc_asg
24
The output which I am getting is as below,
JavaScript
1
2
1
['prd-dcc-ein-w2', 'dev-ccc-hap', 'dev-ccc-wfd', 'dev-ccc-sdf']
2
Where as 'prd-dcc-ein-w2'
is an asg with a different tag 'gweb'
. And the last one (dev-ccc-msp-agt-asg)
in the CCC ASG list is missing. I need output as below,
JavaScript
1
5
1
dev-ccc-hap-sdf
2
dev-ccc-hap-gfh
3
dev-ccc-hap-tyu
4
dev-ccc-mso-hjk
5
Am I missing something ?.
Advertisement
Answer
I got it working with below script.
JavaScript
1
23
23
1
#!/usr/bin/python
2
import boto3
3
4
client = boto3.client('autoscaling',region_name='us-west-2')
5
6
response = client.describe_auto_scaling_groups()
7
8
ccp_asg = []
9
10
all_asg = response['AutoScalingGroups']
11
for i in range(len(all_asg)):
12
all_tags = all_asg[i]['Tags']
13
app = False
14
asg_name = ''
15
for j in range(len(all_tags)):
16
if 'Application' in all_tags[j]['Key'] and all_tags[j]['Value'] in ('CCP'):
17
app = True
18
if app:
19
if 'Name' in all_tags[j]['Key']:
20
asg_name = all_tags[j]['Value']
21
ccp_asg.append(asg_name)
22
print ccp_asg
23
Feel free to ask if you have any doubts.