Python Script to return all instances in all regions

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

LVL 11
legolasthehansyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZhaawZSoftware DeveloperCommented:
Remove line 12 ("return instances")
0
legolasthehansyAuthor Commented:
Thanks for your response. But how do I see the output or see the results of running instances?
0
ZhaawZSoftware DeveloperCommented:
I haven't used AWS, Amazon EC2 or boto3, so I can only guess what "instance" means in this case.
1) You should remove "return instances", because keyword "return" may be used only in functions to return function's result. In your case, you don't have any function there, therefore you got syntax error. After removing that line, you should get some output (assuming that there are no other errors).
2) I believe you should indent lines 10 and 11, otherwise the second loop (for instance in result) is executed only for last value of "result" variable. If the first loop is not run at all, you will probably get "name 'result' is not defined" exception.
3) line 14 ("print instances") should print to stdout(?) contents of "instances" list. I have no idea how you run the script. If run it from the console, you should see the output in the console.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
legolasthehansyAuthor 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!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.