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 boto3import loggingclient = 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 instancesprint instances
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.