We help IT Professionals succeed at work.

Python Script to return all instances in all regions

legolasthehansy
on
4,172 Views
Last Modified: 2018-08-25
I'm trying to get all running instances in all regions to shut them down off hours and this is the script I use. However, I get a Syntax error: return outside function. It maybe a indentation problem which I'm not able find out. Can you help?

import boto3
import logging

client = boto3.client('ec2')

instances = []
for region in client.describe_regions()['Regions']:
    ec2 = boto3.resource('ec2', region_name=region['RegionName'])
    result = ec2.instances.filter(Filters=[{'Name': 'instance-state-name', 'Values': ['running']}])
for instance in result:
    instances.append(instance.id)
return instances

print instances

Open in new window

Comment
Watch Question

ZhaawZSoftware Developer

Commented:
Remove line 12 ("return instances")

Author

Commented:
Thanks for your response. But how do I see the output or see the results of running instances?
Software Developer
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
That was it. The second for loop did not have a indentation as expected and didn't give me an output I was expecting. Cheers!